Bonjour.
J'ai repris l'exemple de récupération d'informations sur le disque dur de la MSDN.
Je veux en plus lire le MBR du disque via l'API ReadFile.
Le code compile et s'exécute mais GetLastError() renvoie le code erreur 5. Pourquoi à votre avis ?
J'ai testé le prog DiskInfo (
http://www.cppfrance.com/codes/INFORMATIONS-SUR-VOS-DISQUES-DURS-WIN32_28413.aspx) qui utilise la même méthode et qui lui fonctionne...
D'avance merci.
Voici le code source :
#include <stdio.h>
#include
#include <windows.h>
#include <winioctl.h>
#define MBR_SIZEOF 512
using namespace std;
BOOL GetMBR(BYTE *mbr)
{
HANDLE hDevice;
DWORD BytesRead;
hDevice=CreateFile(TEXT("\\\\.\\PhysicalDrive0"), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice==INVALID_HANDLE_VALUE) return (FALSE);
SetFilePointer(hDevice, 0, NULL, FILE_BEGIN);
ReadFile(hDevice, mbr, MBR_SIZEOF, &BytesRead, NULL);
cout << "BytesRead : " << BytesRead << endl;
cout << "GetLastError() : " << GetLastError() << endl;
CloseHandle(hDevice);
}
BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
HANDLE hDevice;
BOOL bResult;
DWORD junk;
hDevice=CreateFile(TEXT("\\\\.\\PhysicalDrive0"), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice==INVALID_HANDLE_VALUE) return (FALSE);
bResult=DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, pdg, sizeof(*pdg), &junk, (LPOVERLAPPED) NULL);
CloseHandle(hDevice);
return bResult;
}
int main(int argc, char *argv[])
{
DISK_GEOMETRY pdg;
BOOL bResult;
ULONGLONG DiskSize;
BYTE mbr[MBR_SIZEOF];
bResult=GetDriveGeometry(&pdg);
if (bResult)
{
printf("Cylinders = %I64d\n", pdg.Cylinders);
printf("Tracks/Cylinder = %ld\n", (ULONG)pdg.TracksPerCylinder);
printf("Sectors/Track = %ld\n", (ULONG)pdg.SectorsPerTrack);
printf("Bytes/Sector = %ld\n", (ULONG)pdg.BytesPerSector);
DiskSize=pdg.Cylinders.QuadPart*(ULONG)pdg.TracksPerCylinder*(ULONG)pdg.SectorsPerTrack*(ULONG)pdg.BytesPerSector;
printf("DiskSize
%I64d (Bytes) %I64d (Gb)\n", DiskSize, DiskSize/(1024*1024*1024));
}
else printf("GetGeometryDrive Failed. Error %ld.\n", GetLastError());
GetMBR(mbr);
system("PAUSE");
return EXIT_SUCCESS;
}
SORTIE CONSOLE :
Cylinders = 121601
Tracks/Cylinder = 255
Sectors/Track = 63
Bytes/Sector = 512
DiskSize
1000202273280 (Bytes) 931 (Gb)
BytesRead : 0
GetLastError() : 5
Press any key to continue . . .
Afficher la suite