Опытный
 
Профиль
Группа: Участник
Сообщений: 792
Регистрация: 24.12.2007
Репутация: нет Всего: 2
|
| Код | #include "THID.h"
#include <string.h> #include <windows.h> //----------- #define BUFSIZE 1024 #define MD5LEN 16 //-----------
// Valid values for the bCommandReg member of IDEREGS; #define IDE_ATAPI_IDENTIFY 0xA1 // Returns ID sector for ATAPI. #define IDE_ATA_IDENTIFY 0xEC // Returns ID sector for ATA. #define DFP_GET_VERSION 0x00074080 #define DFP_RECEIVE_DRIVE_DATA 0x0007c088
typedef struct _GETVERSIONOUTPARAMS { BYTE bVersion; // Binary driver version. BYTE bRevision; // Binary driver revision. BYTE bReserved; // Not used. BYTE bIDEDeviceMap; // Bit map of IDE devices. DWORD fCapabilities; // Bit mask of driver capabilities. DWORD dwReserved[4]; // For future use. } GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;
//---------------------------------------------------------------------------
std::string THID::GetHDDSerial() { PrepareClass(); // ---- int iTheHDDNumber = 0; // номер диска; // ---- // создаем хэндл диска PHYSICALDRIVEX - номер диска; HANDLE hDisk = CreateFile(L"\\\\.\\PHYSICALDRIVE0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); // ---- if ( hDisk == INVALID_HANDLE_VALUE ) { int i = GetLastError(); SafeReturn(); return m_szHardDriveSerialNumber; } // ---- DWORD dwBytesRet = 0; // ---- GETVERSIONINPARAMS VIP; memset( & VIP, 0, sizeof(VIP)); // ---- if ( DeviceIoControl(hDisk, SMART_GET_VERSION, NULL, 0, & VIP, sizeof(VIP), & dwBytesRet, NULL) == NULL ) { int i = GetLastError(); wchar_t buf[10]; wsprintf(buf,L"%d",i); MessageBox(NULL,buf,L"Error",MB_OK); CloseHandle(hDisk); SafeReturn(); return m_szHardDriveSerialNumber; } // ---- if ( VIP.bIDEDeviceMap <= 0 ) { CloseHandle(hDisk); SafeReturn(); return m_szHardDriveSerialNumber; } // ---- BYTE bIDCmd = (((VIP.bIDEDeviceMap >> iTheHDDNumber) & 0x10) != 0) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY; // ---- if ( bIDCmd != IDE_ATA_IDENTIFY ) { CloseHandle(hDisk); SafeReturn(); return m_szHardDriveSerialNumber; } // ---- SENDCMDINPARAMS scip; memset( & scip, 0, sizeof(scip)); // ---- scip.irDriveRegs.bFeaturesReg = 0; scip.irDriveRegs.bSectorCountReg = 1; scip.irDriveRegs.bSectorNumberReg = 1; scip.irDriveRegs.bCylLowReg = 0; scip.irDriveRegs.bCylHighReg = 0; scip.irDriveRegs.bDriveHeadReg = 0xA0 | ((iTheHDDNumber & 1) << 4); scip.irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY; scip.bDriveNumber = iTheHDDNumber; scip.cBufferSize = 512; // ---- int iBufSize = sizeof(SENDCMDOUTPARAMS) + 512 - 1; // ---- BYTE * IdOutCmd = new BYTE[iBufSize]; memset(IdOutCmd, 0, iBufSize); // ---- if ( DeviceIoControl(hDisk, SMART_RCV_DRIVE_DATA, (LPVOID) & scip, sizeof(SENDCMDINPARAMS) - 1, (LPVOID)IdOutCmd, iBufSize, & dwBytesRet, NULL) != NULL ) { // Указатель на адрес сектора HDD, в котором хранится разная полезная инфа; USHORT * pIdSector = (USHORT*)((PSENDCMDOUTPARAMS)IdOutCmd)->bBuffer; // ---- if ( pIdSector != NULL ) { WORD wDiskData[256]; memset(wDiskData, 0, sizeof(wDiskData)); // ---- // Заполняем наши данные; //for (int ijk = 0; ijk < 256; ijk++) wDiskData[ijk] = pIdSector[ijk]; memcpy(wDiskData, pIdSector, sizeof(WORD) * 256); // ---- // Конвертируем в строку SN; ConvertToString(wDiskData, 10, 19, m_szHardDriveSerialNumber); } } // ---- CloseHandle(hDisk); // ---- delete IdOutCmd; // ---- return m_szHardDriveSerialNumber; } //---------------------------------------------------------------------------
std::string THID::ReadPhysicalDriveInNTWithAdminRights (void) { PrepareClass(); int done = FALSE; int drive = 0; BYTE IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
HANDLE hPhysicalDriveIOCTL = 0;
// Try to get a handle to PhysicalDrive IOCTL, report failure // and exit if can't. TCHAR driveName [256];
// Windows NT, Windows 2000, must have admin rights hPhysicalDriveIOCTL = CreateFile(L"\\\\.\\PHYSICALDRIVE0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE) { GETVERSIONOUTPARAMS VersionParams; DWORD cbBytesReturned = 0;
// Get the version, etc of PhysicalDrive IOCTL memset ((void*) &VersionParams, 0, sizeof(VersionParams));
if ( DeviceIoControl (hPhysicalDriveIOCTL, DFP_GET_VERSION, NULL, 0, &VersionParams, sizeof(VersionParams), &cbBytesReturned, NULL) ) {
// If there is a IDE device at number "i" issue commands // to the device if (VersionParams.bIDEDeviceMap > 0) { BYTE bIDCmd = 0; // IDE or ATAPI IDENTIFY cmd SENDCMDINPARAMS scip;
// Now, get the ID sector for all IDE devices in the system. // If the device is ATAPI use the IDE_ATAPI_IDENTIFY command, // otherwise use the IDE_ATA_IDENTIFY command bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ? \ IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
memset (&scip, 0, sizeof(scip)); memset (IdOutCmd, 0, sizeof(IdOutCmd));
if ( DoIDENTIFY (hPhysicalDriveIOCTL, &scip, (PSENDCMDOUTPARAMS)&IdOutCmd, (BYTE) bIDCmd, (BYTE) drive, &cbBytesReturned)) {
USHORT *pIdSector = (USHORT *) ((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;
if ( pIdSector != NULL ) { WORD wDiskData[256]; memset(wDiskData, 0, sizeof(wDiskData)); // ---- // Заполняем наши данные; //for (int ijk = 0; ijk < 256; ijk++) wDiskData[ijk] = pIdSector[ijk]; memcpy(wDiskData, pIdSector, sizeof(WORD) * 256); // ---- // Конвертируем в строку SN; ConvertToString(wDiskData, 10, 19, m_szHardDriveSerialNumber); } } } } int i = GetLastError(); CloseHandle (hPhysicalDriveIOCTL); }
return MD5(m_szHardDriveSerialNumber); }
BOOL THID::DoIDENTIFY (HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP, PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum, PDWORD lpcbBytesReturned) { // Set up data structures for IDENTIFY command. pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE; pSCIP -> irDriveRegs.bFeaturesReg = 0; pSCIP -> irDriveRegs.bSectorCountReg = 1; pSCIP -> irDriveRegs.bSectorNumberReg = 1; pSCIP -> irDriveRegs.bCylLowReg = 0; pSCIP -> irDriveRegs.bCylHighReg = 0;
// Compute the drive number. pSCIP -> irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);
// The command can either be IDE identify or ATAPI identify. pSCIP -> irDriveRegs.bCommandReg = bIDCmd; pSCIP -> bDriveNumber = bDriveNum; pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;
return ( DeviceIoControl (hPhysicalDriveIOCTL, DFP_RECEIVE_DRIVE_DATA, (LPVOID) pSCIP, sizeof(SENDCMDINPARAMS) - 1, (LPVOID) pSCOP, sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1, lpcbBytesReturned, NULL) ); }
// ф-я конвертирования в строку данных void THID::ConvertToString(WORD wDiskData[256], int firstIndex, int lastIndex, PCHAR szOutString) { char string[1024]; memset(string, 0, sizeof(string)); // ---- int i = 0; int j = 0; int position = 0; // ---- // each integer has two characters stored in it backwards; for (i = firstIndex; i <= lastIndex; i++) { // get high byte for 1st character; string[position] = (char)(wDiskData[i] / 256); position++; // ---- // get low byte for 2nd character; string[position] = (char)(wDiskData[i] % 256); position++; } // ---- // cut off the trailing blanks; // ---- for (i = 0, j = 0; i <= position; i++) { if ( string[i] != ' ' ) { szOutString[j] = string[i]; j++; } } } //---------------------------------------------------------------------------
|
|