Open device name using CreateFile

2020-02-26 08:27发布

问题:

I am working on a simple device driver I want to comunicate with the driver from user-mode using IRP.

I am having trouble opening the device driver. Using DeviceTree I am able to see the device name eg \Device\MyDevice.

But when I try to open it like this :

hand := CreateFile('\Device\MyDevice', GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);  

I always get INVALID_HANDLE_VALUE and GetLastError is (The System cannot find the path specified)

What I am doing wrong ? I know the driver works because I can see it running and printing stuff in DebugView. So any tips ?

回答1:

Here is a good explanation by Tim Robinson, MVP (Windows SDK):

Names of the form \Device\xxx are internal NT object manager names which are inaccessible to Win32. You will only be able to access your device if it creates a symbolic link to \Device\MyDevice from the \??\ directory. Objects in the \??\ kernel directory show up through \\.\ in Win32. Use Winobj in the DDK (or download it from www.sysinternals.com) to check.

NOTE: Nowadays NT namespace root is exposed via GLOBALROOT symbolic link, so any NT path is accessible to Win32 including \Device\xxx: use \\.\GLOBALROOT\Device\xxx. A device symlink is not required in such case.