#include <windows.h>
#include <stdio.h>
#include <string.h>
/**************************************/
// web:itbaby.jss.cn
// 作者:javasuki(itbaby)
// 日期:2003/03/04
/**************************************/
//用于WinNT/Win2000,对Win9X无效
//通过MS的S.M.A.R.T.接口,直接从RING3调用
//API DeviceIoControl()来获取硬盘信息
typedef struct _SRB_IO_CONTROL {
ULONG HeaderLength;
char Signature[8];
ULONG Timeout;
ULONG ControlCode;
ULONG ReturnCode;
ULONG Length;
} SRB_IO_CONTROL;
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 _IDEREGS {
BYTE bFeaturesReg;
BYTE bSectorCountReg;
BYTE bSectorNumberReg;
BYTE bCylLowReg;
BYTE bCylHighReg;
BYTE bDriveHeadReg;
BYTE bCommandReg;
BYTE bReserved;
} IDEREGS;
typedef struct _SENDCMDINPARAMS {
DWORD cBufferSize;
IDEREGS irDriveRegs;
BYTE bDriveNumber;
BYTE bReserved[3];
DWORD dwReserved[4];
BYTE bBuffer[1];
} SENDCMDINPARAMS;
typedef struct _SENDCMDOUTPARAMS {
DWORD cBufferSize;
DRIVERSTATUS DriverStatus;
BYTE bBuffer[1];
} SENDCMDOUTPARAMS;
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;
#define IDE_ATAPI_IDENTIFY 0xA1
#define IDE_ATA_IDENTIFY 0xEC
#define IDENTIFY_BUFFER_SIZE 512
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
#define IOCTL_SCSI_MINIPORT 0x0004d008
#define IOCTL_SCSI_MINIPORT_IDENTIFY 0x001b0501
#define DATA_SIZE (sizeof(SENDCMDINPARAMS)-1+IDENTIFY_BUFFER_SIZE)
#define BUFFER_SIZE (sizeof(SRB_IO_CONTROL)+DATA_SIZE)
#define W9X_BUFFER_SIZE (IDENTIFY_BUFFER_SIZE+16)
#define SENDIDLENGTH (sizeof(SENDCMDOUTPARAMS)+IDENTIFY_BUFFER_SIZE)
#define PRINTING_TO_CONSOLE_ALLOWED
static char HardDriveSerialNumber [1024];
//-----------------------------------------------------------------
char *ConvertToString (DWORD diskdata [256], int firstIndex, int lastIndex)
{
static char string [1024];
int index = 0;
int position = 0;
for (index = firstIndex; index <= lastIndex; index++){
string [position] = (char) (diskdata [index] / 256);
position++;
string [position] = (char) (diskdata [index] % 256);
position++;
}
string [position] = '\0';
for (index = position - 1; index > 0 && ' ' == string [index]; index--)
string [index] = '\0';
return string;
}
//-----------------------------------------------------------------
void PrintIdeInfo (int drive, DWORD diskdata [256])
{
strcpy (HardDriveSerialNumber, ConvertToString (diskdata, 10, 19));
#ifdef PRINTING_TO_CONSOLE_ALLOWED
switch (drive / 2) {
case 0: //printf ("\nPrimary Controller - ");
break;
case 1: //printf ("\nSecondary Controller - ");
break;
case 2: //printf ("\nTertiary Controller - ");
break;
case 3: //printf ("\nQuaternary Controller - ");
break;
}
switch (drive % 2) {
case 0: //printf ("Master drive\n\n");
break;
case 1: //printf ("Slave drive\n\n");
break;
}
//输出硬盘信息
printf ("Drive Model Number: %s\n", ConvertToString (diskdata, 27, 46));
printf ("Drive Serial Number: %s\n", ConvertToString (diskdata, 10, 19));
printf ("Drive Controller Revision Number__: %s\n", ConvertToString (diskdata, 23, 26));
printf ("Controller Buffer Size on Drive___: %u bytes\n", diskdata [21] * 512);
printf ("Drive Type________________________: ");
if (diskdata [0] & 0x0080)
printf ("Removable\n");
else if (diskdata [0] & 0x0040)
printf ("Fixed\n");
else printf ("Unknown\n");
printf ("Physical Geometry: "
"%u Cylinders %u Heads %u Sectors per track\n",
diskdata [1], diskdata [3], diskdata [6]);
#else // PRINTING_TO_CONSOLE_ALLOWED
#endif // PRINTING_TO_CONSOLE_ALLOWED
}
//-----------------------------------------------------------------
int ReadIdeDriveAsScsiDriveInNT (void)
{
int done = FALSE;
int controller = 0;
for (controller = 0; controller < 2; controller++) {
HANDLE hScsiDriveIOCTL = 0;
char driveName [256];
sprintf (driveName, "\\\\.\\Scsi%d:", controller);
hScsiDriveIOCTL = CreateFile (driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
// if (hScsiDriveIOCTL == INVALID_HANDLE_VALUE)
// printf ("Unable to open SCSI controller %d, error code: 0x%lX\n",
// controller, GetLastError ());
if (hScsiDriveIOCTL != INVALID_HANDLE_VALUE){
int drive = 0;
for (drive = 0; drive < 2; drive++) {
char buffer [sizeof (SRB_IO_CONTROL) + SENDIDLENGTH];
SRB_IO_CONTROL *p = (SRB_IO_CONTROL *)buffer;
SENDCMDINPARAMS *pin =(SENDCMDINPARAMS *)(buffer + sizeof (SRB_IO_CONTROL));
DWORD dummy;
memset (buffer, 0, sizeof (buffer));
p -> HeaderLength = sizeof (SRB_IO_CONTROL);
p -> Timeout = 10000;
p -> Length = SENDIDLENGTH;
p -> ControlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;
strncpy ((char *) p -> Signature, "SCSIDISK", 8);
pin -> irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;
pin -> bDriveNumber = drive;
if (DeviceIoControl (hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT,
buffer,
sizeof (SRB_IO_CONTROL) +
sizeof (SENDCMDINPARAMS) - 1,
buffer,
sizeof (SRB_IO_CONTROL) + SENDIDLENGTH,
&dummy, NULL)) {
SENDCMDOUTPARAMS *pOut =(SENDCMDOUTPARAMS *)(buffer + sizeof (SRB_IO_CONTROL));
IDSECTOR *pId = (IDSECTOR *)(pOut -> bBuffer);
if (pId -> sModelNumber [0]) {
DWORD diskdata [256];
int ijk = 0;
USHORT *pIdSector = (USHORT *) pId;
for (ijk = 0; ijk < 256; ijk++)
diskdata [ijk] = pIdSector [ijk];
PrintIdeInfo (controller * 2 + drive, diskdata);
done = TRUE;
}
}
}
CloseHandle (hScsiDriveIOCTL);
}
}
return done;
}
//-----------------------------------------------------------------
long getHardDriveComputerID ()
{
int done = FALSE;
__int64 id = 0;
strcpy (HardDriveSerialNumber, "");
if ( ! done) done = ReadIdeDriveAsScsiDriveInNT ();
if (done) {
char *p = HardDriveSerialNumber;
if ( ! strncmp (HardDriveSerialNumber, "WD-W", 4)) p += 5;
for ( ; p && *p; p++) {
if ('-' == *p) continue;
id *= 10;
switch (*p) {
case '0': id += 0; break;
case '1': id += 1; break;
case '2': id += 2; break;
case '3': id += 3; break;
case '4': id += 4; break;
case '5': id += 5; break;
case '6': id += 6; break;
case '7': id += 7; break;
case '8': id += 8; break;
case '9': id += 9; break;
case 'a': case 'A': id += 10; break;
case 'b': case 'B': id += 11; break;
case 'c': case 'C': id += 12; break;
case 'd': case 'D': id += 13; break;
case 'e': case 'E': id += 14; break;
case 'f': case 'F': id += 15; break;
case 'g': case 'G': id += 16; break;
case 'h': case 'H': id += 17; break;
case 'i': case 'I': id += 18; break;
case 'j': case 'J': id += 19; break;
case 'k': case 'K': id += 20; break;
case 'l': case 'L': id += 21; break;
case 'm': case 'M': id += 22; break;
case 'n': case 'N': id += 23; break;
case 'o': case 'O': id += 24; break;
case 'p': case 'P': id += 25; break;
case 'q': case 'Q': id += 26; break;
case 'r': case 'R': id += 27; break;
case 's': case 'S': id += 28; break;
case 't': case 'T': id += 29; break;
case 'u': case 'U': id += 30; break;
case 'v': case 'V': id += 31; break;
case 'w': case 'W': id += 32; break;
case 'x': case 'X': id += 33; break;
case 'y': case 'Y': id += 34; break;
case 'z': case 'Z': id += 35; break;
}
}
}
if (id > 268435455) id %= 268435456;
#ifdef PRINTING_TO_CONSOLE_ALLOWED
//printf ("\nComputer ID_______________________: %d\n", id);
#endif
return (long) id;
}
//-----------------------------------------------------------------
int main (int argc, char * argv [])
{
OSVERSIONINFO ver;
ver.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
GetVersionEx(&ver);
if(VER_PLATFORM_WIN32_NT==ver.dwPlatformId)
getHardDriveComputerID ();
else
printf("不能在Win9X下运行!!!\n");
return 0;
}
2:
/*+++
HDID.CPP
Written by Lu Lin
http://lu0.126.com
2000.11.3
---*/
#include <windows.h>
#include <iostream>
#include <cstdio>
using namespace std;
#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;
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 vers;
SENDCMDINPARAMS in;
SENDCMDOUTPARAMS out;
HANDLE h;
DWORD i;
BYTE j;
void CopyRight(){
cerr<<endl<<"HDD identifier v1.0 for WIN95/98/Me/NT/2000. written by Lu Lin"<<endl;
cerr<<"For more information, please visit Inside Programming: http://lu0.126.com"<<endl;
cerr<<"2000.11.3"<<endl<<endl;
}
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;
}
}
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;
}
}
}
void hdid9x(){
ZeroMemory(&vers,sizeof(vers));
//We start in 95/98/Me
h=CreateFile("\\\\.\\Smartvsd",0,0,0,CREATE_NEW,0,0);
if (!h){
cout<<"open smartvsd.vxd failed"<<endl;
exit(0);
}
if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0)){
cout<<"DeviceIoControl failed:DFP_GET_VERSION"<<endl;
CloseHandle(h);
return;
}
//If IDE identify command not supported, fails
if (!(vers.fCapabilities&1)){
cout<<"Error: IDE identify command not supported.";
CloseHandle(h);
return;
}
//Display IDE drive number detected
DetectIDE(vers.bIDEDeviceMap);
//Identify the IDE drives
for (j=0;j<4;j++){
PIDSECTOR phdinfo;
char s[41];
ZeroMemory(&in,sizeof(in));
ZeroMemory(&out,sizeof(out));
if (j&1){
in.irDriveRegs.bDriveHeadReg=0xb0;
}else{
in.irDriveRegs.bDriveHeadReg=0xa0;
}
if (vers.fCapabilities&(16>>j)){
//We don't detect a ATAPI device.
cout<<"Drive "<<(int)(j+1)<<" is a ATAPI device, we don't detect it"<<endl;
continue;
}else{
in.irDriveRegs.bCommandReg=0xec;
}
in.bDriveNumber=j;
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;
}
phdinfo=(PIDSECTOR)out.bBuffer;
memcpy(s,phdinfo->sModelNumber,40);
s[40]=0;
ChangeByteOrder(s,40);
cout<<endl<<"Module Number:"<<s<<endl;
memcpy(s,phdinfo->sFirmwareRev,8);
s[8]=0;
ChangeByteOrder(s,8);
cout<<"\tFirmware rev:"<<s<<endl;
memcpy(s,phdinfo->sSerialNumber,20);
s[20]=0;
ChangeByteOrder(s,20);
cout<<"\tSerial Number:"<<s<<endl;
cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
}
//Close handle before quit
CloseHandle(h);
CopyRight();
}
void hdidnt(){
char hd[80];
PIDSECTOR phdinfo;
char s[41];
ZeroMemory(&vers,sizeof(vers));
//We start in NT/Win2000
for (j=0;j<4;j++){
sprintf(hd,"\\\\.\\PhysicalDrive%d",j);
h=CreateFile(hd,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if (!h){
continue;
}
if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0)){
CloseHandle(h);
continue;
}
//If IDE identify command not supported, fails
if (!(vers.fCapabilities&1)){
cout<<"Error: IDE identify command not supported.";
CloseHandle(h);
return;
}
//Identify the IDE drives
ZeroMemory(&in,sizeof(in));
ZeroMemory(&out,sizeof(out));
if (j&1){
in.irDriveRegs.bDriveHeadReg=0xb0;
}else{
in.irDriveRegs.bDriveHeadReg=0xa0;
}
if (vers.fCapabilities&(16>>j)){
//We don't detect a ATAPI device.
cout<<"Drive "<<(int)(j+1)<<" is a ATAPI device, we don't detect it"<<endl;
continue;
}else{
in.irDriveRegs.bCommandReg=0xec;
}
in.bDriveNumber=j;
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;
}
phdinfo=(PIDSECTOR)out.bBuffer;
memcpy(s,phdinfo->sModelNumber,40);
s[40]=0;
ChangeByteOrder(s,40);
cout<<endl<<"Module Number:"<<s<<endl;
memcpy(s,phdinfo->sFirmwareRev,8);
s[8]=0;
ChangeByteOrder(s,8);
cout<<"\tFirmware rev:"<<s<<endl;
memcpy(s,phdinfo->sSerialNumber,20);
s[20]=0;
ChangeByteOrder(s,20);
cout<<"\tSerial Number:"<<s<<endl;
cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
CloseHandle(h);
}
CopyRight();
}
void main(){
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;
return;
case VER_PLATFORM_WIN32_WINDOWS:
hdid9x();
return;
case VER_PLATFORM_WIN32_NT:
hdidnt();
return;
}
}
3:
#include <windows.h>
#include <iostream>
using namespace std;
typedef BOOL (WINAPI*DISKID32)(char[],char[]);
int main()
{
char factory[20];//硬盘厂商
char diskID[50];//硬盘id号
HINSTANCE ghDiskID=NULL;
DISKID32 diskIDFunc;
ghDiskID = LoadLibrary("DiskID32.dll");
if(ghDiskID == NULL)
{
cout<<"Load library error!";
return 1;
}
else
{
diskIDFunc = (DISKID32)GetProcAddress(ghDiskID,"DiskID32");
diskIDFunc(factory,diskID);
printf("%s:%s\n",factory,diskID);
FreeLibrary(ghDiskID);
}
return 1;
}