title: Change Brightness using WMI
body: <p>I have tried this Windows example <a href="https://msdn.microsoft.com/en-us/library/aa390422(v=vs.85).aspx" rel="nofollow noreferrer">Getting WMI Data from a Remote Computer</a> and this other <a href="https://msdn.microsoft.com/en-us/library/aa390421(v=vs.85).aspx" rel="nofollow noreferrer">Calling a Provider Method</a>, and both work correctly in my computer. However I have tried to use WMI to change the brightness and I am getting an error on step 6, in this part:</p>
  
  <pre><code>    //Get the Next Object from the collection  
      hres = pEnum-&gt;Next(WBEM_INFINITE, //Timeout  
      1, //No of objects requested  
      &amp;pObj, //Returned Object  
      &amp;ulReturned /*No of object returned*/);
  </code></pre>
  
  <p><code>pEnum</code> is a negative value.</p>
  
  <p>IMPORTANT: While I was writing this question, I tried with different computers and I get errors in all of them, except when I use a laptop. Therefore, how can I change the brightness of a monitor?</p>
  
  <p>I also realize that if I go to wbemtest.exe, in all my computers the class <code>WmiMonitorBrightnessMethods</code> and the method <code>WmiSetBrightness</code> exist, but only in the laptop there is an instance of it. In fact, in the computers when I click on view objects of the class I get this message (see image)</p>
  
  <p><a href="https://i.stack.imgur.com/pWz05.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pWz05.png" alt="No instances of WmiMonitorBrightnessMethodsClass"></a></p>
  
  <p>This is my code:</p>
  
  <pre><code>#define _WIN32_DCOM
  
  #include &lt;iostream&gt;
  using namespace std;
  #include &lt;comdef.h&gt;
  #include &lt;Wbemidl.h&gt;
  
  #pragma comment(lib, "wbemuuid.lib")
  
  int main(int iArgCnt, char ** argv)
  {
      HRESULT hres;
  
      // Step 1: --------------------------------------------------
      // Initialize COM. ------------------------------------------
  
      hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
      if (FAILED(hres))
      {
          cout &lt;&lt; "Failed to initialize COM library. Error code = 0x" 
               &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl;
          return 1;                  // Program has failed.
      }
  
      // Step 2: --------------------------------------------------
      // Set general COM security levels --------------------------
  
      hres =  CoInitializeSecurity(
          NULL, 
          -1,                          // COM negotiates service
          NULL,                        // Authentication services
          NULL,                        // Reserved
          RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
          RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
          NULL,                        // Authentication info
          EOAC_NONE,                   // Additional capabilities 
          NULL                         // Reserved
          );
  
  
      if (FAILED(hres))
      {
          cout &lt;&lt; "Failed to initialize security. Error code = 0x" 
               &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl;
          CoUninitialize();
          return 1;                      // Program has failed.
      }
  
      // Step 3: ---------------------------------------------------
      // Obtain the initial locator to WMI -------------------------
  
      IWbemLocator *pLoc = NULL;
  
      hres = CoCreateInstance(
          CLSID_WbemLocator,
          0, 
          CLSCTX_INPROC_SERVER, 
          IID_IWbemLocator, (LPVOID *) &amp;pLoc);
  
      if (FAILED(hres))
      {
          cout &lt;&lt; "Failed to create IWbemLocator object. "
               &lt;&lt; "Err code = 0x"
               &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl;
          CoUninitialize();
          return 1;                 // Program has failed.
      }
  
      // Step 4: ---------------------------------------------------
      // Connect to WMI through the IWbemLocator::ConnectServer method
  
      IWbemServices *pSvc = NULL;
  
      // Connect to the local root\wminamespace
      // and obtain pointer pSvc to make IWbemServices calls.
      hres = pLoc-&gt;ConnectServer(
          _bstr_t(L"ROOT\\WMI"), 
          NULL,
          NULL, 
          0, 
          NULL, 
          0, 
          0, 
          &amp;pSvc
      );
  
      if (FAILED(hres))
      {
          cout &lt;&lt; "Could not connect. Error code = 0x" 
               &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl;
          pLoc-&gt;Release();
          CoUninitialize();
          return 1;                // Program has failed.
      }
  
      cout &lt;&lt; "Connected to ROOT\\WMI namespace" &lt;&lt; endl;
  
  
      // Step 5: --------------------------------------------------
      // Set security levels for the proxy ------------------------
  
      hres = CoSetProxyBlanket(
          pSvc,                        // Indicates the proxy to set
          RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx 
          RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx 
          NULL,                        // Server principal name 
          RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
          RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
          NULL,                        // client identity
          EOAC_NONE                    // proxy capabilities 
      );
  
      if (FAILED(hres))
      {
          cout &lt;&lt; "Could not set proxy blanket. Error code = 0x" 
               &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl;
          pSvc-&gt;Release();
          pLoc-&gt;Release();
          CoUninitialize();
          return 1;               // Program has failed.
      }
  
      // Step 6: --------------------------------------------------
      // Call WmiSetBrightness method -----------------------------
  
      // set up to call the Win32_Process::Create method
      BSTR ClassName = SysAllocString(L"WmiMonitorBrightnessMethods");
      BSTR MethodName = SysAllocString(L"WmiSetBrightness");
      BSTR bstrQuery = SysAllocString(L"Select * from WmiMonitorBrightnessMethods");
      IEnumWbemClassObject *pEnum = NULL;
  
      hres = pSvc-&gt;ExecQuery(_bstr_t(L"WQL"), //Query Language  
      bstrQuery, //Query to Execute  
      WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, //Make a semi-synchronous call  
      NULL, //Context  
      &amp;pEnum /*Enumeration Interface*/);
  
      hres = WBEM_S_NO_ERROR;
  
      ULONG ulReturned;
      IWbemClassObject *pObj;
      DWORD retVal = 0;
  
      //Get the Next Object from the collection  
      hres = pEnum-&gt;Next(WBEM_INFINITE, //Timeout  
      1, //No of objects requested  
      &amp;pObj, //Returned Object  
      &amp;ulReturned /*No of object returned*/);
  
      IWbemClassObject* pClass = NULL;
      hres = pSvc-&gt;GetObject(ClassName, 0, NULL, &amp;pClass, NULL);
  
      IWbemClassObject* pInParamsDefinition = NULL;
      hres = pClass-&gt;GetMethod(MethodName, 0, &amp;pInParamsDefinition, NULL);
  
      IWbemClassObject* pClassInstance = NULL;
      hres = pInParamsDefinition-&gt;SpawnInstance(0, &amp;pClassInstance);
  
      VARIANT var1;
      VariantInit(&amp;var1);
      BSTR ArgName0 = SysAllocString(L"Timeout");
  
      V_VT(&amp;var1) = VT_BSTR;
      V_BSTR(&amp;var1) = SysAllocString(L"0");
      hres = pClassInstance-&gt;Put(ArgName0,
          0,
          &amp;var1,
          CIM_UINT32); //CIM_UINT64  
      VariantClear(&amp;var1);
  
      VARIANT var;
      VariantInit(&amp;var);
      BSTR ArgName1 = SysAllocString(L"Brightness");
  
      V_VT(&amp;var2) = VT_BSTR;
      V_BSTR(&amp;var2) = SysAllocString(L"80"); //Brightness value
      hres = pClassInstance-&gt;Put(ArgName1,
          0,
          &amp;var2,
          CIM_UINT8);
      VariantClear(&amp;var2);
  
      // Call the method  
      VARIANT pathVariable;
      VariantInit(&amp;pathVariable);
  
      hres = pSvc-&gt;ExecMethod(pathVariable.bstrVal,
          MethodName,
          0,
          NULL,
          pClassInstance,
          NULL,
          NULL);
      VariantClear(&amp;pathVariable);
  
  
      return 0;
  }
  </code></pre>