How to open and disable a wireless card for a Windows device in a C + + program _c language

Source: Internet
Author: User

1. List Current network adapters: Setupdienumdeviceinfo
2. Find out the name of the current wireless card (using the Natvie WiFi API)
3. Uninstall \ Install this driver
Problem:
Log is: Setupdisetclassinstallparams failed. -536870347
 
The complete code is as follows:

ControlWirelessCard.cpp:Defines the entry point for the console application. #include "stdafx.h" #include <windows.h> #include <SetupAPI.h> #include <devguid.h> #include & lt;string> #include <cfgmgr32.h> #include <wlanapi.h> #pragma comment (lib, "setupapi.lib") #pragma comme 
 
NT (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_net 
    Work_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 *) &pi 
      flist->interfaceinfo[i]; IRet = StringFromGUID2 (Pifinfo->interfaceguid, (LPOLESTR) &guidstring, sizeof (guidstring)/sizeof (*guidstr Ing)); 
    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 Setupdienumdeviceinfo (L "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 *) &Amp Propchangeparams, sizeof (Propchangeparams))) {wprintf (L "Setupdisetclassinstallparams failed.%d\n", Getla 
      Sterror ()); 
    Throw lpszmsg; 
    }//Call the Classinstaller and perform the change. if (! Setupdicallclassinstaller (Dif_propertychange,hdevinfo,&deviceinfodata)) {wprintf (L) SetupDiCallClassInstal Ler failed. 
      %d\n ", GetLastError ()); 
    Throw lpszmsg;  
    } setcursor (Hcursor); 
  return TRUE; 
    catch (TCHAR * pszerror) {setcursor (hcursor); 
    if (NULL!= lpszmsg) {LocalFree ((hlocal) lpszmsg); 
  return FALSE; 
   an int _tmain (int argc, _tchar* argv[]) {LPTSTR = 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) {Wprin 
           TF (L "Enable the network card\n"); Changestatus (dics_enable, I, hdevinfo);/ENABLE}} return Setupdidestroydeviceinfol 
   IST (hdevinfo); 
     The catch (TCHAR * pszerror) {if (NULL!= lpszmsg) {LocalFree ((hlocal) lpszmsg); 
   return FALSE; 
return 0; 
 }

Some common errors are resolved
one,
to start with the error return value.
-536870347, with Windows own calculator, select programmer mode, convert this decimal to 16 to E0000235.
Use this error number to search, is the famous 32-bit program running in 64-bit system error.
Although I use the 64-bit WIN8 vs compiled program, the compilation option is still a WIN32 program. Then we only need to modify the x64 to compile, the operation is as follows.
Build-->configuration manager...--> Find the project that you want to modify, and select Platform as x64.
If not, the x64 compile entry is not added, and then platform select <New...>-->new platform the x64-->ok below.
Two,
Compile 64-bit program, run it. The
still complains, and the return value is 5. Access_denied.
Insufficient permissions, we need to apply for high privileges. Request Admin Permission below:
your project's properties-->linker-->manifest file-->uac Execution level select Requireadministrator.
Three,
continue to run. Uninstall the wireless card driver successfully.
But the previous program still has a problem, because the driver uninstall, you can not use the WLAN API to get the name of the wireless card,
so the program load wireless network card driver will fail.
We can use the keyword "Wireless network Adapter" to find the wireless network card, eliminating the use of WLAN APIs that step.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.