使用 WMI 更改亮度

2023-12-06

我尝试过这个 Windows 示例从远程计算机获取 WMI 数据还有这个调用提供者方法,并且两者都在我的计算机上正常工作。不过,我尝试使用 WMI 来更改亮度,但在本部分的第 6 步中出现错误:

    //Get the Next Object from the collection  
    hres = pEnum->Next(WBEM_INFINITE, //Timeout  
    1, //No of objects requested  
    &pObj, //Returned Object  
    &ulReturned /*No of object returned*/);

pEnum是一个负值。

重要提示:当我写这个问题时,我尝试了不同的计算机,但除了使用笔记本电脑外,所有计算机都出现错误。那么,如何改变显示器的亮度呢?

我还意识到,如果我转到 wbemtest.exe,在我所有的计算机中,该类WmiMonitorBrightnessMethods和方法WmiSetBrightness存在,但仅在笔记本电脑中存在它的实例。事实上,在计算机中,当我单击该类的视图对象时,我收到此消息(见图)

No instances of WmiMonitorBrightnessMethodsClass

这是我的代码:

#define _WIN32_DCOM

#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

#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 << "Failed to initialize COM library. Error code = 0x" 
             << hex << hres << 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 << "Failed to initialize security. Error code = 0x" 
             << hex << hres << 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 *) &pLoc);

    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object. "
             << "Err code = 0x"
             << hex << hres << 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->ConnectServer(
        _bstr_t(L"ROOT\\WMI"), 
        NULL,
        NULL, 
        0, 
        NULL, 
        0, 
        0, 
        &pSvc
    );

    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hres << endl;
        pLoc->Release();
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to ROOT\\WMI namespace" << 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 << "Could not set proxy blanket. Error code = 0x" 
             << hex << hres << endl;
        pSvc->Release();
        pLoc->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->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*/);

    hres = WBEM_S_NO_ERROR;

    ULONG ulReturned;
    IWbemClassObject *pObj;
    DWORD retVal = 0;

    //Get the Next Object from the collection  
    hres = pEnum->Next(WBEM_INFINITE, //Timeout  
    1, //No of objects requested  
    &pObj, //Returned Object  
    &ulReturned /*No of object returned*/);

    IWbemClassObject* pClass = NULL;
    hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

    IWbemClassObject* pInParamsDefinition = NULL;
    hres = pClass->GetMethod(MethodName, 0, &pInParamsDefinition, NULL);

    IWbemClassObject* pClassInstance = NULL;
    hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

    VARIANT var1;
    VariantInit(&var1);
    BSTR ArgName0 = SysAllocString(L"Timeout");

    V_VT(&var1) = VT_BSTR;
    V_BSTR(&var1) = SysAllocString(L"0");
    hres = pClassInstance->Put(ArgName0,
        0,
        &var1,
        CIM_UINT32); //CIM_UINT64  
    VariantClear(&var1);

    VARIANT var;
    VariantInit(&var);
    BSTR ArgName1 = SysAllocString(L"Brightness");

    V_VT(&var2) = VT_BSTR;
    V_BSTR(&var2) = SysAllocString(L"80"); //Brightness value
    hres = pClassInstance->Put(ArgName1,
        0,
        &var2,
        CIM_UINT8);
    VariantClear(&var2);

    // Call the method  
    VARIANT pathVariable;
    VariantInit(&pathVariable);

    hres = pSvc->ExecMethod(pathVariable.bstrVal,
        MethodName,
        0,
        NULL,
        pClassInstance,
        NULL,
        NULL);
    VariantClear(&pathVariable);


    return 0;
}

正如阿米特·沙基亚(Amit Shakya)所说。只有在可以动态设置亮度的系统(笔记本电脑和某些一体机设备)中才可以通过 WMI 更改亮度。

然而,微软有一个功能可以让你改变外接显示器的亮度,SetMonitorBrightness.

See 微软图书馆

我附上一个简单的例子来说明如何做到这一点:

// Includes
#include "PhysicalMonitorEnumerationAPI.h"
#include "HighLevelMonitorConfigurationAPI.h"

(...)

// Prepare variables
HMONITOR hMonitor = NULL;
HMONITOR hMonitorTest = NULL;
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;

// Get the screen
HWND hWnd = GetDesktopWindow();
hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);

_BOOL success = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);
if(success)
{
    pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));        
    if(pPhysicalMonitors != NULL)
    {
        success = GetPhysicalMonitorsFromHMONITOR(hMonitor,cPhysicalMonitors, pPhysicalMonitors);
        HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;

        // Set brightness to 50%
        DWORD dwNewBrightness = 50;
        success = SetMonitorBrightness(hPhysicalMonitor, dwNewBrightness);

        // Free resources
        free(pPhysicalMonitors);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 WMI 更改亮度 的相关文章

随机推荐