-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetupApiDll.cs
89 lines (78 loc) · 3.9 KB
/
SetupApiDll.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SharpHID
{
internal static class SetupApiDll
{
[StructLayout(LayoutKind.Sequential)]
internal struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid interfaceClassGuid;
public int flags;
private UIntPtr reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
internal struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
public char devicePath;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public IntPtr Reserved;
}
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] // 2nd form uses an Enumerator only, with null ClassGUID
internal static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, int Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, uint memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr hDevInfo,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
IntPtr deviceInterfaceDetailData,
int deviceInterfaceDetailDataSize,
ref uint requiredSize,
ref SP_DEVINFO_DATA deviceInfoData);
internal static bool SetupDiGetDeviceInterfaceDetail(
IntPtr hDevInfo,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
out string devicePath,
ref SP_DEVINFO_DATA deviceInfoData)
{
bool gotDetails = false;
devicePath = "";
IntPtr detailData = IntPtr.Zero;
UInt32 detailSize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo, ref deviceInterfaceData, detailData, 0, ref detailSize, ref deviceInfoData);
if (detailSize > 0)
{
int structSize = Marshal.SystemDefaultCharSize;
if (IntPtr.Size == 8)
structSize += 6;
else
structSize += 4;
detailData = Marshal.AllocHGlobal((int)detailSize + structSize);
Marshal.WriteInt32(detailData, (int)structSize);
bool success = SetupDiGetDeviceInterfaceDetail(hDevInfo, ref deviceInterfaceData, detailData, (int)detailSize, ref detailSize, ref deviceInfoData);
if (success)
{
devicePath = Marshal.PtrToStringUni(new IntPtr(detailData.ToInt64() + 4)).ToLower();
gotDetails = true;
}
Marshal.FreeHGlobal(detailData);
}
return gotDetails;
}
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);
}
}