C#对硬件的控制.docx

上传人:b****6 文档编号:8621106 上传时间:2023-02-01 格式:DOCX 页数:15 大小:109.05KB
下载 相关 举报
C#对硬件的控制.docx_第1页
第1页 / 共15页
C#对硬件的控制.docx_第2页
第2页 / 共15页
C#对硬件的控制.docx_第3页
第3页 / 共15页
C#对硬件的控制.docx_第4页
第4页 / 共15页
C#对硬件的控制.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

C#对硬件的控制.docx

《C#对硬件的控制.docx》由会员分享,可在线阅读,更多相关《C#对硬件的控制.docx(15页珍藏版)》请在冰豆网上搜索。

C#对硬件的控制.docx

C#对硬件的控制

C#对硬件的控制。

最近在做项目的过程中,涉及到对硬件启用及禁用的功能(在指定时间段内才能启用硬件)。

因为C#自身对硬件的操作基本为零,为此,搜索了一下MSDN中与设备相关的API函数。

还果然就有。

下面一一列举出各函数的原型及相关的说明文档。

并将其转为C#代码实现。

API相关。

函数原型具体可参考:

C#code

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Runtime.InteropServices;

namespaceHardware{

///

/

//下列所需函数可参考MSDN中与驱动程序相关的API函数

///

publicclassExterns{

publicconstintDIGCF_ALLCLASSES=(0x00000004);

publicconstintDIGCF_PRESENT=(0x00000002);

publicconstintINVALID_HANDLE_VALUE=-1;

publicconstintSPDRP_DEVICEDESC=(0x00000000);

publicconstintMAX_DEV_LEN=1000;

publicconstintDEVICE_NOTIFY_WINDOW_HANDLE=(0x00000000);publicconstintDEVICE_NOTIFY_SERVICE_HANDLE=(0x000000010,0);">);

publicconstintDEVICE_NOTIFY_ALL_INTERFACE_CLASSES=(0x00000004);

publicconstintDBT_DEVTYP_DEVICEINTERFACE=(0x00000005);

publicconstintDBT_DEVNODES_CHANGED=(0x0007);

publicconstintWM_DEVICECHANGE=(0x0219);

publicconstintDIF_PROPERTYCHANGE=(0x00000012);

publicconstintDICS_FLAG_GLOBAL=(0x00000001);

publicconstintDICS_FLAG_CONFIGSPECIFIC=(0x00000002);

publicconstintDICS_ENABLE=(0x00000001);

publicconstintDICS_DISABLE=(0x00000002);

///

///注册设备或者设备类型,在指定的窗口返回相关的信息

///

///

///

///

///128);">

[DllImport("user32.dll",CharSet=CharSet.Auto)]

publicstaticexternIntPtrRegisterDeviceNotification(IntPtrhRecipient,DEV_BROADCAST_DEVICEINTERFACENotificationFilter,UInt32Flags);

///

///通过名柄,关闭指定设备的信息。

///

///

///

[DllImport("user32.dll",CharSet=CharSet.Auto)]

publicstaticexternuintUnregisterDeviceNotification(IntPtrhHandle);

///

///获取一个指定类别或全部类别的所有已安装设备的信息

///

///

///

///

///

///

[DllImport("setupapi.dll",SetLastError=true)]

publicstaticexternIntPtrSetupDiGetClassDevs(refGuidgClass,UInt32iEnumerator,IntPtrhParent,UInt32nFlags);

///

///销毁一个设备信息集合,并且释放所有关联的内存

///128);">

///

///

[DllImport("setupapi.dll",SetLastError=true)]

publicstaticexternintSetupDiDestroyDeviceInfoList(IntPtrlpInfoSet);

///

///枚举指定设备信息集合的成员,并将数据放在SP_DEVINFO_DATA中

///

///

///

///

///

[DllImport("setupapi.dll",SetLastError=true)]

publicstaticexternboolSetupDiEnumDeviceInfo(IntPtrlpInfoSet,UInt32dwIndex,SP_DEVINFO_DATAdevInfoData);

///

///获取指定设备的属性

///

///

///

///

///

///

///128);">

///

///

[DllImport("setupapi.dll",SetLastError=true)]

publicstaticexternboolSetupDiGetDeviceRegistryProperty(IntPtrlpInfoSet,SP_DEVINFO_DATADeviceInfoData,UInt32Property,UInt32PropertyRegDataType,StringBuilderPropertyBuffer,UInt32PropertyBufferSize,IntPtrRequiredSize);

///

///停用设备

///

///

///

///

///

///

[DllImport("setupapi.dll",SetLastError=true,CharSet=CharSet.Auto)]

publicstaticexternboolSetupDiSetClassInstallParams(IntPtrDeviceInfoSet,IntPtrDeviceInfoData,IntPtrClassInstallParams,intClassInstallParamsSize);

///

///启用设备

///

///

///

///0);">

///

[DllImport("setupapi.dll",CharSet=CharSet.Auto)]

publicstaticexternBooleanSetupDiCallClassInstaller(UInt32InstallFunction,IntPtrDeviceInfoSet,IntPtrDeviceInfoData);

///

///RegisterDeviceNotification所需参数

///

[StructLayout(LayoutKind.Sequential)]

publicstructDEV_BROADCAST_HANDLE

{

publicintdbch_size;

publicintdbch_devicetype;

publicintdbch_reserved;

publicIntPtrdbch_handle;

publicIntPtrdbch_hdevnotify;

publicGuiddbch_eventguid;

publiclongdbch_nameoffset;

publicbytedbch_data;

publicbytedbch_data1;

}

//WM_DEVICECHANGEmessage

[StructLayout(LayoutKind.Sequential)]

publicclassDEV_BROADCAST_DEVICEINTERFACE

{

publicintdbcc_size;

publicintdbcc_devicetype;

publicintdbcc_reserved;

}

///

///设备信息数据

///

[StructLayout(LayoutKind.Sequential)]

publicclassSP_DEVINFO_DATA

{

publicintcbSize;

public0);">GuidclassGuid;

publicintdevInst;

publiculongreserved;

};

///

///属性变更参数

///

[StructLayout(LayoutKind.Sequential)]

publicclassSP_PROPCHANGE_PARAMS

{

publicSP_CLASSINSTALL_HEADERClassInstallHeader=newSP_CLASSINSTALL_HEADER();

publicintStateChange;

publicintScope;

publicintHwProfile;

};

///

///设备安装

///

[StructLayout(LayoutKind.Sequential)]

publicclassSP_DEVINSTALL_PARAMS

{

publicintcbSize;

publicintFlags;

publicintFlagsEx;

publicIntPtrhwndParent;

publicIntPtrInstallMsgHandler;

publicIntPtrInstallMsgHandlerContext;

publicIntPtrFileQueue;

publicIntPtrClassInstallReserved;

publicintReserved;

[MarshalAs(UnmanagedType.LPTStr)]

publicstringDriverPath;

};

[StructLayout(LayoutKind.Sequential)]

publicclassSP_CLASSINSTALL_HEADER

{

publicintcbSize;

publicintInstallFunction;

};

}

}t>C#code

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;namespaceHardware{publicclassHardwareClass{属性#region属性///

///返回所有硬件信息列表//////publicstring[]List{get{ListHWList=newList();try{GuidmyGUID=System.Guid.Empty;IntPtrhDevInfo=Externs.SetupDiGetClassDevs(refmyGUID,0,IntPtr.Zero,Externs.DIGCF_ALLCLASSES|Externs.DIGCF_PRESENT);if(hDevInfo.ToInt32()==Externs.INVALID_HANDLE_VALUE){thrownewException("InvalidHandle");}Externs.SP_DEVINFO_DATADeviceInfoData;DeviceInfoData=newExterns.SP_DEVINFO_DATA();DeviceInfoData.cbSize=28;DeviceInfoData.devInst=0;DeviceInfoData.classGuid=System.Guid.Empty;DeviceInfoData.reserved=0;UInt32i;StringBuilderDeviceName=newStringBuilder("");DeviceName.Capacity=0);">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(Exceptionex)

...{

thrownewException("枚举设备列表出错",ex);

}

returnHWList.ToArray();

}

}

