Search code examples
windowswinapifilesystemsdevicedevice-driver

Some APIs that support the NT namespace absolute path of the format "\Device\Xxx"


In this document, http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#paths

To make these device objects accessible by Windows applications, the device drivers create a symbolic link (symlink) in the Win32 namespace, "Global??", to their respective device objects. For example, COM0 and COM1 under the "Global??" subdirectory are simply symlinks to Serial0 and Serial1, "C:" is a symlink to HarddiskVolume1, "Physicaldrive0" is a symlink to DR0, and so on. Without a symlink, a specified device "Xxx" will not be available to any Windows application using Win32 namespace conventions as described previously. However, a handle could be opened to that device using any APIs that support the NT namespace absolute path of the format "\Device\Xxx".

What are the APIs? Let me know some such functions please.


For example, we can have a device sitting in the GLOBAL?? namespace:

GLOBAL??\
   COM227

This device we can successfully open using CreateFile:

//Note: we have to prefix it with \\.\ in order to tell CreateFile that
//we want to open something from the Global device namespace.
//Otherwise it will try to open a file
HANDLE hdev = CreateFile("\\.\COM227", GENERIC_READ, 0, null, OPEN_EXISTING, 0, 0);
if (hdev == INVALID_HANDLE_VALUE)
   raise new EWin32Exception(GetLastError);

This device (along with every other device in the Win32 Global?? namespace), actually a symbolic link to the "real" device:

GLOBAL??\
   COM227 (SymbolicLink) ==> \Device\VCP0
Device\
   VCP0  (Device)

So we try to open this real device:

HANDLE hdev = CreateFile("\\.\Device\VCP0", GENERIC_READ, 0, null, OPEN_EXISTING, 0, 0);
if (hdev == INVALID_HANDLE_VALUE)
   raise new EWin32Exception(GetLastError);

But it fails with error code 3 (The system cannot find the specified file).

Short:

  • Works: COM227 (which is an alias of \Device\VCP0)
  • Fails: \Device\VCP0

The problem is that

Which means that CreateFile is not one of the "APIs that support the NT namespace absolute path format of \Device\Xxx".

However, a handle could be opened to that device using any APIs that support the NT namespace absolute path of the format "\Device\Xxx".

What are the APIs?


Solution

  • Benjamin -

    1. The simple fact is that you CAN open a "special device file" in Windows, very much as you do in *nix. This is what I tried to say in my original reply. I stand by everything I said in my first post. And I believe the MSDN link I referred to there does a very good job of explaining this, too.

    2. The syntax for a *nix device file is "/dev/SOME_DEVICE". Multiple devices are (by convention, not necessity) distinguished as "/dev/SOME_DEVICE0", "/dev/SOME_DEVICE1", etc. Device files can also be "aliased" using *nix "symbolic links".

    3. The syntax for a Windows device file is a UNC name. I'm sure you're familiar with UNC shares (for example, "\\myserver\c$"). In all the examples we've discussed above, the server happens to be the local host. Hence "\\.\SOME_RESOURCE_NAME".

    It's really as simple as that.

    And it DOES work.

    Please let me know if you have any further questions.

    Thank you in advance .. PSM