Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open partition with CreateFile API?

Tags:

c++

winapi

I want to get handle from drive C:/.I can get handle from disk by:

HANDLE hd = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, NULL);

how to access partition of disk?

when use this code:

HANDLE hd = CreateFile("\\.\C", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, NULL);

and use GetLastError() error code is 123.

ERROR_INVALID_NAME

123 (0x7B)

The filename, directory name, or volume label syntax is incorrect.

from Microsoft doc: image

but not work!

thanks in advance.

like image 223
mohammad madani Avatar asked Apr 27 '16 05:04

mohammad madani


1 Answers

The name you should be using to open a volume is C: and make sure the back slashes are escaped appropriately.

HANDLE hd = CreateFile("\\\\.\\C:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, NULL);

Note that you will probably get error 5 (Access denied), unless the exe is being run with administrator privileges.

like image 134
Steve Avatar answered Sep 23 '22 05:09

Steve