QueryDosDevice

C# Signature:

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

Alternate C# signature:

[DllImport("kernel32.dll", SetLastError = true)]
static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);

Alternate C# signature useful for retrieving list of devices

[DllImport("Kernel32.dll")]
static extern uint QueryDosDevice([In, Optional] string lpDeviceName, [Out]byte[] rv, uint ucchMax);

VB.NET Signature:

<DllImport("Kernel32.dll", EntryPoint:="QueryDosDevice")>
Public Shared Function QueryDosDevice(lpDeviceName As String, lpTargetPath As System.Text.StringBuilder, ucchMax As Integer) As Integer
End Function

VB.NET Sample Code:

' Use the VB.NET signature above with this code. Converting the C#
' signatures will not work.
Private Function QueryDosDevice(ByVal device As String) As List(Of String)

    Dim returnSize As Integer = 0
    Dim maxSize As UInteger = 65536
    Dim allDevices As String = Nothing
    Dim mem As IntPtr
    Dim retval() As String = Nothing
    Dim results As New List(Of String)

    ' Convert an empty string into Nothing, so 
    ' QueryDosDevice will return everything available.
    If device.Trim = "" Then device = Nothing

    While returnSize = 0
        mem = Marshal.AllocHGlobal(CInt(maxSize))
        If mem <> IntPtr.Zero Then
        Try
            returnSize = CInt(QueryDosDevice(device, mem, maxSize))
            If returnSize <> 0 Then
            allDevices = Marshal.PtrToStringAuto(mem, returnSize)
            retval = allDevices.Split(ControlChars.NullChar)
            Exit Try
            Else
            ' This query produced no results. Exit the loop.
            returnSize = -1
            End If
        Finally
            Marshal.FreeHGlobal(mem)
        End Try
        Else
        Throw New OutOfMemoryException()
        End If
    End While

    If retval IsNot Nothing Then
        For Each result As String In retval
        If result.Trim <> "" Then results.Add(result)
        Next
    End If

    Return results
End Function

C# Sample Code:

Sample Code for alternate C# signature:

Sample C# code retrieving list of devices

Última actualización