Crash on DeviceIoControl call

I'm trying to get partitions offset on my HDD. I do it this way:

PDRIVE_LAYOUT_INFORMATION_EX partitions;
DWORD partitionsSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX) 
                       + 127 * sizeof(PARTITION_INFORMATION_EX);
partitions = (PDRIVE_LAYOUT_INFORMATION_EX)malloc(partitionsSize);

HANDLE h = CreateFile(L"\\\\.\\PhysicalDrive0", FILE_ANY_ACCESS , 
                      FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 
                      OPEN_EXISTING, 0, 0);
if (h == INVALID_HANDLE_VALUE)
{
   // handle error and exit
}
if (DeviceIoControl(h, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, partitions, 
                    partitionsSize, 0, NULL) != 0)
{
   // do stuff with partitions here
}

It works fine on Windows 10, but fails on Windows 7 (on DeviceIoControl call). Debugger output is: Exception at 0x7fefcc3b2b2, code: 0xc0000005: write access violation at: 0x1, flags=0x0

I tried: run as admin, change file access parameters on CreateFile, google (maybe not good enough)

I use: Microsoft Visual Studio 14.0

Upvotes: 1

Views: 629

Answers (1)

I read docs inattentively. The answer is in comments (answered by @RbMm)

one of the last 2 params (lpBytesReturned or lpOverlapped) must not be 0. and visual studio version, run as admin, change file access parameters on CreateFile - can not affect access violation on

Upvotes: 2

Related Questions