Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Management
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Windows.Forms
Public Class Form1
Dim HID_Handle As Integer
Public Enum DIGCF
DIGCF_DEFAULT = &H1
DIGCF_PRESENT = &H2
DIGCF_ALLCLASSES = &H4
DIGCF_PROFILE = &H8
DIGCF_DEVICEINTERFACE = &H10
End Enum
Structure SP_DEVICE_INTERFACE_DATA
Public cbSize As Integer
Public interfaceClassGuid As Guid
Public flags As Integer
Public reserved As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Class SP_DEVINFO_DATA
Public cbSize As Integer = Marshal.SizeOf(GetType(SP_DEVINFO_DATA))
Public classGuid As Guid = Guid.Empty
Public devInst As Integer = 0
Public reserved As Integer = 0
End Class
<StructLayout(LayoutKind.Sequential, Pack:=2)> _
Protected Structure SP_DEVICE_INTERFACE_DETAIL_DATA
Public cbSize As Integer
Public devicePath As Short
End Structure
Public Declare Function HidD_GetHidGuid Lib "hid.dll" Alias "HidD_GetHidGuid" (ByRef HidGuid As Guid) As Integer
Public Declare Function SetupDiGetClassDevs Lib "setupapi.dll" Alias "SetupDiGetClassDevsA" (ByRef ClassGuid As Guid, ByVal Enumerator As UInteger, ByVal HwndParent As IntPtr, ByVal Flags As DIGCF) As IntPtr
Public Declare Function SetupDiEnumDeviceInterfaces Lib "setupapi.dll" Alias "SetupDiEnumDeviceInterfaces" (ByVal hDevInfo As IntPtr, ByVal devInfo As IntPtr, ByRef interfaceClassGuid As Guid, ByVal memberIndex As UInteger, ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
Public Declare Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" Alias "SetupDiGetDeviceInterfaceDetailA" (ByVal deviceInfoSet As IntPtr, ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByVal deviceInterfaceDetailData As IntPtr, ByVal deviceInterfaceDetailDataSize As Integer, ByRef requiredSize As Integer, ByVal deviceInfoData As SP_DEVINFO_DATA) As Boolean
Public Declare Function WriteFile Lib "Kernel32.dll" Alias "WriteFile" (ByVal hFile As Integer, ByVal lpBuffer() As Byte, ByVal nNumberOfBytesToWrite As UInteger, ByRef lpNumberOfBytesWritten As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
Public Declare Function CreateFile Lib "Kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As UInteger, ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As UInteger, ByVal dwCreationDisposition As UInteger, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As UInteger) As Integer
Public Declare Function ReadFile Lib "Kernel32.dll" Alias "ReadFile" (ByVal hFile As IntPtr, ByVal lpBuffer() As Byte, ByVal nNumberOfBytesToRead As UInteger, ByRef lpNumberOfBytesRead As UInteger, ByVal lpOverlapped As IntPtr) As Boolean REM 黍?扢掘恅璃
Public Declare Function HidD_FreePreparsedData Lib "hid.dll" Alias "HidD_FreePreparsedData" (ByRef PreparsedData As IntPtr) As Boolean REM 庋溫扢掘
Public Declare Function CloseHandle Lib "kernel32.dll" Alias "CloseHandle" (ByVal hObject As Integer) As Integer REM 壽敕溼恀扢掘曆梟
Dim guidHID As Guid = Guid.Empty
Dim classguidhid As Guid = Guid.Empty
Dim hDevInfo As IntPtr
Public Const GENERIC_READ As UInteger = &H80000000&
Public Const GENERIC_WRITE As UInteger = &H40000000
Public Const FILE_SHARE_READ As UInteger = &H1
Public Const FILE_SHARE_WRITE As UInteger = &H2
Public Const OPEN_EXISTING As UInteger = 3
Public Function UsBInitial() As Integer
HidD_GetHidGuid(guidHID)
hDevInfo = SetupDiGetClassDevs(guidHID, 0, IntPtr.Zero, DIGCF.DIGCF_PRESENT Or DIGCF.DIGCF_DEVICEINTERFACE)
Dim bufferSize As Integer = 0
Dim DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA = New SP_DEVICE_INTERFACE_DATA
DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData)
REM 鳳?扢掘曆梟ㄛtrue鳳?善
Dim result As Boolean = SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, guidHID, 1, DeviceInterfaceData)
Dim strtInterfaceData As SP_DEVINFO_DATA = New SP_DEVINFO_DATA
REM 第一次调用会出错,但是会返回正确的 SIZE
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, IntPtr.Zero, 0, bufferSize, strtInterfaceData)
Dim detailDataBuffer As IntPtr = Marshal.AllocHGlobal(bufferSize)
Dim detailData As SP_DEVICE_INTERFACE_DETAIL_DATA = New SP_DEVICE_INTERFACE_DETAIL_DATA()
detailData.cbSize = Marshal.SizeOf(GetType(SP_DEVICE_INTERFACE_DETAIL_DATA))
Marshal.StructureToPtr(detailData, detailDataBuffer, False)
REM 第二次即可调用成功
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, detailDataBuffer, bufferSize, bufferSize, strtInterfaceData)
Dim pdevicePathName As IntPtr = CInt(detailDataBuffer) + 4 REM 鳳?扢掘繚噤
Dim devicePathName As String = Marshal.PtrToStringAuto(pdevicePathName) REM 鳳?扢掘繚噤
' MessageBox.Show(devicePathName)
Return CT_CreateFile(devicePathName) REM 蟀諉扢掘恅璃
End Function
Public Function CT_CreateFile(ByVal DeviceName As String) As Integer REM 膘蕾睿扢掘腔蟀諉
Dim HID_Handle As Integer = CreateFile(DeviceName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, 3, 0, 0)
Return HID_Handle
End Function
Public Function USBDataWrite(ByVal HidHandle As Integer) As Boolean REM 跦擂CreateFile鏽善扢掘腔handle迡恅璃ㄛ甜殿隙杅擂
Dim bytesWritten As UInteger = 0
Dim buffer() As Byte = {&H2, &H2, &H0}
Dim status As Boolean = WriteFile(HID_Handle, buffer, 7, bytesWritten, IntPtr.Zero)
If status = False Then
MessageBox.Show("Write data Failed")
Return False
Else
MessageBox.Show("Write data sucsccfully")
Return True
End If
End Function
Public Sub CloseUsbHandle(ByVal HidHandle As Long)
CloseHandle(HID_Handle)
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
HID_Handle = UsBInitial()
USBDataWrite(HID_Handle)
CloseUsbHandle(HID_Handle)
End Sub
End Class
Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Management
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Windows.Forms
Public Class Form1
Dim HID_Handle As Integer
Public Enum DIGCF
DIGCF_DEFAULT = &H1
DIGCF_PRESENT = &H2
DIGCF_ALLCLASSES = &H4
DIGCF_PROFILE = &H8
DIGCF_DEVICEINTERFACE = &H10
End Enum
Structure SP_DEVICE_INTERFACE_DATA
Public cbSize As Integer
Public interfaceClassGuid As Guid
Public flags As Integer
Public reserved As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Class SP_DEVINFO_DATA
Public cbSize As Integer = Marshal.SizeOf(GetType(SP_DEVINFO_DATA))
Public classGuid As Guid = Guid.Empty
Public devInst As Integer = 0
Public reserved As Integer = 0
End Class
<StructLayout(LayoutKind.Sequential, Pack:=2)> _
Protected Structure SP_DEVICE_INTERFACE_DETAIL_DATA
Public cbSize As Integer
Public devicePath As Short
End Structure
Public Declare Function HidD_GetHidGuid Lib "hid.dll" Alias "HidD_GetHidGuid" (ByRef HidGuid As Guid) As Integer
Public Declare Function SetupDiGetClassDevs Lib "setupapi.dll" Alias "SetupDiGetClassDevsA" (ByRef ClassGuid As Guid, ByVal Enumerator As UInteger, ByVal HwndParent As IntPtr, ByVal Flags As DIGCF) As IntPtr
Public Declare Function SetupDiEnumDeviceInterfaces Lib "setupapi.dll" Alias "SetupDiEnumDeviceInterfaces" (ByVal hDevInfo As IntPtr, ByVal devInfo As IntPtr, ByRef interfaceClassGuid As Guid, ByVal memberIndex As UInteger, ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
Public Declare Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" Alias "SetupDiGetDeviceInterfaceDetailA" (ByVal deviceInfoSet As IntPtr, ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByVal deviceInterfaceDetailData As IntPtr, ByVal deviceInterfaceDetailDataSize As Integer, ByRef requiredSize As Integer, ByVal deviceInfoData As SP_DEVINFO_DATA) As Boolean
Public Declare Function WriteFile Lib "Kernel32.dll" Alias "WriteFile" (ByVal hFile As Integer, ByVal lpBuffer() As Byte, ByVal nNumberOfBytesToWrite As UInteger, ByRef lpNumberOfBytesWritten As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
Public Declare Function CreateFile Lib "Kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As UInteger, ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As UInteger, ByVal dwCreationDisposition As UInteger, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As UInteger) As Integer
Public Declare Function ReadFile Lib "Kernel32.dll" Alias "ReadFile" (ByVal hFile As IntPtr, ByVal lpBuffer() As Byte, ByVal nNumberOfBytesToRead As UInteger, ByRef lpNumberOfBytesRead As UInteger, ByVal lpOverlapped As IntPtr) As Boolean REM 黍?扢掘恅璃
Public Declare Function HidD_FreePreparsedData Lib "hid.dll" Alias "HidD_FreePreparsedData" (ByRef PreparsedData As IntPtr) As Boolean REM 庋溫扢掘
Public Declare Function CloseHandle Lib "kernel32.dll" Alias "CloseHandle" (ByVal hObject As Integer) As Integer REM 壽敕溼恀扢掘曆梟
Dim guidHID As Guid = Guid.Empty
Dim classguidhid As Guid = Guid.Empty
Dim hDevInfo As IntPtr
Public Const GENERIC_READ As UInteger = &H80000000&
Public Const GENERIC_WRITE As UInteger = &H40000000
Public Const FILE_SHARE_READ As UInteger = &H1
Public Const FILE_SHARE_WRITE As UInteger = &H2
Public Const OPEN_EXISTING As UInteger = 3
Public Function UsBInitial() As Integer
HidD_GetHidGuid(guidHID)
hDevInfo = SetupDiGetClassDevs(guidHID, 0, IntPtr.Zero, DIGCF.DIGCF_PRESENT Or DIGCF.DIGCF_DEVICEINTERFACE)
Dim bufferSize As Integer = 0
Dim DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA = New SP_DEVICE_INTERFACE_DATA
DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData)
REM 鳳?扢掘曆梟ㄛtrue鳳?善
Dim result As Boolean = SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, guidHID, 1, DeviceInterfaceData)
Dim strtInterfaceData As SP_DEVINFO_DATA = New SP_DEVINFO_DATA
REM 第一次调用会出错,但是会返回正确的 SIZE
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, IntPtr.Zero, 0, bufferSize, strtInterfaceData)
Dim detailDataBuffer As IntPtr = Marshal.AllocHGlobal(bufferSize)
Dim detailData As SP_DEVICE_INTERFACE_DETAIL_DATA = New SP_DEVICE_INTERFACE_DETAIL_DATA()
detailData.cbSize = Marshal.SizeOf(GetType(SP_DEVICE_INTERFACE_DETAIL_DATA))
Marshal.StructureToPtr(detailData, detailDataBuffer, False)
REM 第二次即可调用成功
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, detailDataBuffer, bufferSize, bufferSize, strtInterfaceData)
Dim pdevicePathName As IntPtr = CInt(detailDataBuffer) + 4 REM 鳳?扢掘繚噤
Dim devicePathName As String = Marshal.PtrToStringAuto(pdevicePathName) REM 鳳?扢掘繚噤
' MessageBox.Show(devicePathName)
Return CT_CreateFile(devicePathName) REM 蟀諉扢掘恅璃
End Function
Public Function CT_CreateFile(ByVal DeviceName As String) As Integer REM 膘蕾睿扢掘腔蟀諉
Dim HID_Handle As Integer = CreateFile(DeviceName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, 3, 0, 0)
Return HID_Handle
End Function
Public Function USBDataWrite(ByVal HidHandle As Integer) As Boolean REM 跦擂CreateFile鏽善扢掘腔handle迡恅璃ㄛ甜殿隙杅擂
Dim bytesWritten As UInteger = 0
Dim buffer() As Byte = {&H2, &H2, &H0}
Dim status As Boolean = WriteFile(HID_Handle, buffer, 7, bytesWritten, IntPtr.Zero)
If status = False Then
MessageBox.Show("Write data Failed")
Return False
Else
MessageBox.Show("Write data sucsccfully")
Return True
End If
End Function
Public Sub CloseUsbHandle(ByVal HidHandle As Long)
CloseHandle(HID_Handle)
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
HID_Handle = UsBInitial()
USBDataWrite(HID_Handle)
CloseUsbHandle(HID_Handle)
End Sub
End Class