📄 gethdidinfo.cpp
字号:
#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#include "GetHDIdInfo.h"
#define DFP_GET_VERSION 0x00074080
#define DFP_SEND_DRIVE_COMMAND 0x0007c084
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
#pragma pack(1)
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;
typedef struct _IDEREGS {
BYTE bFeaturesReg; // Used for specifying SMART "commands".
BYTE bSectorCountReg; // IDE sector count register
BYTE bSectorNumberReg; // IDE sector number register
BYTE bCylLowReg; // IDE low order cylinder value
BYTE bCylHighReg; // IDE high order cylinder value
BYTE bDriveHeadReg; // IDE drive/head register
BYTE bCommandReg; // Actual IDE command.
BYTE bReserved; // reserved for future use. Must be zero.
} IDEREGS, *PIDEREGS, *LPIDEREGS;
typedef struct _SENDCMDINPARAMS {
DWORD cBufferSize; // Buffer size in bytes
IDEREGS irDriveRegs; // Structure with drive register values.
BYTE bDriveNumber; // Physical drive number to send
// command to (0,1,2,3).
BYTE bReserved[3]; // Reserved for future expansion.
DWORD dwReserved[4]; // For future use.
//BYTE bBuffer[1]; // Input buffer.
} SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;
// 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.
typedef struct _DRIVERSTATUS {
BYTE bDriverError; // Error code from driver,
// or 0 if no error.
BYTE bIDEStatus; // Contents of IDE Error register.
// Only valid when bDriverError
// is SMART_IDE_ERROR.
BYTE bReserved[2]; // Reserved for future expansion.
DWORD dwReserved[2]; // Reserved for future expansion.
} DRIVERSTATUS, *PDRIVERSTATUS, *LPDRIVERSTATUS;
typedef struct _SENDCMDOUTPARAMS {
DWORD cBufferSize; // Size of bBuffer in bytes
DRIVERSTATUS DriverStatus; // Driver status structure.
BYTE bBuffer[512]; // Buffer of arbitrary length
// in which to store the data read from the drive.
} SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS;
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;
/*+++
Global vars
---*/
GETVERSIONOUTPARAMS VersionParams;
SENDCMDINPARAMS in;
SENDCMDOUTPARAMS out;
HANDLE h;
DWORD i;
BYTE nDrive;
VOID ChangeByteOrder(PCHAR szString, USHORT uscStrSize)
{
USHORT i;
CHAR temp;
for (i = 0; i < uscStrSize; i+=2)
{
temp = szString[i];
szString[i] = szString[i+1];
szString[i+1] = temp;
}
}
//LR: 8 bit as 11111111=DCBAdcba a=drive 0, b=drive 1, c=drive 2, d=drive 3
//abcd 表示是否存在IDE设备
//ABCD 表示对应的IDE设备是否ATAPI设备(is ATAPI or IDE ATA)
void DetectIDE(BYTE bIDEDeviceMap){
if (bIDEDeviceMap&1){
if (bIDEDeviceMap&16){
cout<<"ATAPI device is attached to primary controller, drive 0."<<endl;
}else{
cout<<"IDE device is attached to primary controller, drive 0."<<endl;
}
}
if (bIDEDeviceMap&2){
if (bIDEDeviceMap&32){
cout<<"ATAPI device is attached to primary controller, drive 1."<<endl;
}else{
cout<<"IDE device is attached to primary controller, drive 1."<<endl;
}
}
if (bIDEDeviceMap&4){
if (bIDEDeviceMap&64){
cout<<"ATAPI device is attached to secondary controller, drive 0."<<endl;
}else{
cout<<"IDE device is attached to secondary controller, drive 0."<<endl;
}
}
if (bIDEDeviceMap&8){
if (bIDEDeviceMap&128){
cout<<"ATAPI device is attached to secondary controller, drive 1."<<endl;
}else{
cout<<"IDE device is attached to secondary controller, drive 1."<<endl;
}
}
}
//nIndex=0 to 3
BOOL GetHardDiskID_9X(BYTE nIndex, char* szModelNumber, char* szSerialNumber, char* szFirmwareRev)
{
ZeroMemory(&VersionParams,sizeof(VersionParams));
//We start in 95/98/Me
h=CreateFile("\\\\.\\Smartvsd",0,0,0,CREATE_NEW,0,0);
if (h==INVALID_HANDLE_VALUE){
//cout<<"open smartvsd.vxd failed"<<endl;
return FALSE;//exit(0);
}
if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&VersionParams,sizeof(VersionParams),&i,0)){
cout<<"DeviceIoControl failed:DFP_GET_VERSION"<<endl;
CloseHandle(h);
return FALSE;
}
//If IDE identify command not supported, fails
if (!(VersionParams.fCapabilities&1)){
cout<<"Error: IDE identify command not supported.";
CloseHandle(h);
return FALSE;
}
//Display IDE drive number detected
// DetectIDE(VersionParams.bIDEDeviceMap);
if( !(VersionParams.bIDEDeviceMap > 0) ){
CloseHandle(h);
return FALSE;
}
// If there is a IDE device at number "i" issue commands
// to the device
//Identify the IDE drives
nDrive=nIndex;
if( !(nDrive>=0 && nDrive<4) )return FALSE;
// for (nDrive=0;nDrive<4;nDrive++)
{
PIDSECTOR phdinfo;
char s[41];
ZeroMemory(&in,sizeof(in));
ZeroMemory(&out,sizeof(out));
if (nDrive&1){ //lr: drive 1 or 3: is master driver
in.irDriveRegs.bDriveHeadReg=0xb0;
}else{ //lr: drive 0 or 2: is Slave driver
in.irDriveRegs.bDriveHeadReg=0xa0;
}
// 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
if(VersionParams.bIDEDeviceMap >> nDrive & 0x10){
//if (VersionParams.fCapabilities&(16>>nDrive)){
//We don't detect a ATAPI device.
//return FALSE;//continue;
in.irDriveRegs.bCommandReg = IDE_ATAPI_IDENTIFY;
}else{
in.irDriveRegs.bCommandReg=IDE_ATA_IDENTIFY;//0xec;
}
in.bDriveNumber=nDrive;
in.irDriveRegs.bSectorCountReg=1;
in.irDriveRegs.bSectorNumberReg=1;
in.cBufferSize=512;
if (!DeviceIoControl(h,DFP_RECEIVE_DRIVE_DATA,&in,sizeof(in),&out,sizeof(out),&i,0)){
cout<<"DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
CloseHandle(h);
return FALSE;
}
phdinfo=(PIDSECTOR)out.bBuffer;
memcpy(s,phdinfo->sModelNumber,40);
s[40]=0;
ChangeByteOrder(s,40);
strcpy(szModelNumber,s);//cout<<endl<<"Module Number:"<<s<<endl;
memcpy(s,phdinfo->sFirmwareRev,8);
s[8]=0;
ChangeByteOrder(s,8);
strcpy(szFirmwareRev,s);//cout<<"\tFirmware rev:"<<s<<endl;
memcpy(s,phdinfo->sSerialNumber,20);
s[20]=0;
ChangeByteOrder(s,20);
strcpy(szSerialNumber,s);//cout<<"\tSerial Number:"<<s<<endl;
// cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
}
//Close handle before quit
CloseHandle(h);
return TRUE;
}
//nIndex=0 to 3
BOOL GetHardDiskID_NT(BYTE nIndex, char* szModelNumber, char* szSerialNumber, char* szFirmwareRev)
{
char hd[80];
PIDSECTOR phdinfo;
char s[41];
ZeroMemory(&VersionParams,sizeof(VersionParams));
//We start in NT/Win2000
nDrive=nIndex;
if( !(nDrive>=0 && nDrive<4) )
{
return FALSE;
}
// for (nDrive=0;nDrive<4;nDrive++)
{
sprintf(hd,"\\\\.\\PhysicalDrive%d",nDrive);
//h=CreateFile(hd,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
//if (!h)
h=CreateFile(hd,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if(h == INVALID_HANDLE_VALUE )
{
return FALSE;//continue;
}
if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&VersionParams,sizeof(VersionParams),&i,0))
{
CloseHandle(h);
return FALSE;//continue;
}
//If IDE identify command not supported, fails
if (!(VersionParams.fCapabilities&1))
{
cout<<"Error: IDE identify command not supported.";
CloseHandle(h);
return FALSE;
}
//Identify the IDE drives
ZeroMemory(&in,sizeof(in));
ZeroMemory(&out,sizeof(out));
if (nDrive&1){ //lr: drive 1 or 3: is master driver
in.irDriveRegs.bDriveHeadReg=0xb0;
}else{ //lr: drive 0 or 2: is Slave driver
in.irDriveRegs.bDriveHeadReg=0xa0;
}
// 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
if(VersionParams.bIDEDeviceMap >> nDrive & 0x10){
//if (VersionParams.fCapabilities&(16>>nDrive)){
//We don't detect a ATAPI device.
//return FALSE;//continue;
in.irDriveRegs.bCommandReg = IDE_ATAPI_IDENTIFY;
}else{
in.irDriveRegs.bCommandReg=IDE_ATA_IDENTIFY;//0xec;
}
in.bDriveNumber=nDrive;
in.irDriveRegs.bSectorCountReg=1;
in.irDriveRegs.bSectorNumberReg=1;
in.cBufferSize=512;
if (!DeviceIoControl(h,DFP_RECEIVE_DRIVE_DATA,&in,sizeof(in),&out,sizeof(out),&i,0))
{
cout<<"DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
CloseHandle(h);
return FALSE;
}
phdinfo=(PIDSECTOR)out.bBuffer;
memcpy(s,phdinfo->sModelNumber,40);
s[40]=0;
ChangeByteOrder(s,40);
strcpy(szModelNumber,s);//cout<<endl<<"Module Number:"<<s<<endl;
memcpy(s,phdinfo->sFirmwareRev,8);
s[8]=0;
ChangeByteOrder(s,8);
strcpy(szFirmwareRev,s);//cout<<"\tFirmware rev:"<<s<<endl;
memcpy(s,phdinfo->sSerialNumber,20);
s[20]=0;
ChangeByteOrder(s,20);
strcpy(szSerialNumber,s);//cout<<"\tSerial Number:"<<s<<endl;
// cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
CloseHandle(h);
}
return TRUE;
}
DWORD GetHardDiskKey(BOOL bUse)
{
char szModelNumber[40+1];
char szSerialNumber[20+1];
char szFirmwareRev[8+1];
DWORD dwKeyHD=0;
if(bUse)
{
OSVERSIONINFO VersionInfo;
ZeroMemory(&VersionInfo,sizeof(VersionInfo));
VersionInfo.dwOSVersionInfoSize=sizeof(VersionInfo);
GetVersionEx(&VersionInfo);
switch (VersionInfo.dwPlatformId){
case VER_PLATFORM_WIN32s:
//cout<<"Win32s is not supported by this programm."<<endl;
dwKeyHD=32323232;
break;
case VER_PLATFORM_WIN32_WINDOWS:
if(!GetHardDiskID_9X(0,szModelNumber,szSerialNumber,szFirmwareRev)){
dwKeyHD=95982000;
}
else{
int n=strlen(szModelNumber);
int i;
for(i=0;i<n;i++){
dwKeyHD+=szModelNumber[i]<<16;
}
n=strlen(szSerialNumber);
for(i=0;i<n;i++){
dwKeyHD+=szSerialNumber[i]<<8;
}
n=strlen(szFirmwareRev);
for(i=0;i<n;i++){
dwKeyHD+=szFirmwareRev[i];
}
}
break;
case VER_PLATFORM_WIN32_NT:
if(!GetHardDiskID_NT(0,szModelNumber,szSerialNumber,szFirmwareRev)){
dwKeyHD=20002003;
}
else{
int n=strlen(szModelNumber);
int i;
for(i=0;i<n;i++){
dwKeyHD+=szModelNumber[i]<<16;
}
n=strlen(szSerialNumber);
for(i=0;i<n;i++){
dwKeyHD+=szSerialNumber[i]<<8;
}
n=strlen(szFirmwareRev);
for(i=0;i<n;i++){
dwKeyHD+=szFirmwareRev[i];
}
}
break;
default:
break;
}
}
else
{
dwKeyHD = 0x05f7a94d;
}
return dwKeyHD;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -