Skip to content

Instantly share code, notes, and snippets.

@JavierJF
Created September 28, 2017 16:57
Show Gist options
  • Save JavierJF/1baca8c68e8838c524faa42d40075308 to your computer and use it in GitHub Desktop.
Save JavierJF/1baca8c68e8838c524faa42d40075308 to your computer and use it in GitHub Desktop.
WMI Client POC function for setting brightness
#include <objbase.h>
#include <windows.h>
#include <stdio.h>
#include <wbemidl.h>
#include <comdef.h>
#include <string>
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "comsuppw.lib")
errno_t setScreenbrightness(int percent) {
if (percent > 100 || percent < 0) {
return E_INVALIDARG;
}
std::wstring percentWS = std::to_string(percent);
// Pointers for accessing and calling the WMI service
IWbemLocator* pLocator = NULL;
IWbemServices* pNamespace = 0;
IWbemClassObject* pClass = NULL;
IWbemClassObject* pInClass = NULL;
IWbemClassObject* pInInst = NULL;
IEnumWbemClassObject* pEnum = NULL;
// Result type for checking the process
HRESULT hr = S_OK;
BSTR path = SysAllocString(L"root\\wmi");
BSTR classPath = SysAllocString(L"WmiMonitorbrightnessMethods");
BSTR methodName = SysAllocString(L"WmiSetbrightness");
BSTR timeout = SysAllocString(L"Timeout");
BSTR brightness = SysAllocString(L"Brightness");
BSTR bstrQuery = SysAllocString(L"Select * from WmiMonitorbrightnessMethods");
if (!path || ! classPath || !methodName || ! timeout) {
hr = ::GetLastError();
goto cleanup;
}
// Initialize COM and connect up to CIMOM
hr = CoInitialize(0);
if (FAILED(hr)) {
hr = ::GetLastError();
goto cleanup;
}
// NOTE:
// When using asynchronous WMI API's remotely in an environment where the "Local System" account
// has no network identity (such as non-Kerberos domains), the authentication level of
// RPC_C_AUTHN_LEVEL_NONE is needed. However, lowering the authentication level to
// RPC_C_AUTHN_LEVEL_NONE makes your application less secure. It is wise to
// use semi-synchronous API's for accessing WMI data and events instead of the asynchronous ones.
hr = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hr)) {
hr = ::GetLastError();
goto cleanup;
}
hr = CoCreateInstance(CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *) &pLocator);
if (FAILED(hr)) {
hr = ::GetLastError();
goto cleanup;
}
hr = pLocator->ConnectServer(
path,
NULL,
NULL,
NULL,
0,
NULL,
NULL,
&pNamespace
);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
hr = CoSetProxyBlanket(
pNamespace,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
hr = pNamespace->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
&pEnum // Enumeration Interface
);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
hr = WBEM_S_NO_ERROR;
ULONG ulReturned;
IWbemClassObject *pObj;
DWORD retVal = 0;
// Get the Next Object from the collection
hr = pEnum->Next(
WBEM_INFINITE, // timeout
1, // No of objects requested
&pObj, // Returned Object
&ulReturned // No of object returned
);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
// Get the class object
hr = pNamespace->GetObject(classPath, 0, NULL, &pClass, NULL);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
// Get the input argument and set the property
hr = pClass->GetMethod(methodName, 0, &pInClass, NULL);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
hr = pInClass->SpawnInstance(0, &pInInst);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
VARIANT timeoutVar;
VariantInit(&timeoutVar);
V_VT(&timeoutVar) = VT_BSTR;
V_BSTR(&timeoutVar) = SysAllocString(L"0");
hr = pInInst->Put(
timeout,
0,
&timeoutVar,
CIM_UINT32
);
VariantClear(&timeoutVar);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
VARIANT brightVar;
VariantInit(&brightVar);
V_VT(&brightVar) = VT_BSTR;
// V_BSTR(&brightVar) = SysAllocString(L"100");
V_BSTR(&brightVar) = percentWS.c_str();
hr = pInInst->Put(brightness, 0, &brightVar, CIM_UINT8);
VariantClear(&brightVar);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
VARIANT pathVariable;
VariantInit(&pathVariable);
hr = pObj->Get(
_bstr_t(L"__PATH"),
0,
&pathVariable,
NULL,
NULL
);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
hr = pNamespace->ExecMethod(
pathVariable.bstrVal,
methodName,
0,
NULL,
pInInst,
NULL,
NULL
);
VariantClear(&pathVariable);
if (hr != WBEM_S_NO_ERROR) {
goto cleanup;
}
// Free up resources
cleanup:
SysFreeString(path);
SysFreeString(classPath);
SysFreeString(methodName);
SysFreeString(timeout);
SysFreeString(brightness);
SysFreeString(bstrQuery);
if (pClass) pClass->Release();
if (pInInst) pInInst->Release();
if (pInClass) pInClass->Release();
if (pLocator) pLocator->Release();
if (pNamespace) pNamespace->Release();
CoUninitialize();
return hr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment