通过API实现C#对硬件的控制(二)

来源:互联网 发布:电脑网络延迟高怎么办 编辑:程序博客网 时间:2024/05/22 04:43
 
  1. 通过上一篇“通过API实现C#对硬件的控制(一)” 我们掌握了我们所需要的相关API知识点。为此,我们现在来构建我们自己的外部调用类:Externs。由于有上一篇的详细说明,该处不再一一说明相关的函数知识点。
  2. 下面是该类的代码:
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Runtime.InteropServices;
  7. namespace Hardware
  8. ...{
  9.     /**//// <summary>
  10.     /// 下列所需函数可参考MSDN中与驱动程序相关的API函数
  11.     /// </summary>
  12.     public class Externs
  13.     ...{
  14.         public const int DIGCF_ALLCLASSES = (0x00000004);
  15.         public const int DIGCF_PRESENT = (0x00000002);
  16.         public const int INVALID_HANDLE_VALUE = -1;
  17.         public const int SPDRP_DEVICEDESC = (0x00000000);
  18.         public const int MAX_DEV_LEN = 1000;
  19.         public const int DEVICE_NOTIFY_WINDOW_HANDLE = (0x00000000);
  20.         public const int DEVICE_NOTIFY_SERVICE_HANDLE = (0x00000001);
  21.         public const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = (0x00000004);
  22.         public const int DBT_DEVTYP_DEVICEINTERFACE = (0x00000005);
  23.         public const int DBT_DEVNODES_CHANGED = (0x0007);
  24.         public const int WM_DEVICECHANGE = (0x0219);
  25.         public const int DIF_PROPERTYCHANGE = (0x00000012);
  26.         public const int DICS_FLAG_GLOBAL = (0x00000001);
  27.         public const int DICS_FLAG_CONFIGSPECIFIC = (0x00000002);
  28.         public const int DICS_ENABLE = (0x00000001);
  29.         public const int DICS_DISABLE = (0x00000002);
  30.         /**//// <summary>
  31.         /// 注册设备或者设备类型,在指定的窗口返回相关的信息
  32.         /// </summary>
  33.         /// <param name="hRecipient"></param>
  34.         /// <param name="NotificationFilter"></param>
  35.         /// <param name="Flags"></param>
  36.         /// <returns></returns>
  37.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  38.         public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, DEV_BROADCAST_DEVICEINTERFACE NotificationFilter, UInt32 Flags);
  39.         /**//// <summary>
  40.         /// 通过名柄,关闭指定设备的信息。
  41.         /// </summary>
  42.         /// <param name="hHandle"></param>
  43.         /// <returns></returns>
  44.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  45.         public static extern uint UnregisterDeviceNotification(IntPtr hHandle);
  46.         /**//// <summary>
  47.         /// 获取一个指定类别或全部类别的所有已安装设备的信息
  48.         /// </summary>
  49.         /// <param name="gClass"></param>
  50.         /// <param name="iEnumerator"></param>
  51.         /// <param name="hParent"></param>
  52.         /// <param name="nFlags"></param>
  53.         /// <returns></returns>
  54.         [DllImport("setupapi.dll", SetLastError = true)]
  55.         public static extern IntPtr SetupDiGetClassDevs(ref Guid gClass, UInt32 iEnumerator, IntPtr hParent, UInt32 nFlags);
  56.         /**//// <summary>
  57.         /// 销毁一个设备信息集合,并且释放所有关联的内存
  58.         /// </summary>
  59.         /// <param name="lpInfoSet"></param>
  60.         /// <returns></returns>
  61.         [DllImport("setupapi.dll", SetLastError = true)]
  62.         public static extern int SetupDiDestroyDeviceInfoList(IntPtr lpInfoSet);
  63.         /**//// <summary>
  64.         /// 枚举指定设备信息集合的成员,并将数据放在SP_DEVINFO_DATA中
  65.         /// </summary>
  66.         /// <param name="lpInfoSet"></param>
  67.         /// <param name="dwIndex"></param>
  68.         /// <param name="devInfoData"></param>
  69.         /// <returns></returns>
  70.         [DllImport("setupapi.dll", SetLastError = true)]
  71.         public static extern bool SetupDiEnumDeviceInfo(IntPtr lpInfoSet, UInt32 dwIndex, SP_DEVINFO_DATA devInfoData);
  72.         /**//// <summary>
  73.         /// 获取指定设备的属性
  74.         /// </summary>
  75.         /// <param name="lpInfoSet"></param>
  76.         /// <param name="DeviceInfoData"></param>
  77.         /// <param name="Property"></param>
  78.         /// <param name="PropertyRegDataType"></param>
  79.         /// <param name="PropertyBuffer"></param>
  80.         /// <param name="PropertyBufferSize"></param>
  81.         /// <param name="RequiredSize"></param>
  82.         /// <returns></returns>
  83.         [DllImport("setupapi.dll", SetLastError = true)]
  84.         public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr lpInfoSet, SP_DEVINFO_DATA DeviceInfoData, UInt32 Property, UInt32 PropertyRegDataType, StringBuilder PropertyBuffer, UInt32 PropertyBufferSize, IntPtr RequiredSize);
  85.         /**//// <summary>
  86.         /// 停用设备
  87.         /// </summary>
  88.         /// <param name="DeviceInfoSet"></param>
  89.         /// <param name="DeviceInfoData"></param>
  90.         /// <param name="ClassInstallParams"></param>
  91.         /// <param name="ClassInstallParamsSize"></param>
  92.         /// <returns></returns>
  93.         [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
  94.         public static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, IntPtr ClassInstallParams, int ClassInstallParamsSize);
  95.         /**//// <summary>
  96.         /// 启用设备
  97.         /// </summary>
  98.         /// <param name="InstallFunction"></param>
  99.         /// <param name="DeviceInfoSet"></param>
  100.         /// <param name="DeviceInfoData"></param>
  101.         /// <returns></returns>
  102.         [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
  103.         public static extern Boolean SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, IntPtr DeviceInfoData);
  104.         /**//// <summary>
  105.         /// RegisterDeviceNotification所需参数
  106.         /// </summary>
  107.         [StructLayout(LayoutKind.Sequential)]
  108.         public struct DEV_BROADCAST_HANDLE
  109.         ...{
  110.             public int dbch_size;
  111.             public int dbch_devicetype;
  112.             public int dbch_reserved;
  113.             public IntPtr dbch_handle;
  114.             public IntPtr dbch_hdevnotify;
  115.             public Guid dbch_eventguid;
  116.             public long dbch_nameoffset;
  117.             public byte dbch_data;
  118.             public byte dbch_data1;
  119.         }
  120.         // WM_DEVICECHANGE message
  121.         [StructLayout(LayoutKind.Sequential)]
  122.         public class DEV_BROADCAST_DEVICEINTERFACE
  123.         ...{
  124.             public int dbcc_size;
  125.             public int dbcc_devicetype;
  126.             public int dbcc_reserved;
  127.         }
  128.         /**//// <summary>
  129.         /// 设备信息数据
  130.         /// </summary>
  131.         [StructLayout(LayoutKind.Sequential)]
  132.         public class SP_DEVINFO_DATA
  133.         ...{
  134.             public int cbSize;
  135.             public Guid classGuid;
  136.             public int devInst;
  137.             public ulong reserved;
  138.         };
  139.         /**//// <summary>
  140.         /// 属性变更参数
  141.         /// </summary>
  142.         [StructLayout(LayoutKind.Sequential)]
  143.         public class SP_PROPCHANGE_PARAMS
  144.         ...{
  145.             public SP_CLASSINSTALL_HEADER ClassInstallHeader = new SP_CLASSINSTALL_HEADER();
  146.             public int StateChange;
  147.             public int Scope;
  148.             public int HwProfile;
  149.         };
  150.         /**//// <summary>
  151.         /// 设备安装
  152.         /// </summary>
  153.         [StructLayout(LayoutKind.Sequential)]
  154.         public class SP_DEVINSTALL_PARAMS
  155.         ...{
  156.             public int cbSize;
  157.             public int Flags;
  158.             public int FlagsEx;
  159.             public IntPtr hwndParent;
  160.             public IntPtr InstallMsgHandler;
  161.             public IntPtr InstallMsgHandlerContext;
  162.             public IntPtr FileQueue;
  163.             public IntPtr ClassInstallReserved;
  164.             public int Reserved;
  165.             [MarshalAs(UnmanagedType.LPTStr)]
  166.             public string DriverPath;
  167.         };
  168.         [StructLayout(LayoutKind.Sequential)]
  169.         public class SP_CLASSINSTALL_HEADER
  170.         ...{
  171.             public int cbSize;
  172.             public int InstallFunction;
  173.         };
  174.     }
  175. }
  176. 在完成了该类后,我们还需要写一个对硬件操作与获取的操作类HardwareClass。
  177. 下面为它的完整代码:
  178. using System;
  179. using System.Collections.Generic;
  180. using System.Text;
  181. using System.Runtime.InteropServices;
  182. namespace Hardware
  183. ...{
  184.     public class HardwareClass
  185.     ...{
  186.         属性#region 属性
  187.         /**//// <summary>
  188.         /// 返回所有硬件信息列表
  189.         /// </summary>
  190.         /// <returns></returns>
  191.         public string[] List
  192.         ...{
  193.             get
  194.             ...{
  195.                 List<string> HWList = new List<string>();
  196.                 try
  197.                 ...{
  198.                     Guid myGUID = System.Guid.Empty;
  199.                     IntPtr hDevInfo = Externs.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);
  200.                     if (hDevInfo.ToInt32() == Externs.INVALID_HANDLE_VALUE)
  201.                     ...{
  202.                         throw new Exception("Invalid Handle");
  203.                     }
  204.                     Externs.SP_DEVINFO_DATA DeviceInfoData;
  205.                     DeviceInfoData = new Externs.SP_DEVINFO_DATA();
  206.                     DeviceInfoData.cbSize = 28;
  207.                     DeviceInfoData.devInst = 0;
  208.                     DeviceInfoData.classGuid = System.Guid.Empty;
  209.                     DeviceInfoData.reserved = 0;
  210.                     UInt32 i;
  211.                     StringBuilder DeviceName = new StringBuilder("");
  212.                     DeviceName.Capacity = Externs.MAX_DEV_LEN;
  213.                     for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
  214.                     ...{
  215.                         while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero))
  216.                         ...{
  217.                             //Skip
  218.                         }
  219.                         HWList.Add(DeviceName.ToString());
  220.                     }
  221.                     Externs.SetupDiDestroyDeviceInfoList(hDevInfo);
  222.                 }
  223.                 catch (Exception ex)
  224.                 ...{
  225.                     throw new Exception("枚举设备列表出错", ex);
  226.                 }
  227.                 return HWList.ToArray();
  228.             }
  229.         }
  230.         #endregion
  231.         公共事件#region 公共事件
  232.         /**//// <summary>
  233.         /// 清理非托管资源
  234.         /// </summary>
  235.         /// <param name="callback"></param>
  236.         public void Dispose(IntPtr callback)
  237.         ...{
  238.             try
  239.             ...{
  240.                 Externs.UnregisterDeviceNotification(callback);
  241.             }
  242.             catch
  243.             ...{
  244.             }
  245.         }
  246.         /**//// <summary>
  247.         /// 设置指定设备的状态
  248.         /// </summary>
  249.         /// <param name="match">设备名称</param>
  250.         /// <param name="bEnable">是否启用</param>
  251.         /// <returns></returns>
  252.         public bool SetState(string[] match, bool bEnable)
  253.         ...{
  254.             try
  255.             ...{
  256.                 Guid myGUID = System.Guid.Empty;
  257.                 IntPtr hDevInfo = Externs.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);
  258.                 if (hDevInfo.ToInt32() == Externs.INVALID_HANDLE_VALUE)
  259.                 ...{
  260.                     return false;
  261.                 }
  262.                 Externs.SP_DEVINFO_DATA DeviceInfoData;
  263.                 DeviceInfoData = new Externs.SP_DEVINFO_DATA();
  264.                 DeviceInfoData.cbSize = 28;
  265.                 DeviceInfoData.devInst = 0;
  266.                 DeviceInfoData.classGuid = System.Guid.Empty;
  267.                 DeviceInfoData.reserved = 0;
  268.                 UInt32 i;
  269.                 StringBuilder DeviceName = new StringBuilder("");
  270.                 DeviceName.Capacity = Externs.MAX_DEV_LEN;
  271.                 for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
  272.                 ...{
  273.                     while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero))
  274.                     ...{
  275.                     }
  276.                     bool bMatch = true;
  277.                     foreach (string search in match)
  278.                     ...{
  279.                         if (!DeviceName.ToString().ToLower().Contains(search.ToLower()))
  280.                         ...{
  281.                             bMatch = false;
  282.                             break;
  283.                         }
  284.                     }
  285.                     if (bMatch)
  286.                     ...{
  287.                         OpenClose(hDevInfo, DeviceInfoData, bEnable);
  288.                     }
  289.                 }
  290.                 Externs.SetupDiDestroyDeviceInfoList(hDevInfo);
  291.             }
  292.             catch (Exception ex)
  293.             ...{
  294.                 throw new Exception("枚举设备信息出错!", ex);
  295.                 return false;
  296.             }
  297.             return true;
  298.         }
  299.         /**//// <summary>
  300.         /// 允许一个窗口或者服务接收所有硬件的通知
  301.         /// 注:目前还没有找到一个比较好的方法来处理如果通知服务。
  302.         /// </summary>
  303.         /// <param name="callback"></param>
  304.         /// <param name="UseWindowHandle"></param>
  305.         /// <returns></returns>
  306.         public bool AllowNotifications(IntPtr callback, bool UseWindowHandle)
  307.         {
  308.             try
  309.             {
  310.                 Externs.DEV_BROADCAST_DEVICEINTERFACE dbdi = new Externs.DEV_BROADCAST_DEVICEINTERFACE();
  311.                 dbdi.dbcc_size = Marshal.SizeOf(dbdi);
  312.                 dbdi.dbcc_reserved = 0;
  313.                 dbdi.dbcc_devicetype = Externs.DBT_DEVTYP_DEVICEINTERFACE;
  314.                 if (UseWindowHandle)
  315.                     Externs.RegisterDeviceNotification(callback, dbdi, Externs.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Externs.DEVICE_NOTIFY_WINDOW_HANDLE);
  316.                 else
  317.                     Externs.RegisterDeviceNotification(callback, dbdi, Externs.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Externs.DEVICE_NOTIFY_SERVICE_HANDLE);
  318.                 return true;
  319.             }
  320.             catch (Exception ex)
  321.           {
  322.                 string err = ex.Message;
  323.                 return false;
  324.             }
  325.         }
  326.         #endregion
  327.         私有事件#region 私有事件
  328.         /**//// <summary>
  329.         /// 开启或者关闭指定的设备驱动
  330.         /// 注意:该方法目前没有检查是否需要重新启动计算机。^.^
  331.         /// </summary>
  332.         /// <param name="hDevInfo"></param>
  333.         /// <param name="devInfoData"></param>
  334.         /// <param name="bEnable"></param>
  335.         /// <returns></returns>
  336.         private bool OpenClose(IntPtr hDevInfo, Externs.SP_DEVINFO_DATA devInfoData, bool bEnable)
  337.         {
  338.             try
  339.             {
  340.                 int szOfPcp;
  341.                 IntPtr ptrToPcp;
  342.                 int szDevInfoData;
  343.                 IntPtr ptrToDevInfoData;
  344.      Externs.SP_PROPCHANGE_PARAMS SP_PROPCHANGE_PARAMS1 = new Externs.SP_PROPCHANGE_PARAMS();
  345.                 if (bEnable)
  346.                 {
  347.                     SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
  348.                     SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
  349.                     SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE;
  350.                     SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_GLOBAL;
  351.                     SP_PROPCHANGE_PARAMS1.HwProfile = 0;
  352.                     szOfPcp = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1);
  353.                     ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
  354.                     Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, ptrToPcp, true);
  355.                     szDevInfoData = Marshal.SizeOf(devInfoData);
  356.                     ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);
  357.                     if (Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_PROPCHANGE_PARAMS))))
  358.                     {
  359.                         Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
  360.                     }
  361.                     SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
  362.                     SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
  363.                     SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE;
  364.                     SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_CONFIGSPECIFIC;
  365.                     SP_PROPCHANGE_PARAMS1.HwProfile = 0;
  366.                 }
  367.                 else
  368.                 ...{
  369.                     SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
  370.                     SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
  371.                     SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_DISABLE;
  372.                     SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_CONFIGSPECIFIC;
  373.                     SP_PROPCHANGE_PARAMS1.HwProfile = 0;
  374.                 }
  375.                 szOfPcp = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1);
  376.                 ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
  377.                 Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, ptrToPcp, true);
  378.                 szDevInfoData = Marshal.SizeOf(devInfoData);
  379.                 ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);
  380.                 Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true);
  381.                 bool rslt1 = Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_PROPCHANGE_PARAMS)));
  382.                 bool rstl2 = Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
  383.                 if ((!rslt1) || (!rstl2))
  384.                 
  385.                     throw new Exception("不能更改设备状态。");
  386.                     return false;
  387.                 }
  388.                 else
  389.                 {
  390.                     return true;
  391.                 }
  392.             }
  393.             catch (Exception ex)
  394.             {
  395.                 return false;
  396.             }
  397.         }
  398.         #endregion
  399.     }
  400. }
  401. 文章出处:http://www.diybl.com/course/4_webprogram/asp.net/asp_netxl/2008215/99921.html
原创粉丝点击