Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using DeviceIoControl()

Started by AbhishekP Jun 16, 2007 at 1:09 AM 2 replies 2.1k views
Original Post
AbhishekP
AbhishekP
Hi Can anyone point me to good tutorial on using DeviceIoControl() function and related codes. I need it very badly.
AbhishekP
AbhishekP
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.
LessBread
LessBread
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 tworeasons:The standard device input/output control codes are available only on WindowsNT/Windows 2000.On Microsoft® Windows® 95/98, an application must specify a virtual devicedriver in the CreateFile function - not a specific device.*/#include <windows.h>#include <winioctl.h>BOOLGetDriveGeometry(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                       FILE_SHARE_READ | FILE_SHARE_WRITE,  // share mode                       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);}intmain(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);}
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.