private static string GetDevicePath(IntPtr DeviceInfoTable, ref SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DATA InterfaceDataStructure) { int RequiredSize = 0; SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData = new SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA(); interfaceDetailData.cbSize = Marshal.SizeOf(interfaceDetailData); SemtechLib.Devices.Common.NativeMethods.SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref InterfaceDataStructure, IntPtr.Zero, 0, ref RequiredSize, IntPtr.Zero); IntPtr hDevice = Marshal.AllocHGlobal(RequiredSize); interfaceDetailData.cbSize = IntPtr.Size != 8 ? 4 + Marshal.SystemDefaultCharSize : 8; Marshal.StructureToPtr(interfaceDetailData, hDevice, false); if (SemtechLib.Devices.Common.NativeMethods.SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref InterfaceDataStructure, hDevice, RequiredSize, IntPtr.Zero, IntPtr.Zero)) { string devicePath = Marshal.PtrToStringUni(new IntPtr(hDevice.ToInt32() + 4)); Marshal.FreeHGlobal(hDevice); return(devicePath); } Marshal.FreeHGlobal(hDevice); return(string.Empty); }
private static string GetDevicePath(IntPtr DeviceInfoTable, ref SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DATA InterfaceDataStructure) { int RequiredSize = 0; SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData = new SemtechLib.Devices.Common.NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA(); interfaceDetailData.cbSize = Marshal.SizeOf(interfaceDetailData); SemtechLib.Devices.Common.NativeMethods.SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref InterfaceDataStructure, IntPtr.Zero, 0, ref RequiredSize, IntPtr.Zero); IntPtr hDevice = Marshal.AllocHGlobal(RequiredSize); interfaceDetailData.cbSize = IntPtr.Size != 8 ? 4 + Marshal.SystemDefaultCharSize : 8; Marshal.StructureToPtr(interfaceDetailData, hDevice, false); if (SemtechLib.Devices.Common.NativeMethods.SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref InterfaceDataStructure, hDevice, RequiredSize, IntPtr.Zero, IntPtr.Zero)) { string devicePath = Marshal.PtrToStringUni(new IntPtr(hDevice.ToInt32() + 4)); Marshal.FreeHGlobal(hDevice); return devicePath; } Marshal.FreeHGlobal(hDevice); return string.Empty; }