Option Explicit (setupapi)
Last changed: anonymous

TODO - a short description

C# Signature:

[DllImport("setupapi.dll", SetLastError=true)]
static extern TODO Option Explicit(TODO);

VB Signature:

Declare Function Option Explicit Lib "setupapi.dll" (TODO) As TODO

User-Defined Types:


Alternative Managed API:

Do you know one? Please contribute it!



Tips & Tricks:

Please add some!

Sample Code:

Please add some!

DocuOption Explicit

Private Const DIGCF_PRESENT As Integer = &H2

Private Const DIGCF_DEVICEINTERFACE As Integer = &H10

Private Const DIGCF_ALLCLASSES As Integer = &H4

Private Const GENERIC_READ = &H80000000

Private Const GENERIC_WRITE = &H40000000

Private Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000



Private Const FILE_SHARE_READ = &H1

Private Const FILE_SHARE_WRITE = &H2

Private Const OPEN_ALWAYS = 4

Private Const OPEN_EXISTING = 3

Private Type GUID

Data1 As Long

Data2 As Integer

Data3 As Integer

Data4(7) As Byte

End Type

Private Type Device_Interface_Data

cbSize As Long

InterfaceClassGuid As GUID

Flags As Long

ReservedPtr As Long

End Type

Private Type Device_Interface_Detail

cbSize As Long

DataPath(256) As Byte

End Type

Private Declare Function SetupDiGetDeviceInterfaceDetail _

Lib “setupapi.dll” Alias “SetupDiGetDeviceInterfaceDetailA” _

(ByVal DeviceInfoSet As Long, DeviceInterfaceData As Any, _

DeviceInterfaceDetailData As Any, _

ByVal DeviceInterfaceDetailDataSize As Long, _

RequiredSize As Long, ByVal DeviceInfoData As Long) As Long

Private Declare Function CreateFile Lib “kernel32″ Alias “CreateFileA” _

(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _

ByVal dwShareMode As Long, lpSecurityAttributes As Any, _

ByVal dwCreationDisposition As Long, _

ByVal dwFlagsAndAttributes As Long, _

ByVal hTemplateFile As Long) As Long

Private Declare Sub CloseHandle Lib “kernel32″ _

(ByVal HandleToClose As Long)

Private Declare Function ReadFile Lib “kernel32″ _

(ByVal Handle As Long, ByVal BufferPtr As Long, _

ByVal ByteCount As Long, BytesReturnedPtr As Long, _

ByVal OverlappedPtr As Long) As Long

Private Declare Function WriteFile Lib “kernel32″ _

(ByVal Handle As Long, Buffer As String, _

ByVal ByteCount As Long, BytesReturnedPtr As Long, _

ByVal OverlappedPtr As Long) As Long

Private Declare Function SetupDiGetClassDevs Lib “setupapi.dll” _

Alias “SetupDiGetClassDevsA” (GuidPtr As Long, _

ByVal EnumPtr As Long, ByVal hwndParent As Long, _

ByVal Flags As Long) As Long

Private Declare Function SetupDiDestroyDeviceInfoList _

Lib “setupapi.dll” _

(ByVal DeviceInfoSet As Long) As Boolean

Private Declare Function SetupDiEnumDeviceInterfaces _

Lib “setupapi.dll” (ByVal Handle As Long, _

ByVal InfoPtr As Long, GuidPtr As Long, _

ByVal MemberIndex As Long, _

InterfaceDataPtr As Long) As Boolean

Private Sub Command1_Click()

Text1.Text = SendToUsbPrinter(”Hello world.”)

End Sub

Function SendToUsbPrinter(PrintOut As String) As Boolean

Dim PrnGuid As GUID

Dim Success As Long, Ret As Long

Dim Openned As Boolean

Dim Buffer(256) As Byte

Dim DeviceInterfaceData As Device_Interface_Data

Dim FunctionClassDeviceData As Device_Interface_Detail

Dim PnPHandle As Long, BytesReturned As Long

Dim I As Long

Dim DeviceName As String, DevIndex As Long, DeviceHandle As Long

Dim ReqdSize As Long

Dim BytesWritten As Long

‘ \\?\usb#vid_0a5f&pid_000a#41a081100503#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}

PrnGuid.Data1 = &H28D78FAD

PrnGuid.Data2 = &H5A12

PrnGuid.Data3 = &H11D1

PrnGuid.Data4(0) = &HAE

PrnGuid.Data4(1) = &H5B

PrnGuid.Data4(2) = &H0

PrnGuid.Data4(3) = &H0

PrnGuid.Data4(4) = &HF8

PrnGuid.Data4(5) = &H3

PrnGuid.Data4 = &HA8

PrnGuid.Data4(7) = &HC2

PnPHandle = SetupDiGetClassDevs(PrnGuid.Data1, 0, 0, _


SendToUsbPrinter = False

If (PnPHandle = -1) Then

MsgBox “Could not attach to PnP node”


DeviceInterfaceData.cbSize = Len(DeviceInterfaceData)

DevIndex = 0

‘ Should be a Do While > looking for the correct devicename…

If SetupDiEnumDeviceInterfaces(PnPHandle, 0, PrnGuid.Data1, _

DevIndex, DeviceInterfaceData.cbSize) Then

FunctionClassDeviceData.cbSize = 5

Success = SetupDiGetDeviceInterfaceDetail(PnPHandle, _

DeviceInterfaceData, FunctionClassDeviceData, _

UBound(FunctionClassDeviceData.DataPath), _

BytesReturned, 0)

If Success = 0 Then

MsgBox “Could not get the name of this device”


DeviceName = “”

I = 0

Do While FunctionClassDeviceData.DataPath 0

DeviceName = DeviceName & _


I = I + 1


Debug.Print DeviceName

DeviceHandle = CreateFile(DeviceName, _





If (DeviceHandle = -1) Then

Debug.Print “Open failed on ” & DeviceName


Ret = WriteFile(DeviceHandle, PrintOut, _

Len(PrintOut), BytesWritten, 0)

Debug.Print “Sent ” & BytesWritten & ” bytes.”

SendToUsbPrinter = True

CloseHandle DeviceHandle

End If

End If


MsgBox “Device not connected”

End If

SetupDiDestroyDeviceInfoList (PnPHandle)

End If

End Function

mentation: Option Explicit on MSDN