1、#include #include #include #include #include #include #include #include /cfgmgr32.h Microsoft Windows 2000 DDK 中.#include #pragma comment(lib,“setupapi.lib“)#define UNKNOWNDEVICE _T(“)typedef struct _DEVICE_INFOSP_DEVINFO_DATA DevInfoData; /设备信息包括实例句柄和设备类的 GUIDDWORD DevID; /在此次全局的枚举中设备 IDTCHAR *lpsz
2、DevName; /设备的名称ULONG ulDevNameLen; /设备名称的长度BOOL bDisableable; /是否可以被禁用BOOL bDisabled; /是否已经被禁用struct _DEVICE_INFO *pNextDevInfo; /下一个结构块DEVICE_INFO,*LPDEVICE_INFO;DEVICE_INFO DevInfo; /设备链表头BOOL IsDisableable(DWORD dwDevID, HDEVINFO hDevInfo) ;BOOL IsDisabled(DWORD dwDevID, HDEVINFO hDevInfo) ;BOOL
3、StateChange(DWORD dwNewState,DWORD dwDevID,HDEVINFO hDevInfo);/ 获得设备注册表中的内容/BOOL GetRegistryProperty( HDEVINFO DeviceInfoSet,PSP_DEVINFO_DATA DeviceInfoData,ULONG Property,PVOID Buffer,PULONG Length ) while ( !SetupDiGetDeviceRegistryProperty( DeviceInfoSet,DeviceInfoData,Property,NULL, (BYTE *)*(TC
4、HAR *)Buffer,*Length,Length) / 长度不够则重新分配缓冲区if (GetLastError() = ERROR_INSUFFICIENT_BUFFER) if (*(LPTSTR *)Buffer)LocalFree(*(LPTSTR *)Buffer);*(LPTSTR *)Buffer = (PCHAR)LocalAlloc(LPTR,*Length);else return false;return (BOOL)(*(LPTSTR *)Buffer)0;BOOL EnableDevice(DWORD dwDevID, HDEVINFO hDevInfo)ret
5、urn StateChange(DICS_ENABLE,dwDevID,hDevInfo);BOOL DisableDevice(DWORD dwDevID, HDEVINFO hDevInfo)return StateChange(DICS_DISABLE,dwDevID,hDevInfo);BOOL ControlDevice(DWORD dwDevID, HDEVINFO hDevInfo)BOOL bCanDisable;bCanDisable = (IsDisableable(dwDevID,hDevInfo) if(bCanDisable)return DisableDevice(
6、dwDevID,hDevInfo);elsereturn EnableDevice(dwDevID,hDevInfo);void EnumNetCards() DWORD Status, Problem;LPTSTR Buffer = NULL;DWORD BufSize = 0;/ 返回所有设备信息HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES) ;if (INVALID_HANDLE_VALUE = hDevInfo ) return; SP_DEVINFO_DATA De
7、viceInfoData = sizeof(SP_DEVINFO_DATA); / 枚举设备for ( DWORD DeviceId=0;SetupDiEnumDeviceInfo( hDevInfo,DeviceId,DeviceId+) / 获得设备的状态if (CM_Get_DevNode_Status( / 获取设备类名TCHAR szDevName MAX_PATH = _T(“) ;if (GetRegistryProperty(hDevInfo, if ( lstrcmp( szDevName, _T(“Net“) ) = 0 ) TCHAR szName MAX_PATH =
8、_T(“) ;if (GetRegistryProperty(hDevInfo, if ( lstrcmp( szName, _T(“ROOT“) ) != 0 ) if (GetRegistryProperty(hDevInfo, / 获取设备描述if (GetRegistryProperty(hDevInfo, if(ControlDevice(DeviceId,hDevInfo)printf(“Successfuln“);elseprintf(“FAILEDn“); SetupDiDestroyDeviceInfoList(hDevInfo);BOOL StateChange(DWORD
9、 dwNewState,DWORD dwDevID,HDEVINFO hDevInfo)SP_PROPCHANGE_PARAMS PropChangeParams;SP_DEVINFO_DATA DevInfoData = sizeof(SP_DEVINFO_DATA);SP_DEVINSTALL_PARAMS devParams;/查询设备信息if (!SetupDiEnumDeviceInfo( hDevInfo, dwDevID, return FALSE;/设置设备属性变化参数PropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_
10、CLASSINSTALL_HEADER);PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;PropChangeParams.Scope = DICS_FLAG_GLOBAL; /使修改的属性保存在所有的硬件属性文件PropChangeParams.StateChange = dwNewState;PropChangeParams.HwProfile = 0;/改变设备属性if (!SetupDiSetClassInstallParams( hDevInfo,return FALSE;PropCha
11、ngeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;PropChangeParams.Scope = DICS_FLAG_CONFIGSPECIFIC;/使修改的属性保存在指定的属性文件PropChangeParams.StateChange = dwNewState;PropChangeParams.HwProfile = 0;/改变设备属性并调用安装服务if (
12、!SetupDiSetClassInstallParams( hDevInfo,return TRUE;else/判断是否需要重新启动devParams.cbSize = sizeof(devParams);if (!SetupDiGetDeviceInstallParams( hDevInfo, return FALSE;if (devParams.Flags return TRUE;return TRUE;BOOL IsDisableable(DWORD dwDevID, HDEVINFO hDevInfo)SP_DEVINFO_DATA DevInfoData = sizeof(SP_D
13、EVINFO_DATA);DWORD dwDevStatus,dwProblem;if(!SetupDiEnumDeviceInfo(hDevInfo,dwDevID,return FALSE;/查询设备状态if(CM_Get_DevNode_Status(return FALSE;return (dwDevStatus BOOL IsDisabled(DWORD dwDevID, HDEVINFO hDevInfo)SP_DEVINFO_DATA DevInfoData = sizeof(SP_DEVINFO_DATA);DWORD dwDevStatus,dwProblem;if(!Set
14、upDiEnumDeviceInfo(hDevInfo,dwDevID,return FALSE;/查询设备状态if(CM_Get_DevNode_Status(return FALSE;return (dwDevStatus void GuidCopy(GUID guidSrc, GUID *guidDest)guidDest-Data1 = guidSrc.Data1;guidDest-Data2 = guidSrc.Data2;guidDest-Data3 = guidSrc.Data3;memcpy(guidDest-Data4,guidSrc.Data4,8);BOOL Constr
15、uctDeviceName(HDEVINFO DevInfoSet, PSP_DEVINFO_DATA DevInfoData, PVOID pBuffer, PULONG Length)/尝试通过各种类型的设备描述获得其设备名称if (!GetRegistryProperty( DevInfoSet,DevInfoData,SPDRP_FRIENDLYNAME , /设备的现实名称pBuffer,Length)if (!GetRegistryProperty( DevInfoSet,DevInfoData,SPDRP_DEVICEDESC , /设备的描述pBuffer,Length)if
16、(!GetRegistryProperty( DevInfoSet,DevInfoData,SPDRP_CLASS , /设备类名pBuffer,Length)if (!GetRegistryProperty( DevInfoSet,DevInfoData,SPDRP_CLASSGUID , /设备的GUIDpBuffer,Length)/什么都没找到就返回 UnknownDevice*Length = (lstrlen(UNKNOWNDEVICE)+1)*sizeof(TCHAR);*(LPTSTR *)pBuffer = (PCHAR)LocalAlloc(LPTR,*Length);_t
17、cscpy(*(LPTSTR *)pBuffer,UNKNOWNDEVICE);return TRUE;BOOL EnumDevices(HDEVINFO hDevInfo)DWORD dwDevID ; /, dwStatus, dwProblem;SP_DEVINFO_DATA DevInfoData = sizeof(SP_DEVINFO_DATA);LPDEVICE_INFO pDevInfo, pPreDevInfo;TCHAR *DeviceName = NULL ;ULONG ulNameLen = 256;pPreDevInfo = /穷举每个设备for(dwDevID = 0
18、;SetupDiEnumDeviceInfo(hDevInfo,dwDevID,dwDevID+)/构造设备信息块pDevInfo = (LPDEVICE_INFO)malloc(sizeof(DEVICE_INFO);pDevInfo-pNextDevInfo = NULL;pDevInfo-DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);pDevInfo-ulDevNameLen;pDevInfo-DevID = dwDevID;GuidCopy(DevInfoData.ClassGuid,pDevInfo-DevInfoData.DevInst
19、= DevInfoData.DevInst;pDevInfo-DevInfoData.Reserved = DevInfoData.Reserved;/这里有个问题,如果用设备信息块结构中的字符串指针/SetupDiGetDeviceRegistyProperty函数就会返回数据无效,不知道为什么。/所以下面只能在重新复制一遍了。ConstructDeviceName(hDevInfo,pDevInfo-ulDevNameLen = ulNameLen;pDevInfo-lpszDevName = (TCHAR *)malloc(ulNameLen*sizeof(TCHAR);memcpy(p
20、DevInfo-lpszDevName,DeviceName,ulNameLen);pDevInfo-bDisableable = IsDisableable(dwDevID, hDevInfo);pDevInfo-bDisabled = IsDisabled(dwDevID, hDevInfo);pPreDevInfo-pNextDevInfo = pDevInfo;pPreDevInfo = pDevInfo;return FALSE;int main(int argc, char* argv) HDEVINFO hDevInfo = NULL ;LPDEVICE_INFO pDevInfo = NULL ;EnumNetCards() ;return 0 ;/返回设备信息hDevInfo = SetupDiGetClassDevs(NULL,NULL,NULL,DIGCF_PRESENT|DIGCF_ALLCLASSES);DevInfo.DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);DevInfo.ulDevNameLen = 256;DevInfo.pNextDevInfo = NULL;/枚举系统设备EnumDevices(hDevInfo);
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。