Pour le N° série du disque dur, je fais comme ca :
[cpp]
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define DFP_GET_VERSION 0x00074080
#define DFP_SEND_DRIVE_COMMAND 0x0007c084
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
#pragma pack(1)
typedef struct _IDEREGS {
BYTE bFeaturesReg;
BYTE bSectorCountReg;
BYTE bSectorNumberReg;
BYTE bCylLowReg;
BYTE bCylHighReg;
BYTE bDriveHeadReg;
BYTE bCommandReg;
BYTE bReserved;
} IDEREGS, *PIDEREGS, *LPIDERE;
typedef struct _SENDCMDINPARAMS {
DWORD cBufferSize;
IDEREGS irDriveRegs;
BYTE bDriveNumber;
BYTE bReserved[3];
DWORD dwReserved[4];
//BYTE bBuffer[1];
} SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;
typedef struct _DRIVERSTATUS {
BYTE bDriverError;
BYTE bIDEStatus;
BYTE bReserved[2];
DWORD dwReserved[2];
} DRIVERSTATUS, *PDRIVERSTATUS, *LPDRIVERSTATUS;
typedef struct _SENDCMDOUTPARAMS {
DWORD cBufferSize;
DRIVERSTATUS DriverStatus;
BYTE bBuffer[512];
} 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;
void SwapByteOrder(char *m_lpString, unsigned short m_size)
{
char ch;
int m_counter;
int m_strLen;
m_counter = 0;
m_strLen = strlen(m_lpString);
for (m_counter; m_counter < m_size; m_counter+=2)
{
ch = m_lpString[m_counter];
m_lpString[m_counter] = m_lpString[m_counter+1];
m_lpString[m_counter+1] = ch;
}
}
int main(char argc, char *argv[])
{
int CurrentDrive;
unsigned long m_bytesRead;
HANDLE m_hDrive;
SENDCMDINPARAMS cmd_InParams;
SENDCMDOUTPARAMS cmd_OutParams;
PIDSECTOR cmd_hdInfos;
CurrentDrive = 0;
m_hDrive = CreateFile("\\\\.\\PhysicalDrive0",GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (m_hDrive == INVALID_HANDLE_VALUE) {
printf("CreateFile Erreur\n");
return -1;
}
ZeroMemory(&cmd_InParams,sizeof(cmd_InParams));
ZeroMemory(&cmd_OutParams,sizeof(cmd_OutParams));
if (NULL != m_hDrive)
{
cmd_InParams.bDriveNumber = CurrentDrive;
cmd_InParams.cBufferSize = 512;
if (CurrentDrive & 1)
cmd_InParams.irDriveRegs.bDriveHeadReg = 0xb0;
else
cmd_InParams.irDriveRegs.bDriveHeadReg = 0xa0;
cmd_InParams.irDriveRegs.bCommandReg = 0xec;
cmd_InParams.irDriveRegs.bSectorCountReg = 1;
cmd_InParams.irDriveRegs.bSectorNumberReg = 1;
m_bytesRead = 0;
if (!DeviceIoControl(m_hDrive, DFP_RECEIVE_DRIVE_DATA, &cmd_InParams, sizeof(cmd_InParams), &cmd_OutParams, sizeof(cmd_OutParams), &m_bytesRead, 0)){
printf("DeviceIoControl Erreur");
Sleep(1500);
CloseHandle(m_hDrive);
return -1;
}
if (NULL != m_bytesRead)
{
char buf[41];
cmd_hdInfos = (PIDSECTOR)cmd_OutParams.bBuffer;
memcpy(buf, cmd_hdInfos->sModelNumber,40);
buf[40]=0;
SwapByteOrder(buf, 40);
printf("Model : \t%s\n", buf);
memcpy(buf, cmd_hdInfos->sFirmwareRev, 8);
buf[8]=0;
SwapByteOrder(buf, 8);
printf("Firmware : \t%s\n", buf);
memcpy(buf, cmd_hdInfos->sSerialNumber, 20);
buf[20]=0;
SwapByteOrder(buf, 20);
printf("Numero de serie : \t%s\n", buf);
printf("Taille du disque : \t%d MO\n", cmd_hdInfos->ulTotalAddressableSectors/2/1024);
printf("Nombre de pistes : \t%d \n", cmd_hdInfos->wNumHeads);
printf("Secteurs par tetes : \t%d \n", cmd_hdInfos->wNumCurrentSectorsPerTrack);
printf("Nombre de cylindre : \t%d \n", cmd_hdInfos->wNumCurrentCyls);
printf("Octets ECC : \t%d \n", cmd_hdInfos->wECCSize);
}
}
getch();
CloseHandle(m_hDrive);
return 0;
}
[/cpp]