7

Here is my attempt to disable/enable network adapters on Windows:

void EnableNetDevice(bool aState, int index)
{
  HDEVINFO NetPnPHandle;
  SP_PROPCHANGE_PARAMS PCHP;
  SP_DEVINFO_DATA DeviceData;
  NetPnPHandle = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, 0, 0, DIGCF_PRESENT);

  if (NetPnPHandle == INVALID_HANDLE_VALUE)
  {
        return;
  }

  DeviceData.cbSize = sizeof(SP_DEVINFO_DATA);
  SetupDiEnumDeviceInfo(NetPnPHandle, index, &DeviceData);
  PCHP.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);

  if (SetupDiSetClassInstallParams(NetPnPHandle,&DeviceData,&PCHP.ClassInstallHeader,sizeof(SP_PROPCHANGE_PARAMS)))
  {
        PCHP.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
        PCHP.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
        PCHP.HwProfile = 0;
        PCHP.Scope = DICS_FLAG_CONFIGSPECIFIC;
        if (aState) PCHP.StateChange = DICS_ENABLE;
        else  PCHP.StateChange = DICS_DISABLE;
        SetupDiSetClassInstallParams(NetPnPHandle,&DeviceData,&PCHP.ClassInstallHeader,sizeof(SP_PROPCHANGE_PARAMS));
        SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,NetPnPHandle,&DeviceData);
  }

  DeviceData.cbSize = sizeof(SP_DEVINFO_DATA);
  SetupDiDestroyDeviceInfoList(NetPnPHandle);
}

The problem is it works perfectly on Windows XP but doesn't work on Win 7 :(

Could you help me please find the bug?

Thank you very much!

The_Immortal
  • 117
  • 4
  • 10
  • Are you running with elevated privileges? If not you need to be. – Captain Obvlious Jun 26 '13 at 22:48
  • You should also be checking the return value of each function call for failure and using `GetLastError` to determine the error condition. – Captain Obvlious Jun 26 '13 at 22:50
  • Actually I found the real problem. There is a glitch with function SetupDiCallClassInstaller - it always return FALSE on x64 OS! So it doesn't work on Win XP x64, Win 7 x64, etc... I have no idea how to fix it ;( – The_Immortal Jun 26 '13 at 23:30

1 Answers1

6

It's simply a requirement to call the function from a 64-bit process on x64 OS. See http://msdn.microsoft.com/en-us/library/windows/hardware/ff541255(v=vs.85).aspx

White hawk
  • 1,468
  • 1
  • 15
  • 23
  • Yes indeed, 32 bits processes running under 64 bits operating system are not able to call that API methods. You have to create a 64 bits executable. – bkausbk Oct 22 '13 at 08:53