C#对硬件的控制

简介:

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
{
属性 #region 属性
/// <summary>
/// 返回所有硬件信息列表
/// </summary>
/// <returns></returns>
public string[] List
{
get
{
List< string> HWList = new List< string>();
try
{
Guid myGUID = System.Guid.Empty;
IntPtr hDevInfo = Externs.SetupDiGetClassDevs( ref myGUID, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);
if (hDevInfo.ToInt32() == Externs.INVALID_HANDLE_VALUE)
{
throw new Exception( " Invalid Handle ");
}
Externs.SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData = new Externs.SP_DEVINFO_DATA();
DeviceInfoData.cbSize = 28;
DeviceInfoData.devInst = 0;
DeviceInfoData.classGuid = System.Guid.Empty;
DeviceInfoData.reserved = 0;
UInt32 i;
StringBuilder DeviceName = new StringBuilder( "");
DeviceName.Capacity = Externs.MAX_DEV_LEN;
for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
{
while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero))
{
// Skip
}
HWList.Add(DeviceName.ToString());
}
Externs.SetupDiDestroyDeviceInfoList(hDevInfo);
}
catch (Exception ex)
...{
throw new Exception( " 枚举设备列表出错 ", ex);
}
return HWList.ToArray();
}
}

#endregion

公共事件 #region 公共事件
/// <summary>
/// 清理非托管资源
/// </summary>
/// <param name="callback"></param>
public void Dispose(IntPtr callback)
{
try
{
Externs.UnregisterDeviceNotification(callback);
}
catch
{
}
}

/// <summary>
/// 设置指定设备的状态
/// </summary>
/// <param name="match"> 设备名称 </param>
/// <param name="bEnable"> 是否启用 </param>
/// <returns></returns>
public bool SetState( string[] match, bool bEnable)
{
try
{
Guid myGUID = System.Guid.Empty;
IntPtr hDevInfo = Externs.SetupDiGetClassDevs( ref myGUID, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);
if (hDevInfo.ToInt32() == Externs.INVALID_HANDLE_VALUE)
{
return false;
}
Externs.SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData = new Externs.SP_DEVINFO_DATA();
DeviceInfoData.cbSize = 28;
DeviceInfoData.devInst = 0;
DeviceInfoData.classGuid = System.Guid.Empty;
DeviceInfoData.reserved = 0;
UInt32 i;
StringBuilder DeviceName = new StringBuilder( "");
DeviceName.Capacity = Externs.MAX_DEV_LEN;
for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
{
while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero))
{
}
bool bMatch = true;
foreach ( string search in match)
{
if (!DeviceName.ToString().ToLower().Contains(search.ToLower()))
{
bMatch = false;
break;
}
}
if (bMatch)
{
OpenClose(hDevInfo, DeviceInfoData, bEnable);
}
}
Externs.SetupDiDestroyDeviceInfoList(hDevInfo);
}
catch (Exception ex)
{
throw new Exception( " 枚举设备信息出错! ", ex);
return false;
}
return true;
}

/// <summary>
/// 允许一个窗口或者服务接收所有硬件的通知
/// 注:目前还没有找到一个比较好的方法来处理如果通知服务。
/// </summary>
/// <param name="callback"></param>
/// <param name="UseWindowHandle"></param>
/// <returns></returns>
public bool AllowNotifications(IntPtr callback, bool UseWindowHandle)
{
try
{
Externs.DEV_BROADCAST_DEVICEINTERFACE dbdi = new Externs.DEV_BROADCAST_DEVICEINTERFACE();
dbdi.dbcc_size = Marshal.SizeOf(dbdi);
dbdi.dbcc_reserved = 0;
dbdi.dbcc_devicetype = Externs.DBT_DEVTYP_DEVICEINTERFACE;
if (UseWindowHandle)
Externs.RegisterDeviceNotification(callback, dbdi, Externs.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Externs.DEVICE_NOTIFY_WINDOW_HANDLE);
else
Externs.RegisterDeviceNotification(callback, dbdi, Externs.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Externs.DEVICE_NOTIFY_SERVICE_HANDLE);
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}

#endregion


C# code
 
        

#region 私有事件
/// <summary>
/// 开启或者关闭指定的设备驱动
/// 注意:该方法目前没有检查是否需要重新启动计算机。^.^
/// </summary>
/// <param name="hDevInfo"></param>
/// <param name="devInfoData"></param>
/// <param name="bEnable"></param>
/// <returns></returns>
private bool OpenClose(IntPtr hDevInfo, Externs.SP_DEVINFO_DATA devInfoData, bool bEnable)
{
try
{
int szOfPcp;
IntPtr ptrToPcp;
int szDevInfoData;
IntPtr ptrToDevInfoData;
Externs.SP_PROPCHANGE_PARAMS SP_PROPCHANGE_PARAMS1 = new Externs.SP_PROPCHANGE_PARAMS();
if (bEnable)
{
SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf( typeof(Externs.SP_CLASSINSTALL_HEADER));
SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE;
SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_GLOBAL;
SP_PROPCHANGE_PARAMS1.HwProfile = 0;

szOfPcp = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1);
ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, ptrToPcp, true);
szDevInfoData = Marshal.SizeOf(devInfoData);
ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);

if (Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf( typeof(Externs.SP_PROPCHANGE_PARAMS))))
{
Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
}
SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf( typeof(Externs.SP_CLASSINSTALL_HEADER));
SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE;
SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_CONFIGSPECIFIC;
SP_PROPCHANGE_PARAMS1.HwProfile = 0;
}
else
{
SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf( typeof(Externs.SP_CLASSINSTALL_HEADER));
SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_DISABLE;
SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_CONFIGSPECIFIC;
SP_PROPCHANGE_PARAMS1.HwProfile = 0;
}
szOfPcp = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1);
ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, ptrToPcp, true);
szDevInfoData = Marshal.SizeOf(devInfoData);
ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);
Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true);

