source.tightDev.net Because you don't need to be big, rich and famous to create something great.

Content of "\UsbHidWrapper\Native.vb"

' x86 and x64 verified!
' Ansi / Unicode unverified.

Imports System.Runtime.InteropServices
Imports DMM.USB.NativeMethods
Imports DMM.USB.NativeTypes

Namespace USB

	''' <summary>Class for native calls (P/Invoke)</summary>
	''' <remarks>Don't touch it. Really. Don't touch this. YOU don't need it. It's only used within USB namespace.</remarks>
	Friend NotInheritable Class NativeMethods

		' Deny CreateInstance for this shared class
		Private Sub New()
		End Sub

		' kernel32.dll
		<DllImport("kernel32.dll", SetLastError:=True)> _
		Friend Shared Function CloseHandle(ByVal hFile As IntPtr) As Integer
		End Function
		<DllImport("kernel32.dll", SetLastError:=True)> _
		Friend Shared Function CreateFile( _
		  <MarshalAs(UnmanagedType.LPStr)> ByVal strName As String, _
		  ByVal nAccess As UInt32, _
		  ByVal nShareMode As UInt32, _
		  ByVal lpSecurity As IntPtr, _
		  ByVal nCreationFlags As UInt32, _
		  ByVal nAttributes As UInt32, _
		  ByVal lpTemplate As IntPtr _
		) As IntPtr
		End Function

		' hid.dll
		<DllImport("hid.dll", SetLastError:=True)> _
		Friend Shared Function HidD_FreePreparsedData(ByRef pData As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
		End Function
		<DllImport("hid.dll", SetLastError:=True)> _
		Friend Shared Sub HidD_GetHidGuid(<Out()> ByRef gHid As Guid)
		End Sub
		<DllImport("hid.dll", SetLastError:=True)> _
		Friend Shared Function HidD_GetPreparsedData(ByVal hFile As IntPtr, <Out()> ByRef lpData As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
		End Function
		<DllImport("hid.dll", SetLastError:=True)> _
		Friend Shared Function HidP_GetCaps(ByVal lpData As IntPtr, <Out()> ByRef oCaps As HidCaps) As Integer
		End Function

		' user32.dll
		<DllImport("user32.dll", SetLastError:=True)> _
		Friend Shared Function RegisterDeviceNotification(ByVal hwnd As IntPtr, ByVal oInterface As DeviceBroadcastInterface, ByVal nFlags As UInt32) As IntPtr
		End Function
		<DllImport("user32.dll", SetLastError:=True)> _
		Friend Shared Function UnregisterDeviceNotification(ByVal hHandle As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
		End Function

		' setupapi.dll
		<DllImport("setupapi.dll", SetLastError:=True)> _
		Friend Shared Function SetupDiDestroyDeviceInfoList(ByVal lpInfoSet As IntPtr) As Integer
		End Function
		<DllImport("setupapi.dll", SetLastError:=True)> _
		Friend Shared Function SetupDiEnumDeviceInterfaces( _
		  ByVal lpDeviceInfoSet As IntPtr, _
		  ByVal nDeviceInfoData As UInt32, _
		  ByRef gClass As Guid, _
		  ByVal nIndex As UInt32, _
		  ByRef oInterfaceData As DeviceInterfaceData _
		) As Boolean
		End Function
		<DllImport("setupapi.dll", SetLastError:=True)> _
		Friend Shared Function SetupDiGetClassDevs( _
		  ByRef gClass As Guid, _
		  <MarshalAs(UnmanagedType.LPStr)> ByVal strEnumerator As String, _
		  ByVal hParent As IntPtr, _
		  ByVal nFlags As UInt32 _
		) As IntPtr
		End Function
		<DllImport("setupapi.dll", SetLastError:=True)> _
		Friend Shared Function SetupDiGetDeviceInterfaceDetail( _
		  ByVal lpDeviceInfoSet As IntPtr, _
		  ByRef oInterfaceData As DeviceInterfaceData, _
		  ByRef oDetailData As DeviceInterfaceDetailData, _
		  ByVal nDeviceInterfaceDetailDataSize As UInt32, _
		  ByRef nRequiredSize As UInt32, _
		  ByVal lpDeviceInfoData As IntPtr _
		) As <MarshalAs(UnmanagedType.Bool)> Boolean
		End Function
		<DllImport("setupapi.dll", SetLastError:=True)> _
		Friend Shared Function SetupDiGetDeviceInterfaceDetail( _
		  ByVal lpDeviceInfoSet As IntPtr, _
		  ByRef oInterfaceData As DeviceInterfaceData, _
		  ByVal lpDeviceInterfaceDetailData As IntPtr, _
		  ByVal nDeviceInterfaceDetailDataSize As UInt32, _
		  ByRef nRequiredSize As UInt32, _
		  ByVal lpDeviceInfoData As IntPtr _
		) As <MarshalAs(UnmanagedType.Bool)> Boolean
		End Function



		' Santas little helper
		Friend Shared Function RegisterForUsbEvents(ByVal hWnd As IntPtr, ByVal gClass As Guid) As IntPtr

			Dim Data As New DeviceBroadcastInterface
			With Data
				.Size = Marshal.SizeOf(Data)
				.ClassGuid = gClass
				.DeviceType = 5
				.Reserved = 0
			End With
			Return RegisterDeviceNotification(hWnd, Data, 0)

		End Function
		Friend Shared Function UnregisterForUsbEvents(ByVal hHandle As IntPtr) As Boolean
			Return UnregisterDeviceNotification(hHandle)
		End Function
		Friend Shared ReadOnly Property HIDGuid() As Guid
			Get
				Dim guid As Guid
				HidD_GetHidGuid(guid)
				Return guid
			End Get
		End Property

	End Class



	''' <summary>Class for native types (P/Invoke)</summary>
	''' <remarks>Don't touch it. Really. Don't touch this. YOU don't need it. It's only used within USB namespace.</remarks>
	Friend NotInheritable Class NativeTypes

		' Deny CreateInstance for this shared class
		Private Sub New()
		End Sub



		' Constants (more or less)
		Friend Const DEVICE_NOTIFY_WINDOW_HANDLE As Integer = 0
		Friend Const DEVICE_ARRIVAL As Integer = &H8000
		Friend Const DEVICE_REMOVECOMPLETE As Integer = &H8004
		Friend Const DEVTYP_DEVICEINTERFACE As Integer = 5
		Friend Const DIGCF_PRESENT As Integer = 2
		Friend Const DIGCF_DEVICEINTERFACE As Integer = &H10
		Friend Const ERROR_IO_PENDING As UInt32 = &H3E5UI
		Friend Const FILE_FLAG_OVERLAPPED As UInt32 = &H40000000UI
		Friend Const FILE_SHARE_READ As UInt32 = 1UI
		Friend Const FILE_SHARE_WRITE As UInt32 = 2UI
		Friend Const GENERIC_WRITE As UInt32 = &H40000000UI
		Friend Const GENERIC_READ As UInt32 = &H80000000UI
		Friend Const INFINITE As UInt32 = UInt32.MaxValue
		Friend Const OPEN_EXISTING As UInt32 = 3UI
		Friend Const OPEN_ALWAYS As UInt32 = 4UI
		Friend Const PURGE_TXABORT As UInt32 = 1UI
		Friend Const PURGE_RXABORT As UInt32 = 2UI
		Friend Const PURGE_TXCLEAR As UInt32 = 4UI
		Friend Const PURGE_RXCLEAR As UInt32 = 8UI
		Friend Const WM_DEVICECHANGE As Integer = &H219
		Friend Shared ReadOnly InvalidHandleValue As IntPtr = New IntPtr(-1)
		Friend Shared ReadOnly NullHandle As IntPtr = IntPtr.Zero



		' Structures
		<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode, Pack:=1)> _
		Friend Class DeviceBroadcastInterface
			Friend Size As Integer
			Friend DeviceType As Integer
			Friend Reserved As Integer
			Friend ClassGuid As Guid
			<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=&H100)> Friend Name As String
		End Class
		<StructLayout(LayoutKind.Sequential, Pack:=1)> _
		Friend Structure DeviceInterfaceData
			Friend Size As Integer
			Friend InterfaceClassGuid As Guid
			Friend Flags As Integer
			Friend Reserved As Integer
			Friend Sub New(ByVal SetSize As Boolean)
				If SetSize Then Size = 24 + Marshal.SizeOf(GetType(IntPtr)) ' 28 on x86, 32 on x64
			End Sub
		End Structure
		<StructLayout(LayoutKind.Sequential, Pack:=1)> _
		Friend Structure DeviceInterfaceDetailData
			Friend Size As Integer
			<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=&H100)> Friend DevicePath As String
			Friend Sub New(ByVal SetSize As Boolean)
				If SetSize Then
					Size = Marshal.SizeOf(GetType(IntPtr))
					If Size = 4 Then Size = 5 ' 4 + padding byte for x86, 8 for x64
				End If
			End Sub
		End Structure
		<StructLayout(LayoutKind.Sequential, Pack:=1)> _
		Friend Structure HidCaps
			Friend Usage As Short
			Friend UsagePage As Short
			Friend InputReportByteLength As Short
			Friend OutputReportByteLength As Short
			Friend FeatureReportByteLength As Short
			<MarshalAs(UnmanagedType.ByValArray, SizeConst:=&H11)> Friend Reserved As Short()
			Friend NumberLinkCollectionNodes As Short
			Friend NumberInputButtonCaps As Short
			Friend NumberInputValueCaps As Short
			Friend NumberInputDataIndices As Short
			Friend NumberOutputButtonCaps As Short
			Friend NumberOutputValueCaps As Short
			Friend NumberOutputDataIndices As Short
			Friend NumberFeatureButtonCaps As Short
			Friend NumberFeatureValueCaps As Short
			Friend NumberFeatureDataIndices As Short
		End Structure
		<StructLayout(LayoutKind.Sequential, Pack:=1)> _
		Friend Structure Overlapped
			Friend Internal As UInt32
			Friend InternalHigh As UInt32
			Friend Offset As UInt32
			Friend OffsetHigh As UInt32
			Friend [Event] As IntPtr
		End Structure

	End Class

End Namespace