Author Topic: FindDeviceFromGuid issue with Framework 4.5  (Read 152 times)

Sarva

  • Member
  • ***
  • Posts: 2
FindDeviceFromGuid issue with Framework 4.5
« on: June 11, 2018, 11:04:11 am »
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: [Select]
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);

with the error message

{"Arithmetic operation resulted in an overflow."}

Has anyone seen this issue before?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 2737
    • Lakeview Research

Sarva

  • Member
  • ***
  • Posts: 2
Re: FindDeviceFromGuid issue with Framework 4.5
« Reply #2 on: June 12, 2018, 05:27:45 pm »
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: [Select]
IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);
if I were to code according to the logic in that asnwer, would my code still look like this

Code: [Select]
if (Environment.Is64BitProcess)
{
    IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt64() + 4);
}
else
{
    IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);
}

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


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 2737
    • Lakeview Research
Re: FindDeviceFromGuid issue with Framework 4.5
« Reply #3 on: June 13, 2018, 09:15:04 am »
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.