首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

读串口硬盘的物理序列号.解决思路

2014-04-21 
读串口硬盘的物理序列号.这是妖哥以前发的代码,我运行有4个错误,为什么呢?(118) : error C2065: Win32Pla

读串口硬盘的物理序列号.
这是妖哥以前发的代码,我运行有4个错误,为什么呢?
(118) : error C2065: 'Win32Platform' : undeclared identifier 
(161) : error C2317: 'try' block starting on line '137' has no catch handlers 
(161) : error C2143: syntax error : missing ';' before '__finally' 
(162) : error C2143: syntax error : missing ';' before '{'


原文:
貌似我的生意最近很火?点名提问的挺多,怎么不见专家分涨呢。汗。。。 

翠花,上代码。 

以下代码在我的机器上试验成功,希捷ST3 250G的串口硬盘,物理序列号可以读取。 

C/C++ code#define  IDENTIFY_BUFFER_SIZE  512

#define  IDE_ATAPI_IDENTIFY  0xA0
#define  IDE_ATA_IDENTIFY    0xEC
#define  DFP_RECEIVE_DRIVE_DATA   0x0007c088

#pragma pack(1)
typedef struct _IDSECTOR
{
    USHORT  wGenConfig;
    USHORT  wNumCyls;
    USHORT  wReserved;
    USHORT  wNumHeads;
    USHORT  wBytesPerTrack;
    USHORT  wBytesPerSector;
    USHORT  wSectorsPerTrack;
    USHORT  wVendorUnique[3];
    CHAR    sSerialNumber[20];
    USHORT  wBufferType;
    USHORT  wBufferSize;
    USHORT  wECCSize;
    CHAR    sFirmwareRev[8];
    CHAR    sModelNumber[40];
    USHORT  wMoreVendorUnique;
    USHORT  wDoubleWordIO;
    USHORT  wCapabilities;
    USHORT  wReserved1;
    USHORT  wPIOTiming;
    USHORT  wDMATiming;
    USHORT  wBS;
    USHORT  wNumCurrentCyls;
    USHORT  wNumCurrentHeads;
    USHORT  wNumCurrentSectorsPerTrack;
    ULONG   ulCurrentSectorCapacity;
    USHORT  wMultSectorStuff;
    ULONG   ulTotalAddressableSectors;
    USHORT  wSingleWordDMA;
    USHORT  wMultiWordDMA;
    BYTE    bReserved[128];
}IDSECTOR, *PIDSECTOR;

typedef struct _DRIVERSTATUS
{
    BYTE  bDriverError;
    BYTE  bIDEStatus;
    BYTE  bReserved[2];
    DWORD  dwReserved[2];
} DRIVERSTATUS, *PDRIVERSTATUS, *LPDRIVERSTATUS;

typedef struct _SENDCMDOUTPARAMS
{
    DWORD         cBufferSize;
    DRIVERSTATUS  DriverStatus;
    BYTE          bBuffer[1];
} SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS;

typedef struct _IDEREGS
{
    BYTE bFeaturesReg;
    BYTE bSectorCountReg;
    BYTE bSectorNumberReg;
    BYTE bCylLowReg;
    BYTE bCylHighReg;
    BYTE bDriveHeadReg;
    BYTE bCommandReg;
    BYTE bReserved;
} IDEREGS, *PIDEREGS, *LPIDEREGS;

typedef struct _SENDCMDINPARAMS
{
    DWORD     cBufferSize;
    IDEREGS   irDriveRegs;
    BYTE bDriveNumber;
    BYTE bReserved[3];
    DWORD     dwReserved[4];
    BYTE      bBuffer[1];
} SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;

#pragma pack()

void ChangeByteOrder(LPSTR lpString, int nLen)
{
    USHORT i;
    CHAR c;

    // 63 63 72 75 6E 2E 63 6F 6D
    for(int i = 0; i < nLen; i += 2)
    {
        c = lpString[i];
        lpString[i] = lpString[i+1];
        lpString[i+1] = c;
    }
}

bool CrnGetHDSerialNumber(LPSTR lpBuf)
{
    strcpy(lpBuf, "");

    HANDLE hDevice;

    if(Win32Platform == VER_PLATFORM_WIN32_NT)
        // 获取第一个硬盘,如果有多个,更改PhysicalDrive?就可以了。
        hDevice = CreateFile("\\\\.\\PhysicalDrive0",


                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                NULL,
                OPEN_EXISTING,
                0,
                0);
    else
        hDevice = CreateFile("\\\\.\\SMARTVSD", 0, 0, NULL, CREATE_NEW, 0, 0);

    if(hDevice == INVALID_HANDLE_VALUE) return false;

    SENDCMDINPARAMS scip = { 0 };
    char szBuffer[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE] = { 0 };
    DWORD dwBytesRet = 0;
    bool bSuccess = false;
    try
    {
        scip.cBufferSize = IDENTIFY_BUFFER_SIZE;

        scip.irDriveRegs.bSectorCountReg = 1;
        scip.irDriveRegs.bSectorNumberReg = 1;
        scip.irDriveRegs.bDriveHeadReg = IDE_ATAPI_IDENTIFY;
        scip.irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;

        if(DeviceIoControl(hDevice,
                DFP_RECEIVE_DRIVE_DATA,
                &scip,
                sizeof(SENDCMDINPARAMS) - 1,
                szBuffer,
                sizeof(szBuffer),
                &dwBytesRet,
                NULL))
        {
            PSENDCMDOUTPARAMS pOut = (PSENDCMDOUTPARAMS)szBuffer;
            PIDSECTOR pIdSec = (PIDSECTOR)(pOut->bBuffer);
            ChangeByteOrder(pIdSec->sSerialNumber, sizeof(pIdSec->sSerialNumber));
            strncpy(lpBuf, pIdSec->sSerialNumber, 20);
            bSuccess = true;
        }
    }
    __finally
    {
        CloseHandle(hDevice);
    }

    return bSuccess;
}
//---------------------------------------------------------------------------
// 测试一哈
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    char sz[255];
    CrnGetHDSerialNumber(sz);
    ShowMessage(String(sz).Trim());
}

[解决办法]
已發送 請查收

热点排行