QueryDosDevice (kernel32)
Last changed: -66.61.25.155

.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern uint QueryDosDevice(string lpDeviceName, IntPtr lpTargetPath,
   uint ucchMax);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

If you are trying to get a string array of all the Devices on your system, this is the code that I used for this.

Not sure if it's the cleanest... but it works

Sample Code:

private static string[] QueryDosDevice()

{

    // Allocate some memory to get a list of all system devices.
    // Start with a small size and dynamically give more space until we have enough room.
    int returnSize = 0;
    int maxSize = 100;
    string allDevices = null;
    IntPtr mem;
    string [] retval = null;

    while(returnSize == 0)
    {
        mem = Marshal.AllocHGlobal(maxSize);
        if(mem != IntPtr.Zero)
        {
            // mem points to memory that needs freeing
            try
            {
                returnSize = QueryDosDevice(null, mem, maxSize);
                if(returnSize != 0)
                {
                    allDevices = Marshal.PtrToStringAnsi(mem, returnSize);
                    retval = allDevices.Split('\0');
                    break;    // not really needed, but makes it more clear...
                }
                else if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
                {
                    maxSize *= 10;
                }
                else
                {
                    Marshal.ThrowExceptionForHR(GetLastError());
                }
            }
            finally
            {
                Marshal.FreeHGlobal(mem);
            }
        }
        else
        {
            throw new OutOfMemoryException();
        }
    }
    return retval;

}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation