Jump to content
  • Advertisement
Sign in to follow this  

Using DeviceIoControl()

This topic is 4168 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Can anyone point me to good tutorial on using DeviceIoControl() function and related codes. I need it very badly.

Share this post

Link to post
Share on other sites
Do you think I didnt check those? I am searching from 2 days and havent found a good one. All I need is a tutorial with few examples.

Share this post

Link to post
Share on other sites
Yes, I thought you had not searched. You didn't stipulate that you had, you didn't stipulate the programming language you're looking for examples in and so on.

Have you checked out CodeProject? DeviceIoControl+site:codeproject.com

Here's a sample snippet that I found on my hard disk. It's a few years old but should work on Windows XP as well.

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

This example succeeds only when it runs on Windows NT/Windows 2000, for two

The standard device input/output control codes are available only on Windows
NT/Windows 2000.
On Microsoft® Windows® 95/98, an application must specify a virtual device
driver in the CreateFile function - not a specific device.

#include <windows.h>
#include <winioctl.h>

GetDriveGeometry(DISK_GEOMETRY *pdg)
HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results

hDevice = CreateFile("\\\\.\\PhysicalDrive0", // drive to open
0, // don't need any access to the drive
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // don't copy any file's attributes

if (hDevice == INVALID_HANDLE_VALUE) // we can't open the drive
return (FALSE);

bResult = DeviceIoControl(hDevice, // device we are querying
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer, so pass zero
pdg, sizeof(*pdg), // output buffer
&junk, // discard count of bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O

if (!bResult) // IOCTL failed
return (FALSE);

CloseHandle(hDevice); // we're done with the handle

return (bResult);

main(int argc, char *argv[])
DISK_GEOMETRY pdg; // disk drive geometry structure
BOOL bResult; // generic results flag
ULONGLONG DiskSize; // holder for the size of the drive in bytes

bResult = GetDriveGeometry (&pdg);

if (bResult)
printf("Cylinders = %I64d\n", pdg.Cylinders);
printf("Tracks per cylinder = %ld\n", (ULONG) pdg.TracksPerCylinder);
printf("Sectors per track = %ld\n", (ULONG) pdg.SectorsPerTrack);
printf("Bytes per sector = %ld\n", (ULONG) pdg.BytesPerSector);

DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
printf("Disk size = %I64d (Bytes) = %I64d (Mb)\n", DiskSize,
DiskSize / (1024 * 1024));
} else {
printf ("Attempt to get drive geometry failed. Error %ld.\n",
GetLastError ());

return ((int)bResult);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!