分享
 
 
 

如何获得硬盘id号,C++篇

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

今天在看C++栏中时,看到一篇文章C++的获得硬盘id号文章,它使用了DLL库,此文的出处http://blog.csdn.net/fangshi168775/archive/2005/09/04/471016.aspx,很不错,它的CODE简单

这里我给出以前的一段纯C++的代码,在BC++下运行通过

#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

{

ULONG HeaderLength;

char Signature[8];

ULONG Timeout;

ULONG ControlCode;

ULONG ReturnCode;

ULONG Length;

} SRB_IO_CONTROL;

#if 0

typedef struct

{

BYTE bFeaturesReg;

BYTE bSectorCountReg;

BYTE bSectorNumberReg;

BYTE bCylLowReg;

BYTE bCylHighReg;

BYTE bDriveHeadReg;

BYTE bCommandReg;

BYTE bReserved;

} IDEREGS;

typedef struct

{

DWORD cBufferSize;

IDEREGS irDriveRegs;

BYTE bDriveNumber;

BYTE bReserved[3];

DWORD dwReserved[4];

BYTE bBuffer[1];

} SENDCMDINPARAMS;

typedef struct

{

DWORD cBufferSize;

DRIVERSTATUS DriverStatus;

BYTE bBuffer[1];

} SENDCMDOUTPARAMS;

#endif

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));

//便于PHP调用,所以注释上面的输出,改用适合与PHP的控制台输出

printf ("%s<br>", ConvertToString (diskdata, 27, 46));

printf ("%s", 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;

}

//---------------------------------------------------------------------------

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有