Using IOCTL_DISK_GET_DRIVE_GEOMETRY_EX through DeviceIoControl()

Joined
Apr 29, 2007
Messages
2
Reaction score
0
/* Can anybody help me? The following code is intended to get the HDD Signature. It's returning error 998 although i am running it under an administrator account*/

#include <iostream>
using namespace std;

#ifndef WINVER
#define WINVER 0x0501
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif

#include <windows.h>
#include "Winioctl.h"

int main()
{
HANDLE hPhysicalDrive = CreateFile(
"\\\\.\\PhysicalDrive0",
FILE_ALL_ACCESS,
FILE_SHARE_READ|FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
0,
NULL);

if(hPhysicalDrive==INVALID_HANDLE_VALUE)
{
cout<<"Can\'t open device"<<endl;
return 0;
}

DWORD bytesRet;
DRIVE_LAYOUT_INFORMATION_EX* dlix = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(5120);//Just to ensure enough space

if(dlix==NULL)
{
cout<<"Memory Allocation failed"<<endl;
CloseHandle(hPhysicalDrive);
return 0;
}

BOOL ioctlSucceed = DeviceIoControl(
hPhysicalDrive,
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
(LPVOID)&dlix,
5120,
(LPDWORD)&bytesRet,
NULL);

if(ioctlSucceed)
{
cout<<"Success"<<endl;
cout<<"Number of partitions: "<<dlix->PartitionCount<<endl;
switch(dlix->PartitionStyle)
{
case 0://MBR
cout<<"Partition style is MBR"<<endl;
cout<<"Signature in hex: "<<hex<<uppercase<<dlix->Mbr.Signature<<endl;
break;
case 1://GPT
cout<<"Partition style is GPT"<<endl;
break;
case 2://RAW
cout<<"Partition style is RAW"<<endl;
break;
}
}
else
{
cout<<"DeviceIoControl failed with error: "<<GetLastError()<<endl;
//Output: DeviceIoControl failed with error: 998
}

free((void*)dlix);
CloseHandle(hPhysicalDrive);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top