在C++法式中開啟和禁用Windows裝備的無線網卡的辦法。本站提示廣大學習愛好者:(在C++法式中開啟和禁用Windows裝備的無線網卡的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是在C++法式中開啟和禁用Windows裝備的無線網卡的辦法正文
1.列出以後網卡:SetupDiEnumDeviceInfo
2.找出以後無線網卡的名字(用natvie wifi api)
3.卸載\裝置此驅動
成績:
log為:SetupDiSetClassInstallParams failed. -536870347
完全代碼以下:
// ControlWirelessCard.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> #include <SetupAPI.h> #include <devguid.h> #include <string> #include <cfgmgr32.h> #include <wlanapi.h> #pragma comment(lib,"setupapi.lib") #pragma comment(lib, "wlanapi.lib") using namespace std; wstring getWirelessNetworkCardName() { wstring wirelessName; HANDLE hClient = NULL; DWORD dwMaxClient = 2; // DWORD dwCurVersion = 0; DWORD dwResult = 0; DWORD dwRetVal = 0; int iRet = 0; WCHAR GuidString[39] = {0}; unsigned int i; /* variables used for WlanEnumInterfaces */ PWLAN_INTERFACE_INFO_LIST pIfList = NULL; PWLAN_INTERFACE_INFO pIfInfo = NULL; dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient); if (dwResult != ERROR_SUCCESS) { wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult); return wirelessName; } dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwResult != ERROR_SUCCESS) { wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult); return wirelessName; } else { PWLAN_AVAILABLE_NETWORK_LIST pWLAN_AVAILABLE_NETWORK_LIST = NULL; dwResult = WlanGetAvailableNetworkList(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid, WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES, NULL, &pWLAN_AVAILABLE_NETWORK_LIST); if (dwResult != ERROR_SUCCESS) { printf("WlanGetAvailableNetworkList failed with error: %u\n",dwResult); WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST); return wirelessName; } for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) { pIfInfo = (WLAN_INTERFACE_INFO *) &pIfList->InterfaceInfo[i]; iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString, sizeof(GuidString)/sizeof(*GuidString)); wirelessName = pIfInfo->strInterfaceDescription; } } return wirelessName; } BOOL changeStatus(DWORD NewStatus, DWORD SelectedItem, HDEVINFO hDevInfo) { LPTSTR lpszMsg = NULL; HCURSOR hCursor = NULL; try { SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)}; SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)}; hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT)); // Get a handle to the Selected Item. if (!SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, &DeviceInfoData)) { wprintf(L"SetupDiEnumDeviceInfo failed. %d\n",GetLastError()); throw lpszMsg; } // Set the PropChangeParams structure. PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; PropChangeParams.Scope = DICS_FLAG_GLOBAL; PropChangeParams.StateChange = NewStatus; if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&PropChangeParams, sizeof(PropChangeParams))) { wprintf(L"SetupDiSetClassInstallParams failed. %d\n",GetLastError()); throw lpszMsg; } // Call the ClassInstaller and perform the change. if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&DeviceInfoData)) { wprintf(L"SetupDiCallClassInstaller failed. %d\n",GetLastError()); throw lpszMsg; } SetCursor(hCursor); return TRUE; } catch (TCHAR * pszError) { SetCursor(hCursor); if (NULL != lpszMsg) { LocalFree((HLOCAL)lpszMsg); } return FALSE; } } int _tmain(int argc, _TCHAR* argv[]) { LPTSTR lpszMsg = NULL; try { /* TCHAR * GUIDString = NULL; GUID guid; ZeroMemory(&guid, sizeof(GUID)); GUIDString = _T("4d36e972-e325-11ce-bfc1-08002be10318"); UuidFromString((unsigned char *)GUIDString, &guid); */ //GUID guid = getWirelessNetworkCardGUID(); HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_NET,/*L"WIRELESS"*/NULL,NULL,DIGCF_PRESENT); //HDEVINFO hDevInfo = SetupDiGetClassDevs(&guid,/*L"WIRELESS"*/NULL,NULL,DIGCF_PRESENT); if (INVALID_HANDLE_VALUE == hDevInfo) { wprintf(L"INVALID_HANDLE_VALUE"); return -1; } DWORD i; int Icount=0; SP_DEVINFO_DATA DeviceInfoData; ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA)); DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); ++i) { //Get DeviceInfo LPTSTR buffer = NULL; DWORD buffersize = 0; DWORD Status, Problem; BOOL bDisabled = FALSE; while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, //Read the names of the designated card NULL, (PBYTE) buffer, buffersize, &buffersize) ) { if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) { if (buffer) LocalFree(buffer); buffer=(LPTSTR)LocalAlloc(LPTR,buffersize); } else { wprintf(L"read network card failed!"); } } std::wstring strEthernet = buffer; //wprintf(L"the buffer is %ws\n",buffer); wstring wirelessName = getWirelessNetworkCardName(); if(wirelessName.empty()) { wprintf(L"wirelessName IS empty\n"); return -1; } //wprintf(L"wirelessName is %ws\n",wirelessName); if(wirelessName == buffer) { wprintf(L"Found the wireless card: %ws\n",wirelessName); if (int i= CM_Get_DevNode_Status(&Status, &Problem, DeviceInfoData.DevInst,0) != CR_SUCCESS) { wprintf(L"CM_Get_DevNode_Status failed. %d\n",GetLastError()); throw "failed\n"; } if (!((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem))) { bDisabled = FALSE; } if ((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem)) { bDisabled = TRUE; } if(bDisabled==FALSE) { wprintf(L"disable the network card\n"); changeStatus(DICS_DISABLE, i, hDevInfo);//disable } if(bDisabled==TRUE) { wprintf(L"enable the network card\n"); changeStatus(DICS_ENABLE, i, hDevInfo);// enable } } } return SetupDiDestroyDeviceInfoList(hDevInfo); } catch (TCHAR * pszError) { if (NULL != lpszMsg) { LocalFree((HLOCAL)lpszMsg); } return FALSE; } return 0; }
一些罕見毛病處理
1、
從毛病前往值動手。
-536870347,用windows自帶盤算器,選擇法式員形式,將此十進制轉換為16進制為E0000235.
用此毛病號搜刮,就是年夜名鼎鼎的32位法式跑在64位體系中報錯。
固然我用的64位win8下VS編譯的法式,然則編譯選項中依然是WIN32法式。那末我們只須要修正成x64編譯便可,操作以下。
BUILD-->Configuration Manager...-->找到要修正的project,將platform選擇為x64.
假如沒有,解釋沒有添加x64編譯項,接著下面,在platform當選擇<New...> -->New platform下選x64-->OK.
2、
編譯64位法式,運轉之。
依然報錯,前往值是5.ACCESS_DENIED.
權限不敷,我們須要請求高權限。以下請求Admin權限:
你項目標Properties-->Linker-->Manifest File-->UAC Execution Level選requireAdministrator。
3、
持續運轉。卸載無線網卡驅動勝利。
然則上一篇的法式依然有個成績,因為驅動卸載了,就不克不及用wlan api獲得無線網卡稱號,
所以法式加載無線網卡驅動就會掉敗。
我們可以用症結字“Wireless Network Adapter”來查找無線網卡,免卻應用wlan api那一步。