资源描述:
《C#调用windows的API函数》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingSystem.Threading;usingSystem.Collections;usingSystem.IO;//以下是调用windows的API的函数//获得G
2、UID[DllImport("hid.dll")]publicstaticexternvoidHidD_GetHidGuid(refGuidHidGuid);GuidguidHID=Guid.Empty;//过滤设备,获取需要的设备[DllImport("setupapi.dll",SetLastError=true)]publicstaticexternIntPtrSetupDiGetClassDevs(refGuidClassGuid,uintEnumerator,IntPtrHwndParent,DIGCFFlags);IntPtrhD
3、evInfo;//获取设备,true获取到[DllImport("setupapi.dll",CharSet=CharSet.Auto,SetLastError=true)]publicstaticexternBooleanSetupDiEnumDeviceInterfaces(IntPtrhDevInfo,IntPtrdevInfo,refGuidinterfaceClassGuid,UInt32memberIndex,refSP_DEVICE_INTERFACE_DATAdeviceInterfaceData);publicstructS
4、P_DEVICE_INTERFACE_DATA{publicintcbSize;publicGuidinterfaceClassGuid;publicintflags;publicintreserved;}//获取接口的详细信息必须调用两次第1次返回长度第2次获取数据[DllImport("setupapi.dll",SetLastError=true,CharSet=CharSet.Auto)]privatestaticexternboolSetupDiGetDeviceInterfaceDetail(IntPtrdeviceInfoSet
5、,refSP_DEVICE_INTERFACE_DATAdeviceInterfaceData,IntPtrdeviceInterfaceDetailData,intdeviceInterfaceDetailDataSize,refintrequiredSize,SP_DEVINFO_DATAdeviceInfoData);[StructLayout(LayoutKind.Sequential)]publicclassSP_DEVINFO_DATA{publicintcbSize=Marshal.SizeOf(typeof(SP_DEVINF
6、O_DATA));publicGuidclassGuid=Guid.Empty;//temppublicintdevInst=0;//dumypublicintreserved=0;}[StructLayout(LayoutKind.Sequential,Pack=2)]internalstructSP_DEVICE_INTERFACE_DETAIL_DATA{internalintcbSize;internalshortdevicePath;}publicenumDIGCF{DIGCF_DEFAULT=0x1,DIGCF_PRESENT=0
7、x2,DIGCF_ALLCLASSES=0x4,DIGCF_PROFILE=0x8,DIGCF_DEVICEINTERFACE=0x10}//获取设备文件[DllImport("kernel32.dll",SetLastError=true)]privatestaticexternintCreateFile(stringlpFileName,//filenameuintdwDesiredAccess,//accessmodeuintdwShareMode,//sharemodeuintlpSecurityAttributes,//SDuint
8、dwCreationDisposition,//howtocreateuintdwFlagsAndAttributes,//fileattributesuinthT