home *** CD-ROM | disk | FTP | other *** search
- /*
- * Author : Ranveer Chandra
- * Directory: VirtualWiFi_Root\install
- * File Name: control.cpp
- * Purpose : Code to enable/disable network adapters
- */
-
- #include <windows.h>
- #include <tchar.h>
- #include <stdio.h>
- #include <setupapi.h>
- #include <devguid.h>
-
- int
- ControlDevice(
- HDEVINFO Devs, // Device set.
- PSP_DEVINFO_DATA DevInfo, // Device in set.
- DWORD Action) // Action to perform.
- {
- SP_PROPCHANGE_PARAMS Params;
-
- //
- // Operate on config-specific profile.
- //
- Params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
- Params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
- Params.StateChange = Action;
- Params.Scope = DICS_FLAG_CONFIGSPECIFIC;
- Params.HwProfile = 0;
-
- if (SetupDiSetClassInstallParams(Devs, DevInfo, &Params.ClassInstallHeader,
- sizeof(Params)) &&
- SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, Devs, DevInfo)) {
- //
- // Success!
- //
- return 1;
- } else {
- return 0;
- }
- }
-
-
- LPTSTR
- GetDeviceServiceName(
- HDEVINFO Devs, // Device set.
- PSP_DEVINFO_DATA DevInfo) // Device in set.
- {
- LPTSTR Buffer;
- DWORD Attempt;
- DWORD Size;
- DWORD ActualSize;
- DWORD DataType;
-
- Size = 64; // Educated guess.
-
- for (Attempt = 0; Attempt < 2; Attempt++) {
-
- Buffer = new TCHAR[(Size / sizeof(TCHAR)) + sizeof(TCHAR)];
- if (Buffer == NULL) {
- return NULL;
- }
-
- if (!SetupDiGetDeviceRegistryProperty(Devs, DevInfo, SPDRP_SERVICE,
- &DataType, (LPBYTE)Buffer, Size,
- &ActualSize)) {
- printf("Got here\n");
- if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
- delete [] Buffer;
- return NULL;
- }
-
- //
- // Our guess at a buffer size was too small. Try again.
- //
- Size = ActualSize;
- delete [] Buffer;
- continue;
- }
-
- //
- // Make sure the registry entry we found is of the expected type.
- //
- if (DataType != REG_SZ) {
- delete [] Buffer;
- return NULL;
- }
-
- Size = ActualSize / sizeof(TCHAR);
- Buffer[Size] = TEXT('\0');
-
- return Buffer;
- }
-
- return NULL;
- }
-
-
- extern int
- ControlDeviceClass(
- DWORD Action) // Action to take on found device(s).
- {
- HDEVINFO Devs = INVALID_HANDLE_VALUE;
- SP_DEVINFO_DATA DevInfo;
- DWORD DevIndex;
- int Count = 0;
-
- //
- // Create a device information set, consisting of our desired devices.
- //
- Devs = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, // Only "Net" devices.
- TEXT("ROOT"), // With plausible PnP name.
- NULL,
- DIGCF_PRESENT); // And currently present.
- if (Devs == INVALID_HANDLE_VALUE) {
- return 0;
- }
-
- //
- // Run through the set, looking for devices which have "VWiFi" as
- // their service name.
- //
- DevInfo.cbSize = sizeof(DevInfo);
- for (DevIndex = 0; SetupDiEnumDeviceInfo(Devs, DevIndex, &DevInfo);
- DevIndex++) {
-
- LPTSTR Buffer;
-
- Buffer = GetDeviceServiceName(Devs, &DevInfo);
-
- if (_tcsicmp(Buffer, TEXT("VWiFimp")) == 0) {
- //
- // Found one. Attempt to take requested action.
- //
- Count += ControlDevice(Devs, &DevInfo, Action);
- }
-
- delete [] Buffer;
- }
-
- SetupDiDestroyDeviceInfoList(Devs);
-
- return Count;
- }
-