在C++中通过PID为进程启用效率模式(EcoQoS)并在任务管理器中验证

问题描述 投票:0回答:1

我正在尝试为给定进程 ID (PID) 的进程启用效率模式 (EcoQoS)。我已经在 Stack Overflow 上提到了这个类似的问题,但我仍然面临问题。

问题: 尽管日志表明 EcoQoS 已启用,但它并未反映在任务管理器中。对于任何进程,如果效率模式打开,任务管理器应在效率模式下显示该进程的状态。

enter image description here

目标: 为给定的 PID 启用 EcoQoS。目前,我正在 Windows 11 中的 Visual Studio 2022 中的示例 C++ 控制台应用程序中尝试此操作。最终,我计划将此方法集成到用 .NET C# 编写的更大的 UWP 应用程序中。

代码:

#include <iostream>
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <tchar.h>
#include <sstream>

#pragma comment(lib, "pdh.lib")

// Enables EcoQoS (power throttling) for a specific process by its PID
void enable_ecoqos_for_pid(DWORD pid) {
    // Open a handle to the target process with the required access rights
    HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, pid);
    if (hProcess == NULL) {
        // Log an error message if the process handle cannot be obtained
        std::cerr << "Failed to open process. Error: " << GetLastError() << std::endl;
        return;
    }

    // Initialize the power throttling state structure
    PROCESS_POWER_THROTTLING_STATE powerThrottlingState = {};
    powerThrottlingState.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
    powerThrottlingState.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
    powerThrottlingState.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;

    // Apply the power throttling settings to the target process
    if (!SetProcessInformation(hProcess, ProcessPowerThrottling, &powerThrottlingState, sizeof(powerThrottlingState))) {
        // Log an error message if setting the power throttling information fails
        std::cerr << "Failed to enable power throttling for PID " << pid << ". Error: " << GetLastError() << std::endl;
    } else {
        // Log a success message if the power throttling settings are applied successfully
        std::cout << "Power throttling enabled successfully for PID " << pid << "." << std::endl;
    }

    // Close the handle to the process to free resources
    CloseHandle(hProcess);
}

int main() {
    // Define the process ID for which EcoQoS should be enabled
    DWORD pid = 16196;

    // Enable EcoQoS for the specified process ID
    enable_ecoqos_for_pid(pid);

    // Print a message indicating the end of the program
    std::cout << "Hello World!" << std::endl;

    // Return 0 to indicate successful program termination
    return 0;
}

问题:

  1. 我的代码是否正确为给定的 PID 启用效率模式?
  2. 如何验证流程是否启用了效率模式,特别是当它未反映在任务管理器中时?

附加信息: 我正在使用 Visual Studio 2022。 用于集成的目标 UWP 应用程序是用 .NET C# 编写的,具有要在效率模式下运行的进程。 有关为流程启用效率模式的文档很少。 任何见解或建议将不胜感激!预先感谢您的帮助。

c++ .net windows performance process
1个回答
0
投票

您是否尝试过使用 SetPriorityClass 函数将进程优先级设置为 IDLE_PRIORITY_CLASS?

SetPriorityClass(hProcess, IDLE_PRIORITY_CLASS);

将其添加到您的代码中,它看起来像下面这样

#include <iostream>
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <tchar.h>
#include <sstream>

#pragma comment(lib, "pdh.lib")

// Enables EcoQoS (power throttling) for a specific process by its PID
void enable_ecoqos_for_pid(DWORD pid) {
    // Open a handle to the target process with the required access rights
    HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, pid);

    // Set process priority class
    SetPriorityClass(hProcess, IDLE_PRIORITY_CLASS);

    if (hProcess == NULL) {
        // Log an error message if the process handle cannot be obtained
        std::cerr << "Failed to open process. Error: " << GetLastError() << std::endl;
        return;
    }

    // Initialize the power throttling state structure
    PROCESS_POWER_THROTTLING_STATE powerThrottlingState = {};
    powerThrottlingState.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
    powerThrottlingState.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
    powerThrottlingState.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;

    // Apply the power throttling settings to the target process
    if (!SetProcessInformation(hProcess, ProcessPowerThrottling, &powerThrottlingState, sizeof(powerThrottlingState))) {
        // Log an error message if setting the power throttling information fails
        std::cerr << "Failed to enable power throttling for PID " << pid << ". Error: " << GetLastError() << std::endl;
    } else {
        // Log a success message if the power throttling settings are applied successfully
        std::cout << "Power throttling enabled successfully for PID " << pid << "." << std::endl;
    }

    // Close the handle to the process to free resources
    CloseHandle(hProcess);
}

int main() {
    // Define the process ID for which EcoQoS should be enabled
    DWORD pid = 16196;

    // Enable EcoQoS for the specified process ID
    enable_ecoqos_for_pid(pid);

    // Print a message indicating the end of the program
    std::cout << "Hello World!" << std::endl;

    // Return 0 to indicate successful program termination
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.