c语言 获取硬盘号,C语言获得硬盘序列号

这篇博客介绍了如何使用C语言获取硬盘序列号,通过定义并使用结构体和设备I/O控制来实现。代码示例展示了针对不同IDE设备的检查和获取硬盘信息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++获取硬盘序列号 #include #include #include #pragma argsused

#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 \ 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;

char HardDiskNO[200];

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<

//cout<

if (bIDEDeviceMap&2){ if (bIDEDeviceMap&32){

//cout<

//cout<

if (bIDEDeviceMap&4){ if (bIDEDeviceMap&64){

//cout<

//cout<

} }

if (bIDEDeviceMap&8){

if (bIDEDeviceMap&128){

//cout<

//cout<

void hdid9x(){

ZeroMemory(&vers,sizeof(vers)); //We start in 95/98/Me

h=CreateFile(\if (!h){

//cout<

if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0)){ //cout<

//If IDE identify command not supported, fails if (!(vers.fCapabilities&1)){

//cout<

//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; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值