Ports and Interfaces > USB

FindDeviceFromGuid issue with Framework 4.5

(1/1)

Sarva:
I am having issues with the FindDeviceFromGuid(System.Guid myGuid, ref String[] devicePathName) function. I have the code part of my project, and when I compile it till .Net framework 4.0 it runs fine. However, for Framework 4.5 and higher (and only on Windows versions higher than 7), the code fails at the following section


--- Code: --- success = SetupDiGetDeviceInterfaceDetail
(deviceInfoSet,
ref MyDeviceInterfaceData,
detailDataBuffer,
bufferSize,
ref bufferSize,
IntPtr.Zero);

// Skip over cbsize (4 bytes) to get the address of the devicePathName.

IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);               <===== This is where it fails

// Get the String containing the devicePathName.

devicePathName[memberIndex] = Marshal.PtrToStringAuto(pDevicePathName);

--- End code ---

with the error message

{"Arithmetic operation resulted in an overflow."}

Has anyone seen this issue before?

Jan Axelson:
See if this helps:

https://stackoverflow.com/questions/36478303/arithmetic-operation-resulted-in-an-overflow-c-sharp

Sarva:
Jan, based on that discussion, I changed the Platform Target of my application to x86, and that does take care of the issue. So the following question is just my curiosity - 

For the following line of code


--- Code: ---IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);
--- End code ---

if I were to code according to the logic in that asnwer, would my code still look like this


--- Code: ---if (Environment.Is64BitProcess)
{
    IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt64() + 4);
}
else
{
    IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);
}
--- End code ---

Would I still be offsetting by 4 bytes in case of 64 bit OS?

Jan Axelson:
The "4" refers to the initial 4 bytes (the DWORD cbsize parameter) in the SP_DEVICE_INTERFACE_DETAIL_DATA structure so should be OK.

In other words, you're getting a pointer to the 5th byte in the structure, and the structure doesn't change with 32/64-bit platform.

Navigation

[0] Message Index

Go to full version