您好我正在尝试在vb.net中使用SetupDiEnumDriverInfo并继续获取错误代码259.有人可以帮助我。以下代码应该只是剪切并进入vb表单应用程序项目。
这是我的代码。
Imports System.Runtime.InteropServices
Imports Microsoft.Win32.SafeHandles
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
USB.Doit()
End Sub
End Class
Public Class USB
Private Const DIGCF_PRESENT As Integer = &H2
Private Const DIGCF_ALLCLASSES As Integer = &H4
Private Const DIGCF_PROFILE As Integer = &H8
Private Const DIGCF_DEVICEINTERFACE As Integer = &H10
Private Const ERROR_NO_MORE_ITEMS As Integer = 259
Private Const INVALID_HANDLE_VALUE As Integer = -1
Private Const FILE_FLAG_OVERLAPPED As Integer = &H40000000
Private Const FILE_SHARE_READ As Short = &H1S
Private Const FILE_SHARE_WRITE As Short = &H2S
Private Const GENERIC_READ As Integer = &H80000000
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const OPEN_EXISTING As Short = 3
Private Shared GUID_DEVINTERFACE_USB_DEVICE As Guid = New Guid("{A5DCBF10-6530-11D2-901F-00C04FB951ED}")
Private Const LINE_LEN As Integer = 256
Private Const MAX_PATH As Integer = 260
Private Const ANYSIZE_ARRAY As Integer = 1
Public Enum DeviceDriverType As UInt32
NoDriver = 0
ClassDriver = 1
CompatDriver = 2
End Enum
<StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=CharSet.Unicode)> _
Public Structure SP_DEVINFO_DATA
Dim cbSize As Integer
Dim ClassGuid As Guid
Dim DevInst As IntPtr
Dim Reserved As Integer
End Structure
<StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=CharSet.Unicode)> _
Public Structure SP_DRVINFO_DATA
Dim Size As Integer
Dim DriverType As Integer
Dim Reserved As IntPtr
Dim Description As String
Dim Manufacturer As String
Dim Provider As String
Dim ReleaseData As System.Runtime.InteropServices.ComTypes.FILETIME
Dim Version As ULong
End Structure
<DllImport("setupapi.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
Private Shared Function SetupDiGetClassDevsW(<[In]()> ByRef ClassGuid As Guid _
, <MarshalAs(UnmanagedType.LPWStr)> ByVal Enumerator As String _
, ByVal hwndParent As IntPtr _
, ByVal Flags As UInt32) As IntPtr
End Function
<DllImport("setupapi.dll", CharSet:=CharSet.Unicode, setlasterror:=True)> _
Private Shared Function SetupDiDestroyDeviceInfoList(<[In]()> ByRef DeviceInfoSet As IntPtr) As Boolean
End Function
<DllImport("setupapi.dll", setlasterror:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function SetupDiEnumDeviceInfo(ByVal DeviceInfoSet As IntPtr _
, ByVal MemberIndex As UInt32 _
, <[Out]()> ByRef DeviceInfoData As SP_DEVINFO_DATA) As Boolean
End Function
<DllImport("setupapi.dll", setlasterror:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function SetupDiBuildDriverInfoList(ByRef DeviceinfoSet As IntPtr _
, ByRef DeviceInfoData As SP_DEVINFO_DATA _
, ByVal DriverType As Integer) As Boolean
End Function
<DllImport("setupapi.dll", setlasterror:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function SetupDiDestroyDriverInfoList(ByRef DeviceinfoSet As IntPtr _
, ByRef DeviceInfoData As SP_DEVINFO_DATA _
, ByVal DriverType As Integer) As Boolean
End Function
<DllImport("setupapi.dll", setlasterror:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function SetupDiEnumDriverInfo(ByVal DeviceInfoSet As IntPtr _
, ByRef DeviceInfoData As SP_DEVINFO_DATA _
, ByVal DriverType As Integer _
, ByVal MemberIndex As Integer _
, <[Out]()> ByRef DriverInfoData As SP_DRVINFO_DATA) As Boolean
End Function
Public Shared Sub Doit()
Dim _devInfoData As SP_DEVINFO_DATA
_devInfoData.cbSize = Marshal.SizeOf(_devInfoData)
Dim _guid As Guid = Nothing
Dim _idxA as integer = 1
Dim _idxB as integer = 0
'Use a guid that identifies a device on your machine. i used DevMgr from codeproject.
'_guid = New Guid("{4D36E967-E325-11CE-BFC1-08002BE10318}")
Dim _deviceInfoSet As IntPtr = SetupDiGetClassDevsW(_guid, Nothing, IntPtr.Zero, DIGCF_PRESENT)
Dim _errorcode As Integer = 0
If Not SetupDiEnumDeviceInfo(_deviceInfoSet, _idxA, _devInfoData) Then
_errorcode = Marshal.GetLastWin32Error()
Else
If SetupDiBuildDriverInfoList(_deviceInfoSet, _devInfoData, DeviceDriverType.CompatDriver) Then
Dim ss = 0
Else
Dim _drvInfoData As New SP_DRVINFO_DATA
_drvInfoData.Size = Marshal.SizeOf(_drvInfoData)
If Not SetupDiEnumDriverInfo(_deviceInfoSet, _devInfoData, DeviceDriverType.CompatDriver, _idxB, _drvInfoData) Then
_errorcode = Marshal.GetLastWin32Error()
Else
End If
End If
SetupDiDestroyDriverInfoList(_deviceInfoSet, _devInfoData, DeviceDriverType.CompatDriver)
End If
SetupDiDestroyDeviceInfoList(_deviceInfoSet)
End Sub
End Class
以下代码适用于C ++
HDEVINFO hDevInfo = 0L;
SP_DEVINFO_DATA spDevInfoData = {0};
//Guid to my Prolific device.
GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
DWORD _error = 0;
const short wOrder = 1;
short wIdx = 0;
hDevInfo = SetupDiGetClassDevs(&guid, 0L, 0L, DIGCF_PRESENT);
if (hDevInfo == (void*)-1)
{
_error = GetLastError();
};
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if (SetupDiEnumDeviceInfo(hDevInfo
, wOrder
, &spDevInfoData))
{
SP_DRVINFO_DATA spDrvInfoData = {0};
if (!SetupDiBuildDriverInfoList(hDevInfo,
&spDevInfoData,
SPDIT_COMPATDRIVER))
_error = GetLastError();
wIdx = 0;
spDrvInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
if (SetupDiEnumDriverInfo(hDevInfo,
&spDevInfoData,
SPDIT_COMPATDRIVER,
wIdx,
&spDrvInfoData))
{
int i = 0;
}