C#对硬件的控制

C#对硬件的控制。

最近在做项目的过程中,涉及到对硬件启用及禁用的功能(在指定时间段内才能启用硬件)。因为C#自身对硬件的操作基本为零,为此,搜索了一下MSDN中与设备相关的API函数。还果然就有。下面一一列举出各函数的原型及相关的说明文档。并将其转为C#代码实现。 

API相关。函数原型具体可参考:http://blog.csdn.net/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);

        
/// <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>
        
/// <param name="devInfoData"></param>
        
/// <returns></returns>

        [DllImport("setupapi.dll", SetLastError = true)]
        
public static extern bool SetupDiEnumDeviceInfo(IntPtr lpInfoSet, UInt32 dwIndex, SP_DEVINFO_DATA devInfoData);

        
/// <summary>
        
/// 获取指定设备的属性
         
/// </summary>
        
/// <param name="lpInfoSet"></param>
        
/// <param name="DeviceInfoData"></param>
        
/// <param name="Property"></param>
        
/// <param name="PropertyRegDataType"></param>
        
/// <param name="PropertyBuffer"></param>
        
/// <param name="PropertyBufferSize"></param>
        
/// <param name="RequiredSize"></param>
        
/// <returns></returns>

        [DllImport("setupapi.dll", SetLastError = true)]
        
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr lpInfoSet, SP_DEVINFO_DATA DeviceInfoData, UInt32 Property, UInt32 PropertyRegDataType, StringBuilder PropertyBuffer, UInt32 PropertyBufferSize, IntPtr RequiredSize);

        
/// <summary>
        
/// 停用设备
         
/// </summary>
        
/// <param name="DeviceInfoSet"></param>
        
/// <param name="DeviceInfoData"></param>
        
/// <param name="ClassInstallParams"></param>
        
/// <param name="ClassInstallParamsSize"></param>
        
/// <returns></returns>

        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        
public static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, IntPtr ClassInstallParams, int ClassInstallParamsSize);

        
/// <summary>
        
/// 启用设备
         
/// </summary>
        
/// <param name="InstallFunction"></param>
        
/// <param name="DeviceInfoSet"></param>
        
/// <param name="DeviceInfoData"></param>
        
/// <returns></returns>

        [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
        
public static extern Boolean SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, IntPtr DeviceInfoData);

        
/// <summary>
        
/// RegisterDeviceNotification所需参数
         
/// </summary>

        [StructLayout(LayoutKind.Sequential)]
        
public struct DEV_BROADCAST_HANDLE
        
{
            
public int dbch_size;
            
public int dbch_devicetype;
            
public int dbch_reserved;
            
public IntPtr dbch_handle;
            
public IntPtr dbch_hdevnotify;
            
public Guid dbch_eventguid;
            
public long dbch_nameoffset;
            
public byte dbch_data;
            
public byte dbch_data1;
        }


        
// WM_DEVICECHANGE message
        [StructLayout(LayoutKind.Sequential)]
        
public class DEV_BROADCAST_DEVICEINTERFACE
        
{
            
public int dbcc_size;
            
public int dbcc_devicetype;
            
public int dbcc_reserved;
        }


        
/// <summary>
        
/// 设备信息数据
        
/// </summary>

        [StructLayout(LayoutKind.Sequential)]
        
public class SP_DEVINFO_DATA
        
{
            
public int cbSize;
            
public Guid classGuid;
            
public int devInst;
            
public ulong reserved;
        }
;

        
/// <summary>
        
/// 属性变更参数
        
/// </summary>

        [StructLayout(LayoutKind.Sequential)]
        
public class SP_PROPCHANGE_PARAMS
        
{
            
public SP_CLASSINSTALL_HEADER ClassInstallHeader = new SP_CLASSINSTALL_HEADER();
            
public int StateChange;
            
public int Scope;
            
public int HwProfile;
        }
;

        
/// <summary>
        
/// 设备安装
        
/// </summary>

        [StructLayout(LayoutKind.Sequential)]
        
public class SP_DEVINSTALL_PARAMS
        
{
            
public int cbSize;
            
public int Flags;
            
public int FlagsEx;
            
public IntPtr hwndParent;
            
public IntPtr InstallMsgHandler;
            
public IntPtr InstallMsgHandlerContext;
            
public IntPtr FileQueue;
            
public IntPtr ClassInstallReserved;
            
public int Reserved;
            [MarshalAs(UnmanagedType.LPTStr)]
            
public string DriverPath;
        }
;

        [StructLayout(LayoutKind.Sequential)]
        
public class SP_CLASSINSTALL_HEADER
        
{
            
public int cbSize;
            
public int InstallFunction;
        }
;
    }

}


C# code
  
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.InteropServices;

namespace  Hardware
{
    
public class HardwareClass
    
{
        属性
属性

        公共事件
公共事件
C# code
  
私有事件
    }
}
C# code
  
private   void  Form1_Load( object  sender, EventArgs e)
        
{
            
//枚举硬件列表
            string[] HardList = hc.List;
            
foreach (string s in HardList)
            
{
                listBox1.Items.Add(s);
            }

            hc.AllowNotifications(Handle, 
true);
            label1.Text 
= "计算机内的设备数量为:" + listBox1.Items.Count.ToString();
        }


        
private   void  Form1_FormClosing( object  sender, FormClosingEventArgs e)
        
{
            
//清理非托管资源
            hc.Dispose(Handle);
            hc 
= null;
        }


        
private   void  button1_Click( object  sender, EventArgs e)
        
{
            
//启用硬件
            string[] dev = new string[1];
            hc.Dispose(Handle);
            dev[
0= listBox1.SelectedItem.ToString();
            hc.SetState(dev, 
true);
            hc.Dispose(Handle);
        }


        
private   void  button2_Click( object  sender, EventArgs e)
        
{
            
//停用硬件
            string[] dev = new string[1];
            hc.Dispose(Handle);
            dev[
0= listBox1.SelectedItem.ToString();
            hc.SetState(dev, 
false);
            hc.Dispose(Handle);
        }

相关内容具体可参考:
http://blog.csdn.net/cocosoft/archive/2008/02/08/2086710.aspx
http://blog.csdn.net/cocosoft/archive/2008/02/08/2086712.aspx
http://blog.csdn.net/cocosoft/archive/2008/02/08/2086762.aspx
全部源码下载:
http://download.csdn.net/source/351286
 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论 2
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值