215 lines
9.7 KiB
C#
215 lines
9.7 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace DTS.Common.USBFramework
|
|
{
|
|
public class DeviceManagementDeclarations
|
|
{
|
|
// API declarations relating to device management (SetupDixxx and
|
|
// RegisterDeviceNotification functions).
|
|
|
|
// ******************************************************************************
|
|
// API constants
|
|
// ******************************************************************************
|
|
|
|
// from dbt.h
|
|
public const int DBT_DEVICEARRIVAL = 0x8000;
|
|
public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
|
|
public const int DBT_DEVTYP_DEVICEINTERFACE = 5;
|
|
public const int DBT_DEVTYP_HANDLE = 6;
|
|
public const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = 4;
|
|
public const int DEVICE_NOTIFY_SERVICE_HANDLE = 1;
|
|
public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0;
|
|
public const int WM_DEVICECHANGE = 0x219;
|
|
public const int ERROR_INSUFFICIENT_BUFFER = 122;
|
|
public const int NO_ERROR = 0;
|
|
public const int ERROR_NO_MORE_ITEMS = 259;
|
|
public const int DICS_FLAG_GLOBAL = 1;
|
|
public const int DICS_FLAG_CONFIGSPECIFIC = 2;
|
|
public const int DICS_FLAG_CONFIGGENERAL = 4;
|
|
public const int DIREG_DEV = 1;
|
|
public const int DIREG_DRV = 2;
|
|
public const int DIREG_BOTH = 4;
|
|
public const int SPDRP_FRIENDLYNAME = (0x0000000C);
|
|
public const int SPDRP_DEVICEDESC = (0x00000000);
|
|
public const int SPDRP_DRIVER = (0x00000009);
|
|
|
|
// from setupapi.h
|
|
public const int DIGCF_PRESENT = 0x00000002;
|
|
public const int DIGCF_DEVICEINTERFACE = 0x00000010;
|
|
public const int DIGCF_ALLCLASSES = 0x00000004;
|
|
|
|
// ******************************************************************************
|
|
// Structures and classes for API calls, listed alphabetically
|
|
// ******************************************************************************
|
|
|
|
// There are two declarations for the DEV_BROADCAST_DEVICEINTERFACE structure.
|
|
|
|
// Use this in the call to RegisterDeviceNotification() and
|
|
// in checking dbch_devicetype in a DEV_BROADCAST_HDR structure.
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
public struct DEV_BROADCAST_DEVICEINTERFACE
|
|
{
|
|
public int dbcc_size;
|
|
public int dbcc_devicetype;
|
|
public int dbcc_reserved;
|
|
public Guid dbcc_classguid;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 400)]
|
|
public string dbcc_name;
|
|
}
|
|
|
|
// Use this to read the dbcc_name string and classguid.
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
public class DEV_BROADCAST_DEVICEINTERFACE_1
|
|
{
|
|
public int dbcc_size;
|
|
public int dbcc_devicetype;
|
|
public int dbcc_reserved;
|
|
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 16)]
|
|
public byte[] dbcc_classguid;
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
|
|
public char[] dbcc_name;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public class DEV_BROADCAST_HANDLE
|
|
{
|
|
public int dbch_size;
|
|
public int dbch_devicetype;
|
|
public int dbch_reserved;
|
|
public int dbch_handle;
|
|
public int dbch_hdevnotify;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public class DEV_BROADCAST_HDR
|
|
{
|
|
public int dbch_size;
|
|
public int dbch_devicetype;
|
|
public int dbch_reserved;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct SP_DEVICE_INTERFACE_DATA
|
|
{
|
|
public int cbSize;
|
|
public System.Guid InterfaceClassGuid;
|
|
public int Flags;
|
|
public IntPtr Reserved;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
|
|
{
|
|
public int cbSize;
|
|
public string DevicePath;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct SP_DEVINFO_DATA
|
|
{
|
|
public int cbSize;
|
|
public System.Guid ClassGuid;
|
|
public int DevInst;
|
|
public int Reserved;
|
|
}
|
|
|
|
[Flags]
|
|
public enum RegSAM
|
|
{
|
|
QueryValue = 0x0001,
|
|
SetValue = 0x0002,
|
|
CreateSubKey = 0x0004,
|
|
EnumerateSubKeys = 0x0008,
|
|
Notify = 0x0010,
|
|
CreateLink = 0x0020,
|
|
WOW64_32Key = 0x0200,
|
|
WOW64_64Key = 0x0100,
|
|
WOW64_Res = 0x0300,
|
|
Read = 0x00020019,
|
|
Write = 0x00020006,
|
|
Execute = 0x00020019,
|
|
AllAccess = 0x000f003f
|
|
}
|
|
|
|
// ******************************************************************************
|
|
// API functions, listed alphabetically
|
|
// ******************************************************************************
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient,
|
|
IntPtr NotificationFilter,
|
|
int Flags);
|
|
|
|
[DllImport("setupapi.dll")]
|
|
public static extern int SetupDiCreateDeviceInfoList(ref System.Guid ClassGuid,
|
|
int hwndParent);
|
|
|
|
[DllImport("setupapi.dll")]
|
|
public static extern int SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
|
|
|
|
[DllImport("setupapi.dll")]
|
|
public static extern int SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet,
|
|
IntPtr DeviceInfoData,
|
|
ref System.Guid InterfaceClassGuid,
|
|
int MemberIndex,
|
|
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
|
|
|
|
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr SetupDiGetClassDevs(ref System.Guid ClassGuid,
|
|
string Enumerator,
|
|
int hwndParent,
|
|
uint Flags);
|
|
|
|
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
|
|
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet,
|
|
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
|
|
IntPtr DeviceInterfaceDetailData,
|
|
int DeviceInterfaceDetailDataSize,
|
|
ref int RequiredSize,
|
|
IntPtr DeviceInfoData);
|
|
|
|
[DllImport("setupapi.dll", SetLastError = true)]
|
|
public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, uint MemberIndex, out SP_DEVINFO_DATA DeviceInfoData);
|
|
|
|
[DllImport("setupapi.dll")] //result HDEVINFO
|
|
public static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid,
|
|
uint Enumerator,
|
|
IntPtr hwndParent,
|
|
uint Flags);
|
|
|
|
[DllImport("setupapi.dll")]
|
|
public static extern bool SetupDiGetDeviceRegistryPropertyA(IntPtr DeviceInfoSet,
|
|
SP_DEVINFO_DATA DeviceInfoData,
|
|
uint Property,
|
|
uint PropertyRegDataType,
|
|
StringBuilder PropertyBuffer,
|
|
uint PropertyBufferSize,
|
|
IntPtr RequiredSize);
|
|
|
|
// http://msdn.microsoft.com/en-us/library/ms791236.aspx
|
|
[DllImport("setupapi.dll", SetLastError = true)]
|
|
public static extern int SetupDiOpenDevRegKey(IntPtr DeviceInfoSet,
|
|
ref SP_DEVINFO_DATA DeviceInfoData,
|
|
uint Scope,
|
|
uint HwProfile,
|
|
uint KeyType,
|
|
RegSAM samDesired);
|
|
|
|
[DllImport("Advapi32.dll")]
|
|
public static extern uint RegQueryValueEx(int hKey,
|
|
string lpValueName,
|
|
uint lpReserved, // must be 0
|
|
out uint lpType,
|
|
ref byte[] lpData,
|
|
ref uint lpcbData);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool UnregisterDeviceNotification(IntPtr Handle);
|
|
|
|
[DllImport("kernel32.dll")]
|
|
public static extern int GetLastError();
|
|
}
|
|
}
|