📎 AI Summary:
The thread discusses issues with opening handles to storage devices in Windows using CreateFile, with the original poster struggling despite trying various paths and functions. The main solution found was setting the program to run as administrator, which resolved the problem. The overall sentiment indicates that requiring administrative privileges is a common obstacle, and there are suggestions that other methods, like linker options or manifest edits, might allow running with elevated permissions automatically.

ExylonFiber

Well-Known Member
Joined
Apr 25, 2020
Messages
26
Thread Author #1
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)
 

Solution
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

Well-Known Member
Joined
Apr 25, 2020
Messages
26
Thread Author #4
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!
 

Solution

ExylonFiber

Well-Known Member
Joined
Apr 25, 2020
Messages
26
Thread Author #5
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

Windows Forum Team
Staff member
Joined
Jul 4, 2015
Messages
8,995
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