gpt4 book ai didi

c# - Win32Api USB SetupDiGetDeviceInterfaceDetail 失败

转载 作者:行者123 更新时间:2023-12-03 06:47:32 24 4
gpt4 key购买 nike

我正在尝试连接到 USB GPS 设备。如果我通过 CreateFile WinApi 手动创建文件(使用设备管理器中指定的路径),我可以成功连接到设备。

但是,当我尝试通过枚举选择设备时,SetupDiGetDeviceInterfaceDetail 调用失败。

我的 C 代码可以正常工作,但我的 C# 翻译似乎无法正常工作。我尝试了许多变体,但结果基本相同。

有效的 C 代码

// Get enumerator handle for the specified ClassGuid
HDEVINFO theDevInfo = SetupDiGetClassDevs((GUID*)&GUID_DEVINTERFACE_GRMNUSB, NULL, NULL,
DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);

SP_DEVICE_INTERFACE_DATA theInterfaceData;
theInterfaceData.cbSize = sizeof(theInterfaceData);

// populate theInterfaceData which contains device class information
if (!SetupDiEnumDeviceInterfaces(theDevInfo, NULL, (GUID*)&GUID_DEVINTERFACE_GRMNUSB, 0, &theInterfaceData) &&
GetLastError() == ERROR_NO_MORE_ITEMS)
{
gHandle = 0;
return;
}
// This is normally used to obtain the device path information using theInterfaceData obtained above
bool initialized = SetupDiGetDeviceInterfaceDetail(theDevInfo, &theInterfaceData, NULL, 0, &theBytesReturned, NULL);
// theBytesReturned = 83
theDevDetailData =
(PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(theBytesReturned);
theDevDetailData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

bool initialized = SetupDiGetDeviceInterfaceDetail(theDevInfo, &theInterfaceData, theDevDetailData, theBytesReturned, NULL, &theDevInfoData);
<小时/>

C#

[DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(
IntPtr hDevInfo,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
IntPtr deviceInterfaceDetailData,
UInt32 deviceInterfaceDetailDataSize,
out UInt32 requiredSize,
IntPtr deviceInfoData
);

[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public Int32 cbSize;
public Guid interfaceClassGuid;
public Int32 flags;
private UIntPtr reserved;
}


// Get enumerator handle for the specified ClassGuid
IntPtr theDevInfo = SetupDiGetClassDevs(ref ClassGuid, (DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE));

SP_DEVICE_INTERFACE_DATA DevInterfaceData = new SP_DEVICE_INTERFACE_DATA();
DevInterfaceData.cbSize = Marshal.SizeOf(DevInterfaceData);

initialized = SetupDiEnumDeviceInterfaces(theDevInfo, IntPtr.Zero, ref ClassGuid, 0,
ref DevInterfaceData);
// I assume The DevInterfaceData is populated correctly as it matches the C Code
// And I've compared the values in memory and they match

uint bytesReturned = 0;
initialized = SetupDiGetDeviceInterfaceDetail(theDevInfo, ref DevInterfaceData, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);
// I expect bytesReturned = 83 and initialized = true which is the value that is returned in the C Code
// Instead the value 162 is returned

最佳答案

恭喜,它正在工作。您将得到一个 Unicode 字符串,它的长度是原来的两倍。 FALSE 返回是正确的。您只需调用 Marshal.GetLastWin32Error() 并验证您是否收到 ERROR_INSUFFICIENT_BUFFER。您的 C 代码已损坏,可能是因为您忘记初始化 theBytesReturned 为零。

关于c# - Win32Api USB SetupDiGetDeviceInterfaceDetail 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9245595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com