I'm trying to find the vendor id and the product id of a USB card. For this purpose, I use setupapi.dll. When I closed my program I have 0x80131623 (2146232797) appears. I don't know how I can fix it.
My code is :
result = HidD_GetHidGuid(ref HidGuid);
DeviceInfoSet = SetupDiGetClassDevs(ref HidGuid, IntPtr.Zero, IntPtr.Zero, 18);
do
{
SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
MyDeviceInterfaceData.cbSize = 28;
result = SetupDiEnumDeviceInterfaces(DeviceInfoSet, IntPtr.Zero, ref HidGuid, MemberIndex, ref MyDeviceInterfaceData);
//int lastError = Marshal.GetLastWin32Error();
if (result != 0)
{
//SP_DEVINFO_DATA MyDeviceInfoData = new SP_DEVINFO_DATA();
//MyDeviceInfoData.cbSize = Marshal.SizeOf(MyDeviceInfoData);
//Premier appel pour obtenir la taille du buffer
result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData, IntPtr.Zero, 0, out neededSize, IntPtr.Zero);
//int lastError = Marshal.GetLastWin32Error();
int requiredSize = (int)neededSize;
SP_DEVICE_INTERFACE_DETAIL_DATA MyDeviceInterfaceDetailData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
if (IntPtr.Size == 8) // for 64 bit operating systems
MyDeviceInterfaceDetailData.cbSize = 8;
else
MyDeviceInterfaceDetailData.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems
IntPtr detailDataBuffer = Marshal.AllocHGlobal(requiredSize);
Marshal.StructureToPtr(MyDeviceInterfaceDetailData, detailDataBuffer, true);
result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData, detailDataBuffer, requiredSize, out neededSize, IntPtr.Zero);
int lastError = Marshal.GetLastWin32Error();
//string instanceID = Marshal.PtrToStringAuto(detailDataBuffer);
Marshal.PtrToStructure(detailDataBuffer, MyDeviceInterfaceDetailData);
if (MyDeviceInterfaceDetailData.DevicePath.IndexOf("vid_04fa&pid_9123") != -1)
{
Marshal.FreeHGlobal(detailDataBuffer);
Marshal.FreeHGlobal(DeviceInfoSet);
return true;
}
MemberIndex++;
Marshal.FreeHGlobal(detailDataBuffer);
}
else
{
//int lastError = Marshal.GetLastWin32Error();
}
if (MemberIndex>=16) { lastDevice = true; }
} while (lastDevice == false);
Marshal.FreeHGlobal(DeviceInfoSet);
return false;
}
I think the problem is caused by Marshaling.
User contributions licensed under CC BY-SA 3.0