#endregion

公共事件#region公共事件

///

///清理非托管资源

///

///

publicvoidDispose(IntPtrcallback)

{

try

{

Externs.UnregisterDeviceNotification(callback);

}

catch

{

}

}

///

///设置指定设备的状态

///

///设备名称

///是否启用

///

publicboolSetState(string[]match,boolbEnable)

{

try0);">

{

GuidmyGUID=System.Guid.Empty;

IntPtrhDevInfo=Externs.SetupDiGetClassDevs(refmyGUID,0,IntPtr.Zero,Externs.DIGCF_ALLCLASSES|Externs.DIGCF_PRESENT);

if(hDevInfo.ToInt32()==Externs.INVALID_HANDLE_VALUE)

{

returnfalse;

}

Externs.SP_DEVINFO_DATADeviceInfoData;

DeviceInfoData=newExterns.SP_DEVINFO_DATA();

DeviceInfoData.cbSize=28;

DeviceInfoData.devInst=0;

DeviceInfoData.classGuid=System.Guid.Empty;

DeviceInfoData.reserved=0;

UInt32i;

StringBuilderDeviceName=newStringBuilder("");

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))

{

}

boolbMatch=true;

foreach(stringsearchinmatch)

{

if(!

DeviceName.ToString().ToLower().Contains(search.ToLower()))

{

bMatch=false;

break;

}

}

if(bMatch)

{

OpenClose(hDevInfo,DeviceInfoData,bEnable);

}

}

Externs.SetupDiDestroyDeviceInfoList(hDevInfo);

}

catch(Exceptionex)

{

thrownewException("枚举设备信息出错!

",ex);

returnfalse;

}

returntrue;

}

///

///允许一个窗口或者服务接收所有硬件的通知

///注:

目前还没有找到一个比较好的方法来处理如果通知服务。

///

///

///

///

publicboolAllowNotifications(IntPtrcallback,boolUseWindowHandle)

{

try

{

Externs.DEV_BROADCAST_DEVICEINTERFACEdbdi=newExterns.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);

returntrue;

}

catch(Exceptionex)

{

stringerr=ex.Message;

returnfa

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 卡通动漫

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1