📄 hid.vb
字号:
If Result = 0 Then
success = True
Else
success = False
End If
Catch ex As Exception
Call HandleException(ModuleName, ex)
End Try
End Sub
End Class
Friend Class InputReportViaControlTransfer
Inherits DeviceReport
Protected Overrides Sub ProtectedRead _
(ByVal readHandle As Integer, _
ByVal hidHandle As Integer, _
ByVal writeHandle As Integer, _
ByRef myDeviceDetected As Boolean, _
ByRef inputReportBuffer() As Byte, _
ByRef success As Boolean)
'Purpose : reads an Input report from the device using a control transfer.
'Accepts : readHandle - the handle for reading from the device.
' hidHandle - the handle for other device communications.
' myDeviceDetected - tells whether the device is currently attached.
' readBuffer - contains the requested report.
' success - read success
Try
'***
'API function: HidD_GetInputReport
'Purpose: Attempts to read an Input report from the device using a control transfer.
'Supported under Windows XP and later only.
'Requires:
'A handle to a HID
'A pointer to a buffer containing the report ID and report
'The size of the buffer.
'Returns: true on success, false on failure.
'***
success = HidD_GetInputReport _
(hidHandle, _
inputReportBuffer(0), _
UBound(inputReportBuffer) + 1)
Debug.WriteLine(MyDebugging.ResultOfAPICall("ReadFile"))
Debug.WriteLine("")
Catch ex As Exception
Call HandleException(ModuleName, ex)
End Try
End Sub
End Class
Friend MustInherit Class HostReport
'For reports the host sends to the device.
'Each report class defines a ProtectedWrite method for writing a type of report.
Protected MustOverride Function ProtectedWrite _
(ByVal deviceHandle As Integer, _
ByVal reportBuffer() As Byte) _
As Boolean
Friend Function Write _
(ByVal reportBuffer() As Byte, _
ByVal deviceHandle As Integer) _
As Boolean
Dim Success As Boolean
'Purpose : Calls the overridden ProtectedWrite routine.
' : This method enables other classes to override ProtectedWrite
' : while limiting access as Friend.
' : (Directly declaring Write as Friend MustOverride causes the
' : compiler(warning) "Other languages may permit Friend
' : Overridable members to be overridden.")
'Accepts : reportBuffer - contains the report ID and report data.
' : deviceHandle - handle to the device. '
'Returns : True on success. False on failure.
Try
Success = ProtectedWrite _
(deviceHandle, _
reportBuffer)
Return Success
Catch ex As Exception
Call HandleException(ModuleName, ex)
End Try
End Function
End Class
Class OutFeatureReport
Inherits HostReport
'For Feature reports the host sends to the device.
Protected Overrides Function ProtectedWrite _
(ByVal hidHandle As Integer, _
ByVal outFeatureReportBuffer() As Byte) _
As Boolean
'Purpose : writes a Feature report to the device.
'Accepts : hidHandle - a handle to the device.
' featureReportBuffer - contains the report ID and report to send.
'Returns : True on success. False on failure.
Dim Count As Short
Dim Success As Boolean
Try
'***
'API function: HidD_SetFeature
'Purpose: Attempts to send a Feature report to the device.
'Accepts:
'A handle to a HID
'A pointer to a buffer containing the report ID and report
'The size of the buffer.
'Returns: true on success, false on failure.
'***
Success = HidD_SetFeature _
(hidHandle, _
outFeatureReportBuffer(0), _
UBound(outFeatureReportBuffer) + 1)
Debug.WriteLine(MyDebugging.ResultOfAPICall("Hidd_SetFeature"))
Debug.WriteLine("")
Debug.WriteLine(" FeatureReportByteLength = " & UBound(OutFeatureReportBuffer) + 1)
Debug.WriteLine(" Report ID: " & OutFeatureReportBuffer(0))
Debug.WriteLine(" Report Data:")
For Count = 1 To CShort(UBound(OutFeatureReportBuffer))
Debug.WriteLine(" " & Hex(OutFeatureReportBuffer(Count)))
Next Count
Return Success
Catch ex As Exception
Call HandleException(ModuleName, ex)
End Try
End Function
End Class
Class OutputReport
Inherits HostReport
'For Output reports the host sends to the device.
'Uses interrupt or control transfers depending on the device and OS.
Protected Overrides Function ProtectedWrite _
(ByVal writeHandle As Integer, _
ByVal outputReportBuffer() As Byte) _
As Boolean
'Purpose : writes an Output report to the device.
'Accepts : writeHandle - a handle to the device.
' OutputReportBuffer - contains the report ID and report to send.
'Returns : True on success. False on failure.
Dim Count As Short
Dim NumberOfBytesWritten As Integer
Dim Result As Boolean
Try
'The host will use an interrupt transfer if the the HID has an interrupt OUT
'endpoint (requires USB 1.1 or later) AND the OS is NOT Windows 98 Gold (original version).
'Otherwise the the host will use a control transfer.
'The application doesn't have to know or care which type of transfer is used.
NumberOfBytesWritten = 0
'***
'API function: WriteFile
'Purpose: writes an Output report to the device.
'Accepts:
'A handle returned by CreateFile
'The output report byte length returned by HidP_GetCaps.
'An integer to hold the number of bytes written.
'Returns: True on success, False on failure.
'***
Result = WriteFile _
(writeHandle, _
outputReportBuffer(0), _
UBound(outputReportBuffer) + 1, _
NumberOfBytesWritten, _
0)
Debug.WriteLine(MyDebugging.ResultOfAPICall("WriteFile"))
Debug.WriteLine("")
Debug.WriteLine(" OutputReportByteLength = " & (UBound(outputReportBuffer) + 1))
Debug.WriteLine(" NumberOfBytesWritten = " & NumberOfBytesWritten)
If (Result = True) Then
'On success, display the data written.
Debug.WriteLine(" Report ID: " & outputReportBuffer(0))
Debug.WriteLine(" Report Data:")
For Count = 1 To CShort(UBound(outputReportBuffer))
Debug.WriteLine(" " & Hex(outputReportBuffer(Count)))
Next Count
Else
'On failure, close the handles.
If (writeHandle <> 0) Then
CloseHandle(writeHandle)
Debug.WriteLine(MyDebugging.ResultOfAPICall("CloseHandle (WriteHandle)"))
End If
End If
'Return True on success, False on failure.
Return CBool(Result)
Catch ex As Exception
Call HandleException(ModuleName, ex)
End Try
End Function
End Class
Class OutputReportViaControlTransfer
Inherits HostReport
Protected Overrides Function ProtectedWrite _
(ByVal hidHandle As Integer, _
ByVal outputReportBuffer() As Byte) _
As Boolean
'Purpose : writes an Output report to the device using a control transfer.
'Accepts : hidHandle - a handle to the device.
' outputReportBuffer - contains the report ID and report to send.
'Returns : True on success. False on failure.
Dim Success As Boolean
Dim Count As Short
Try
'***
'API function: HidD_SetOutputReport
'Purpose:
'Attempts to send an Output report to the device using a control transfer.
'Requires Windows XP or later.
'Accepts:
'A handle to a HID
'A pointer to a buffer containing the report ID and report
'The size of the buffer.
'Returns: true on success, false on failure.
'***
Success = HidD_SetOutputReport _
(hidHandle, _
outputReportBuffer(0), _
UBound(outputReportBuffer) + 1)
Debug.WriteLine(MyDebugging.ResultOfAPICall("Hidd_SetFeature"))
Debug.WriteLine("")
Debug.WriteLine(" OutputReportByteLength = " & UBound(OutputReportBuffer) + 1)
Debug.WriteLine(" Report ID: " & OutputReportBuffer(0))
Debug.WriteLine(" Report Data:")
For Count = 1 To CShort(UBound(OutputReportBuffer))
Debug.WriteLine(" " & Hex(OutputReportBuffer(Count)))
Next Count
Return Success
Catch ex As Exception
Call HandleException(ModuleName, ex)
End Try
End Function
End Class
Friend Function FlushQueue _
(ByVal hidHandle As Integer) _
As Boolean
'Purpose : Remove any Input reports waiting in the buffer.
'Accepts : hidHandle - a handle to a device.
'Returns : True on success, False on failure.
Dim Result As Boolean
Try
'***
'API function: HidD_FlushQueue
'Purpose: Removes any Input reports waiting in the buffer.
'Accepts: a handle to the device.
'Returns: True on success, False on failure.
'***
Result = HidD_FlushQueue(HIDHandle)
Debug.WriteLine(MyDebugging.ResultOfAPICall("HidD_FlushQueue, ReadHandle"))
Debug.WriteLine("Result = " & Result)
Return Result
Catch ex As Exception
Call HandleException(ModuleName, ex)
End Try
End Function
Friend Function GetDeviceCapabilities _
(ByVal hidHandle As Integer) _
As HIDP_CAPS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -