C#对硬件的控制。

最近在做项目的过程中,涉及到对硬件启用及禁用的功能(在指定时间段内才能启用硬件)。因为C#自身对硬件的操作基本为零,为此,搜索了一下MSDN中与设备相关的API函数。还果然就有。下面一一列举出各函数的原型及相关的说明文档。并将其转为C#代码实现。
API相关。函数原型具体可参考: http://blog.youkuaiyun.com/cocosoft/archive/2008/02/08/2086710.aspx
-
C# code
-
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);
///