通过上一篇“通过API实现C#对硬件的控制(一)” 我们掌握了我们所需要的相关API知识点。为此,我们现在来构建我们自己的外部调用类:Externs。由于有上一篇的详细说明,该处不再一一说明相关的函数知识点。 下面是该类的代码: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Hardware ... { /**//// <summary> /// 下列所需函数可参考MSDN中与驱动程序相关的API函数 /// </summary> public class Externs ...{ public const int DIGCF_ALLCLASSES = (0x00000004); public const int DIGCF_PRESENT = (0x00000002); public const int INVALID_HANDLE_VALUE = -1; public const int SPDRP_DEVICEDESC = (0x00000000); public const int MAX_DEV_LEN = 1000; public const int DEVICE_NOTIFY_WINDOW_HANDLE = (0x00000000); public const int DEVICE_NOTIFY_SERVICE_HANDLE = (0x00000001); public const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = (0x00000004); public const int DBT_DEVTYP_DEVICEINTERFACE = (0x00000005); public const int DBT_DEVNODES_CHANGED = (0x0007); public const int WM_DEVICECHANGE = (0x0219); public const int DIF_PROPERTYCHANGE = (0x00000012); public const int DICS_FLAG_GLOBAL = (0x00000001); public const int DICS_FLAG_CONFIGSPECIFIC = (0x00000002); public const int DICS_ENABLE = (0x00000001); public const int DICS_DISABLE = (0x00000002); /**//// <summary> /// 注册设备或者设备类型,在指定的窗口返回相关的信息 /// </summary> /// <param name="hRecipient"></param> /// <param name="NotificationFilter"></param> /// <param name="Flags"></param> /// <returns></returns> [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, DEV_BROADCAST_DEVICEINTERFACE NotificationFilter, UInt32 Flags); /**//// <summary> /// 通过名柄,关闭指定设备的信息。 /// </summary> /// <param name="hHandle"></param> /// <returns></returns> [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern uint UnregisterDeviceNotification(IntPtr hHandle); /**//// <summary> /// 获取一个指定类别或全部类别的所有已安装设备的信息 /// </summary> /// <param name="gClass"></param> /// <param name="iEnumerator"></param> /// <param name="hParent"></param> /// <param name="nFlags"></param> /// <returns></returns> [DllImport("setupapi.dll", SetLastError = true)] public static extern IntPtr SetupDiGetClassDevs(ref Guid gClass, UInt32 iEnumerator, IntPtr hParent, UInt32 nFlags); /**//// <summary> /// 销毁一个设备信息集合,并且释放所有关联的内存 /// </summary> /// <param name="lpInfoSet"></param> /// <returns></returns> [DllImport("setupapi.dll", SetLastError = true)] public static extern int SetupDiDestroyDeviceInfoList(IntPtr lpInfoSet); /**//// <summary> /// 枚举指定设备信息集合的成员,并将数据放在SP_DEVINFO_DATA中 /// </summary> /// <param name="lpInfoSet"></param> /// <param name="dwIndex"></param>