无法获得SP_DEVICE_INTERFACE_DETAIL_DATA的工作 [英] Cant get SP_DEVICE_INTERFACE_DETAIL_DATA to work
问题描述
我整夜都在处理这个问题,而且我知道有很多参考主题列出了如何正确调用此函数,但是无论我如何仍然可以使它起作用,所以我希望有人可以帮助我.
我得到的错误是ErrorCode 1784(提供的用户缓冲区对于请求的操作无效."),这是我的代码:
I have been dealing with this all night long, and i know that there are many references topics that list on how to properly call this function but no matter what i still can make it work so i hope someone can help me out.
The error I''m getting is ErrorCode 1784 ("The supplied user buffer is not valid for the requested operation.") and here is a my code:
Const DIGCF_PRESENT As Integer = 2
Const DIGCF_DEVICEINTERFACE As Integer = 16
Public GUID_DISPLAY_DEVICE_ARRIVAL As Guid = Guid.Parse("{1CA05180-A699-450A-9A0C-DE4FBE3DDD89}")
<StructLayout(LayoutKind.Sequential)> Structure SP_DEVICE_INTERFACE_DETAIL_DATA
Public cbSize As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Public DevicePath As String
End Structure
<StructLayout(LayoutKind.Sequential)> Structure SP_DEVICE_INTERFACE_DATA
Public cbSize As Integer
Public interfaceClassGuid As Guid
Public Flags As Integer
Private Reserved As IntPtr
End Structure
<dllimport("setupapi.dll",> _
Shared Function SetupDiGetDeviceInterfaceDetail(ByVal DeviceInfoSet As IntPtr, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByRef DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA, ByVal DeviceInterfaceDetailDataSize As Integer, ByRef RequiredSize As Integer, ByRef DeviceInfoData As SP_DEVICE_INTERFACE_DATA) As Boolean
End Function
<dllimport("setupapi.dll",> _
Shared Function SetupDiEnumDeviceInterfaces(ByVal DeviceInfoSet As IntPtr, ByVal DeviceInfoData As Integer, ByVal InterfaceClassGuid As Guid, ByVal MemberIndex As UInteger, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
End Function
<dllimport("setupapi.dll",> _
Shared Function SetupDiGetClassDevs(ByVal ClassGuid As Guid, ByVal Enumerator As Integer, ByVal hwndParent As IntPtr, ByVal Flags As UInteger) As IntPtr
End Function
Sub Tester
Dim h As IntPtr = SetupDiGetClassDevs(GUID_DISPLAY_DEVICE_ARRIVAL, 0, IntPtr.Zero, DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)
Dim memberIndex As UInteger = 0
Dim dia As New SP_DEVICE_INTERFACE_DATA()
dia.cbSize = Marshal.SizeOf(dia)
If SetupDiEnumDeviceInterfaces(h, 0, GUID_DISPLAY_DEVICE_ARRIVAL, memberIndex, dia) Then
Dim didd As New SP_DEVICE_INTERFACE_DETAIL_DATA()
didd.cbSize = Marshal.SizeOf(didd)
Dim needbytes As Integer = 0
If Not SetupDiGetDeviceInterfaceDetail(h, dia, didd, didd.cbSize, needbytes, Nothing) Then
'Fails here, and even needbytes is set to 0
End If
End If
End Sub
奇怪的是,我什至无法使NeedBytes正确填充,它总是返回0,我发现在Needbytes中获得适当值的唯一方法是进行初始调用,将大小设置为8(I''m运行64位),这样,needbytes的返回值为216,这似乎是有效的,但是如果我尝试将大小设置为216,我仍然会遇到相同的错误.
我也一直在使用该结构的LayoutKind和Pack属性,但也没有成功,所以我真的没有主意,将不胜感激.
The strange part is that i cannot even get needBytes to be correctly populated, it always returns to 0, the only way i have found to get a proper value in needbytes is to make a initial call setting the size to 8 (I''m running 64 bits), this way, needbytes get returned with a value of 216, which seems valid, but if i try to set the size to 216 i still get the same error.
I have been playing with the LayoutKind and Pack properties of the structure without success as well, so I''m really out of ideas, any help would be really appreciated.
推荐答案
对P/Invoke.net上的大小分配有评论
There''s a comment against the Size assignment on P/Invoke.net
SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)
因此,可能值得检查代码中的赋值.
http://www.pinvoke.net/default.aspx/Structures/SP_DEVICE_INTERFACE_DETAIL_DATA.html [ ^ ]
艾伦.
So it''s probably worth checking the assigned value in your code.
http://www.pinvoke.net/default.aspx/Structures/SP_DEVICE_INTERFACE_DETAIL_DATA.html[^]
Alan.
尝试一下:
如果不是SetupDiGetDeviceInterfaceDetail(h,dia,IntPtr.Zero,0,needbytes,Nothing)然后
Try this:
If Not SetupDiGetDeviceInterfaceDetail(h, dia, IntPtr.Zero, 0, needbytes, Nothing) Then
这篇关于无法获得SP_DEVICE_INTERFACE_DETAIL_DATA的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!