我必须在一些 VB .NET 项目中使用从 kernel32.dll 导入的 CreateFile 来获取 WinUSB 进一步功能的句柄。
问题是当我不再需要句柄时,我是否需要在 CreateFile 之后以某种方式/某些东西销毁?
第二个问题是我使用带有 CreateFile 返回句柄的 WinUsb_Initialize() 来获取另一个用于 WinUSB 的句柄。我也需要做一些清洁吗?
提前致谢,
<DllImport("kernel32.dll",
SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Shared Function CreateFile(ByVal lpFileName As String,
ByVal dwDesiredAccess As Int32,
ByVal dwShareMode As UInt32,
ByVal lpSecurityAttributes As IntPtr,
ByVal dwCreationDisposition As UInt32,
ByVal dwFlagsAndAttributes As UInt32,
ByVal hTemplateFile As IntPtr) As SafeFileHandle
End Function
<DllImport("kernel32.dll",
CharSet:=CharSet.Auto,
SetLasterror:=True)>
Public Shared Function CloseHandle(ByVal Handle As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
现在我在某处读到,如果我在 CreateFile 中使用安全处理程序,则不需要 CloseHandle。这是我的“断开连接”代码
Dim ErrorStatus As Integer = 0
If hWinUSBInterface <> INVALID_HANDLE_VALUE Then
WinUsb_Free(hWinUSBInterface)
hWinUSBInterface = INVALID_HANDLE_VALUE
End If
If hDevice <> INVALID_HANDLE_VALUE Then
'hDevice = INVALID_HANDLE_VALUE
'RaiseEvent Error(ErrorStatus, "Disconnect")
If CloseHandle(hDevice) Then
hDevice = INVALID_HANDLE_VALUE
RaiseEvent Disconnected()
Else
ErrorStatus = Err.LastDllError
RaiseEvent Error(ErrorStatus, "Disconnect")
End If
End If