0

Createfile fails while reading mbr on WinXP. Returns -1 i.e INVALID_DEVICE_HANDLE

HANDLE hDisk = CreateFile((LPCWSTR)"\\\\.\\PhysicalDrive0", GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ| FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,NULL );

Any idea why???

andrewsi
  • 10,807
  • 132
  • 35
  • 51

2 Answers2

1

You forgot to add 'L' to the string constant "\\.\PhysicalDrive0".

HANDLE hDisk = CreateFile(L"\\.\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

This is right only when you are using the unicode version of the API, i.e. CreateFileW().

arx
  • 16,686
  • 2
  • 44
  • 61
gureedo
  • 315
  • 1
  • 11
  • 2
    If you are going to use `L"..."` then you should call `CreateFileW()` explicitially: `CreateFileW(L"\\\\.\\PhysicalDrive0", ...)`, otherwise use the `TEXT()` macro instead: `CreateFile(TEXT("\\\\.\\PhysicalDrive0"), ...)`. – Remy Lebeau Sep 12 '12 at 22:13
0

Use this:

HANDLE hDisk = CreateFile(L"\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

It's working for me.

Rick Smith
  • 9,031
  • 15
  • 81
  • 85