bool rslt1 = Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf( typeof(Externs.SP_PROPCHANGE_PARAMS)));
bool rstl2 = Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
if ((!rslt1) || (!rstl2))
{
throw new Exception( " 不能更改设备状态。 ");
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
return false;
}
}

#endregion
}
}


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);
        }本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/08/14/1545717.html,如需转载请自行联系原作者
 

相关文章
|
网络协议 API C#
C#使用访问WMI的接口获取计算机硬件和操作系统信息,WMI代码生成器介绍【ManagementObjectSearcher、ManagementClass】
ManagementObjectSearcher 用于获取基于指定查询的管理对象集合。是获取管理信息最常用的入口点。例如,可以遍历所有的硬盘驱动、网络适配器、进程和系统上的其他管理对象,或者...
1105 0
C#使用访问WMI的接口获取计算机硬件和操作系统信息,WMI代码生成器介绍【ManagementObjectSearcher、ManagementClass】
|
C#
C#枚举硬件设备(升级版)
原文:C#枚举硬件设备(升级版) 先取设备类型: /// /// 设备类型/// class DeviceClasses {public static Guid ClassesGuid;public const int MAX_SIZE_DEVICE_DESCRIPTION = 100...
1248 0
|
6月前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
188 3
|
6月前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
196 3
|
6天前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
18 3
|
27天前
|
安全 C# 数据安全/隐私保护
实现C#编程文件夹加锁保护
【10月更文挑战第16天】本文介绍了两种用 C# 实现文件夹保护的方法:一是通过设置文件系统权限,阻止普通用户访问;二是使用加密技术,对文件夹中的文件进行加密,防止未授权访问。提供了示例代码和使用方法,适用于不同安全需求的场景。
|
2月前
|
API C#
C# 一分钟浅谈:文件系统编程
在软件开发中,文件系统操作至关重要。本文将带你快速掌握C#中文件系统编程的基础知识,涵盖基本概念、常见问题及解决方法。文章详细介绍了`System.IO`命名空间下的关键类库,并通过示例代码展示了路径处理、异常处理、并发访问等技巧,还提供了异步API和流压缩等高级技巧,帮助你写出更健壮的代码。
39 2
|
2月前
|
安全 程序员 编译器
C#一分钟浅谈:泛型编程基础
在现代软件开发中,泛型编程是一项关键技能,它使开发者能够编写类型安全且可重用的代码。C# 自 2.0 版本起支持泛型编程,本文将从基础概念入手,逐步深入探讨 C# 中的泛型,并通过具体实例帮助理解常见问题及其解决方法。泛型通过类型参数替代具体类型,提高了代码复用性和类型安全性,减少了运行时性能开销。文章详细介绍了如何定义泛型类和方法,并讨论了常见的易错点及解决方案,帮助读者更好地掌握这一技术。
70 11
|
2月前
|
SQL 开发框架 安全
并发集合与任务并行库:C#中的高效编程实践
在现代软件开发中,多核处理器普及使多线程编程成为提升性能的关键。然而,传统同步模型在高并发下易引发死锁等问题。为此,.NET Framework引入了任务并行库(TPL)和并发集合,简化并发编程并增强代码可维护性。并发集合允许多线程安全访问,如`ConcurrentQueue&lt;T&gt;`和`ConcurrentDictionary&lt;TKey, TValue&gt;`,有效避免数据不一致。TPL则通过`Task`类实现异步操作,提高开发效率。正确使用这些工具可显著提升程序性能,但也需注意任务取消和异常处理等常见问题。
46 1