home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / control.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  3.7 KB  |  145 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\install
  4.  * File Name: control.cpp
  5.  * Purpose  : Code to enable/disable network adapters
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <tchar.h>
  10. #include <stdio.h>
  11. #include <setupapi.h>
  12. #include <devguid.h>
  13.  
  14. int
  15. ControlDevice(
  16.     HDEVINFO Devs,             // Device set.
  17.     PSP_DEVINFO_DATA DevInfo,  // Device in set.
  18.     DWORD Action)              // Action to perform.
  19. {
  20.     SP_PROPCHANGE_PARAMS Params;
  21.  
  22.     //
  23.     // Operate on config-specific profile.
  24.     //
  25.     Params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  26.     Params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  27.     Params.StateChange = Action;
  28.     Params.Scope = DICS_FLAG_CONFIGSPECIFIC;
  29.     Params.HwProfile = 0;
  30.  
  31.     if (SetupDiSetClassInstallParams(Devs, DevInfo, &Params.ClassInstallHeader,
  32.                                      sizeof(Params)) &&
  33.         SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, Devs, DevInfo)) {
  34.         //
  35.         // Success!
  36.         //
  37.         return 1;
  38.     } else {
  39.         return 0;
  40.     }
  41. }
  42.  
  43.  
  44. LPTSTR
  45. GetDeviceServiceName(
  46.     HDEVINFO Devs,             // Device set.
  47.     PSP_DEVINFO_DATA DevInfo)  // Device in set.
  48. {
  49.     LPTSTR Buffer;
  50.     DWORD Attempt;
  51.     DWORD Size;
  52.     DWORD ActualSize;
  53.     DWORD DataType;
  54.  
  55.     Size = 64;  // Educated guess.
  56.  
  57.     for (Attempt = 0; Attempt < 2; Attempt++) {
  58.  
  59.         Buffer = new TCHAR[(Size / sizeof(TCHAR)) + sizeof(TCHAR)];
  60.         if (Buffer == NULL) {
  61.             return NULL;
  62.         }
  63.  
  64.         if (!SetupDiGetDeviceRegistryProperty(Devs, DevInfo, SPDRP_SERVICE,
  65.                                               &DataType, (LPBYTE)Buffer, Size,
  66.                                               &ActualSize)) {
  67.             printf("Got here\n");
  68.             if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  69.                 delete [] Buffer;
  70.                 return NULL;
  71.             }
  72.  
  73.             //
  74.             // Our guess at a buffer size was too small.  Try again.
  75.             //
  76.             Size = ActualSize;
  77.             delete [] Buffer;
  78.             continue;
  79.         }
  80.  
  81.         //
  82.         // Make sure the registry entry we found is of the expected type.
  83.         //
  84.         if (DataType != REG_SZ) {
  85.             delete [] Buffer;
  86.             return NULL;
  87.         }
  88.  
  89.         Size = ActualSize / sizeof(TCHAR);
  90.         Buffer[Size] = TEXT('\0');
  91.  
  92.         return Buffer;
  93.     }
  94.  
  95.     return NULL;
  96. }
  97.  
  98.  
  99. extern int
  100. ControlDeviceClass(
  101.     DWORD Action)  // Action to take on found device(s).
  102. {
  103.     HDEVINFO Devs = INVALID_HANDLE_VALUE;
  104.     SP_DEVINFO_DATA DevInfo;
  105.     DWORD DevIndex;
  106.     int Count = 0;
  107.  
  108.     //
  109.     // Create a device information set, consisting of our desired devices.
  110.     //
  111.     Devs = SetupDiGetClassDevs(&GUID_DEVCLASS_NET,  // Only "Net" devices.
  112.                                TEXT("ROOT"),        // With plausible PnP name.
  113.                                NULL,
  114.                                DIGCF_PRESENT);      // And currently present.
  115.     if (Devs == INVALID_HANDLE_VALUE) {
  116.         return 0;
  117.     }
  118.  
  119.     //
  120.     // Run through the set, looking for devices which have "VWiFi" as
  121.     // their service name.
  122.     //
  123.     DevInfo.cbSize = sizeof(DevInfo);
  124.     for (DevIndex = 0; SetupDiEnumDeviceInfo(Devs, DevIndex, &DevInfo);
  125.          DevIndex++) {
  126.  
  127.         LPTSTR Buffer;
  128.  
  129.         Buffer = GetDeviceServiceName(Devs, &DevInfo);
  130.  
  131.         if (_tcsicmp(Buffer, TEXT("VWiFimp")) == 0) {
  132.             //
  133.             // Found one.  Attempt to take requested action.
  134.             //
  135.             Count += ControlDevice(Devs, &DevInfo, Action);
  136.         }
  137.  
  138.         delete [] Buffer;
  139.     }
  140.  
  141.     SetupDiDestroyDeviceInfoList(Devs);
  142.  
  143.     return Count;
  144. }
  145.