函數一: RegisterDeviceNotification
功能:註冊設備或者設備類型,在指定的窗口返回相關的信息
原型:
HDEVNOTIFY WINAPI RegisterDeviceNotification(
__in HANDLE hRecipient,
__in LPVOID NotificationFilter,
__in DWORD Flags
);
參考說明文檔:http://msdn2.microsoft.com/en-us/library/aa363431.aspx。
轉爲C#後的代碼爲:
[DllImport(“user32.dll“, CharSet = CharSet.Auto)]
public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, DEV_BROADCAST_DEVICEINTERFACE NotificationFilter, UInt32 Flags);
[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_DEVICEINTERFACE
...{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
}
函數二:UnregisterDeviceNotification
功能:通過名柄,關閉指定設備的信息。(主要應用於清理非託管資源,並與RegisterDeviceNotification配對使用)
原型:
BOOL WINAPI UnregisterDeviceNotification(
__in HDEVNOTIFY Handle
);
參考說明文檔:http://msdn2.microsoft.com/en-us/library/aa363475(VS.85).aspx。
轉爲C#後的代碼:
[DllImport(“user32.dll“, CharSet = CharSet.Auto)]
public static extern uint UnregisterDeviceNotification(IntPtr hHandle);
函數三:SetupDiGetClassDevs
功能:獲取一個指定類別或全部類別的所有已安裝設備的信息。
原型:
HDEVINFO SetupDiGetClassDevs( IN LPGUID ClassGuid, OPTIONAL
IN PCTSTR Enumerator, OPTIONAL IN HWND hwndParent, OPTIONAL IN DWORD Flags );
參考說明文檔:http://msdn2.microsoft.com/en-us/library/ms792959.aspx。
轉爲C#後的代碼:
[DllImport(“setupapi.dll“, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid gClass, UInt32 iEnumerator, IntPtr hParent, UInt32 nFlags);
函數四:SetupDiDestroyDeviceInfoList
功能:銷燬一個設備信息集合,並且釋放所有關聯的內存。
原型:
WINSETUPAPI BOOL WINAPI SetupDiDestroyDeviceInfoList( IN HDEVINFO
DeviceInfoSet );
參考說明文檔:http://msdn2.microsoft.com/en-us/library/ms792991.aspx。
轉爲C#後的代碼:
[DllImport(“setupapi.dll“, SetLastError = true)]
public static extern int SetupDiDestroyDeviceInfoList(IntPtr lpInfoSet);
函數五:SetupDiEnumDeviceInfo
功能:枚舉指定設備信息集合的成員,並將數據放在SP_DEVINFO_DATA中。
原型:
WINSETUPAPI BOOL WINAPI SetupDiEnumDeviceInfo(
IN HDEVINFO DeviceInfoSet,
IN DWORD MemberIndex,
OUT PSP_DEVINFO_DATA DeviceInfoData );
參考說明文檔:http://msdn2.microsoft.com/en-us/library/ms792983.aspx。
轉爲C#後的代碼:
[DllImport(“setupapi.dll“, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr lpInfoSet, UInt32 dwIndex, SP_DEVINFO_DATA devInfoData);
/**//// 〈summary〉
/// 設備信息數據
/// 〈/summary〉
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
...{
public int cbSize;
public Guid classGuid;
public int devInst;
public ulong reserved;
};
函數六:SetupDiGetDeviceRegistryProperty
功能:獲取指定設備的屬性。
原型:
WINSETUPAPI BOOL WINAPI SetupDiGetDeviceRegistryProperty(
IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData,
IN DWORD Property,
OUT PDWORD PropertyRegDataType,
OPTIONAL OUT PBYTE PropertyBuffer,
IN DWORD PropertyBufferSize,
OUT PDWORD RequiredSize OPTIONAL );
參考說明文檔:http://msdn2.microsoft.com/en-us/library/ms792967.aspx。
轉爲C#後的代碼:
[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);
函數七:SetupDiSetClassInstallParams
功能:停用設備。
原型:
WINSETUPAPI BOOL WINAPI
SetupDiSetClassInstallParams(
IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData,
OPTIONAL IN PSP_CLASSINSTALL_HEADER ClassInstallParams,
OPTIONAL IN DWORD ClassInstallParamsSize );
參考說明文檔:http://msdn2.microsoft.com/en-us/library/ms792876.aspx。
轉爲C#後的代碼:
[DllImport(“setupapi.dll“, SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, IntPtr ClassInstallParams, int ClassInstallParamsSize);
函數八:SetupDiCallClassInstaller
功能:啓用設備。
原型:
WINSETUPAPI BOOL WINAPI
SetupDiCallClassInstaller(
IN DI_FUNCTION InstallFunction,
IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL );
參考說明文檔:http://msdn2.microsoft.com/en-us/library/ms792989.aspx。
轉爲C#後的代碼:
[DllImport(“setupapi.dll“, CharSet = CharSet.Auto)]
public static extern Boolean SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, IntPtr DeviceInfoData);
我們掌握了我們所需要的相關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>
/// <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;
};
}
}
在完成了該類後,我們還需要寫一個對硬件操作與獲取的操作類HardwareClass。
下面爲它的完整代碼:
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
私有事件#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
}
}
我們在這裏面建立一個測試項目。簡單的設備管理器:)
界面如下圖:
源碼項目下載地址:http://download.csdn.net/source/352079。