void ChangeByteOrder(LPSTR lpString, int nLen)
{ 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; } } //获取硬盘序列号 VOID GetDiskNum(HWND hWnd) { CHAR szDiskNum[40]={0}; SENDCMDINPARAMS scip = { 0 }; CHAR szBuffer[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE] = { 0 }; DWORD dwBytesRet = 0; HANDLE hDevice=INVALID_HANDLE_VALUE; hDevice = CreateFileW(L"\\\\.\\PhysicalDrive0",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0); if(hDevice == INVALID_HANDLE_VALUE) { return; } 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(szDiskNum, pIdSec->sSerialNumber, 20); } SetDlgItemTextA(hWnd,IDC_STATIC_DISKNUM,szDiskNum); return; } |