Windows 10 Cannot Create Handle to Storage Device Using CreateFile Function

ExylonFiber

Active Member
I cannot open a handle to any of my storage devices.

Code:
HANDLE h = CreateFile(L"\\\\.\\PhysicalDisk0", GENERIC_READ | GENERIC_WRITE, FILE_READ_ACCESS | FILE_WRITE_ACCESS,
        0, OPEN_EXISTING, 0, 0);

I also tried copying the full path from WinObj: \GLOBAL??\PhysicalDrive0 and \\.\PhysicalDisk0 and \\\\?\\PhysicalDisk0

I also tried all CreateFile Variants (CreateFile, CreateFileA, CreateFileW)

What the heck is going on with it here?

Does it have to be a Kernel Mode Driver project in Visual Studio? CPP or C?

Any help would be much appreciated as I have read the documentation and answers on forums very carefully to be sure I am doing this right.

Thank you,
ExylonFiber (Xylianth)
 

ExylonFiber

Active Member
I found the solution to my problem:

I found my program in my filesystem (C:\Users\<username>\source\repos\<projectname>\x64\Debug) and right clicked on the program and in compatibility settings set it to run as administrator. Started Visual Studio up and voila! I now have a valid handle to my storage device!
 

ExylonFiber

Active Member
Should not need to be a kernel mode driver. Most common issues are documented here.
CreateFileA function (fileapi.h) - Win32 apps or createfilew
I found the solution to my problem:

I found my program in my filesystem (C:\Users\<username>\source\repos\<projectname>\x64\Debug) and right clicked on the program and in compatibility settings set it to run as administrator. Started Visual Studio up and voila! I now have a valid handle to my storage device!
 

Neemobeer

Cyber Security Engineer
Staff member
There should be a linker option to force it to run as admin without that change. Also at least for C# which is mainly what I write you can edit the manifest file to include a InvokeAsAdministrator too
 
Top