我一直在尝试连接 GPS 接口卡的设备驱动程序。我从供应商那里获得的示例是 C++ 中的。我正在尝试使用 pinvoke 让它在 C# 中工作
class HaveQuick
{
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
IntPtr Enumerator,
IntPtr hwndParent,
uint Flags
);
[DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiEnumDeviceInterfaces(
IntPtr hDevInfo,
ref SP_DEVINFO_DATA devInfo,
ref Guid interfaceClassGuid,
UInt32 memberIndex,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData
);
Guid GUID_DEVINTERFACE_IPHQT = new Guid(0x13ff045f, 0xe9d2, 0x479c, 0xb3, 0x90, 0x8c, 0xae, 0xc7, 0xd1, 0x1c, 0xc4);
hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_IPHQT, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDeviceInfo == IntPtr.Zero)
{
lastError = Marshal.GetLastWin32Error();
Console.WriteLine("**Error: couldn't get class info error code = {0}\n");
return;
}
// Find the interface for device
devNum = 0x0000;
SP_DEVICE_INTERFACE_DATA interfaceData = new SP_DEVICE_INTERFACE_DATA(); // Interface data for this device
interfaceData.cbSize = (uint)Marshal.SizeOf(interfaceData);
SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA();
deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData);
success = SetupDiEnumDeviceInterfaces(hDeviceInfo, ref deviceInfoData, ref GUID_DEVINTERFACE_IPHQT, devNum, ref interfaceData);
if (!success)
{
lastError = Marshal.GetLastWin32Error();
Console.WriteLine("**Error: couldn't enum device error number = {0}\n", lastError);
}
当我运行代码时,我得到 lasterror = 87 这是 Invalid Parameter
如果我用这行代码替换成功 = SetupDiEnumDeviceInterfaces(hDeviceInfo, IntPtr.Zero, ref GUID_DEVINTERFACE_IPHQT, devNum, ref interfaceData);
我得到 lasterror = 1784 Invlid 用户缓冲区。
谢谢您的帮助