windows 用户gpu使用率代码 cpp
时间: 2023-09-25 09:06:53 浏览: 586
PaddleOCR2.7+Qt5
### 回答1:
你好! 下面是一段使用 C++ 获取 Windows 用户 GPU 使用率的代码:
```cpp
#include <Windows.h>
#include <stdio.h>
#include <Pdh.h>
#include <PdhMsg.h>
#pragma comment(lib, "pdh.lib")
int main()
{
PDH_HQUERY query;
PDH_HCOUNTER counter;
PDH_STATUS status;
PDH_FMT_COUNTERVALUE counterValue;
// 创建查询和计数器
status = PdhOpenQuery(NULL, 0, &query);
if (status != ERROR_SUCCESS)
{
printf("PdhOpenQuery failed with status %ld\n", status);
return 1;
}
status = PdhAddCounter(query, "\\GPU Engine\\Utilization Percent", 0, &counter);
if (status != ERROR_SUCCESS)
{
printf("PdhAddCounter failed with status %ld\n", status);
return 1;
}
// 获取计数器的当前值
status = PdhCollectQueryData(query);
if (status != ERROR_SUCCESS)
{
printf("PdhCollectQueryData failed with status %ld\n", status);
return 1;
}
status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, NULL, &counterValue);
if (status != ERROR_SUCCESS)
{
printf("PdhGetFormattedCounterValue failed with status %ld\n", status);
return 1;
}
printf("GPU utilization: %f%%\n", counterValue.doubleValue);
// 关闭查询和计数器
PdhCloseQuery(query);
return 0;
}
```
这段代码使用了 Windows Performance Data Helper (PDH) 库来获取 GPU 使用率的信息。
需要注意的是,这段代码只能在 Windows 系统上运行。此外,需要在编译时添加 pdh.lib 库的链接。
希望这段代码能帮到你!
### 回答2:
要获取Windows用户GPU使用率的代码,你可以使用以下CPP代码:
```cpp
#include <iostream>
#include <windows.h>
#include <Ntddvdeo.h>
typedef struct _ADAPTER_PERFORMANCE_INFORMATION {
LONGLONG Stardate;
LONGLONG PerformanceCount;
LONGLONG PerformanceFrequency;
} ADAPTER_PERFORMANCE_INFORMATION, *PADAPTER_PERFORMANCE_INFORMATION;
int main() {
HMODULE ntdll = LoadLibraryA("ntdll.dll");
if (ntdll == NULL) {
std::cout << "Failed to load ntdll.dll" << std::endl;
return 1;
}
typedef NTSTATUS (WINAPI * NtQueryAdapterPerformanceCounterFn)(
_Inout_ PADAPTER_PERFORMANCE_INFORMATION PerformanceInformation);
NtQueryAdapterPerformanceCounterFn NtQueryAdapterPerformanceCounter = (NtQueryAdapterPerformanceCounterFn)GetProcAddress(ntdll, "NtQueryAdapterPerformanceCounter");
if (NtQueryAdapterPerformanceCounter == NULL) {
std::cout << "Failed to get NtQueryAdapterPerformanceCounter function" << std::endl;
FreeLibrary(ntdll);
return 1;
}
ADAPTER_PERFORMANCE_INFORMATION info = { 0 };
NTSTATUS status = NtQueryAdapterPerformanceCounter(&info);
if (status != 0) {
std::cout << "Failed to get GPU performance counter" << std::endl;
FreeLibrary(ntdll);
return 1;
}
double usage = static_cast<double>(info.PerformanceCount) / static_cast<double>(info.PerformanceFrequency) * 100.0;
std::cout << "GPU usage: " << usage << "%" << std::endl;
FreeLibrary(ntdll);
return 0;
}
```
这段代码将加载`ntdll.dll`并通过`NtQueryAdapterPerformanceCounter`函数获取GPU性能计数器信息。然后通过计算性能计数器和性能频率的比值来获取GPU使用率,并将结果以百分比的形式输出。请确保你的系统上有合适的GPU驱动和安装了Windows Driver Kit。
### 回答3:
要获取Windows用户GPU使用率的代码,可以使用Windows API函数来实现。以下是一个示例代码:
```cpp
#include <iostream>
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
// 获取GPU使用率
double GetGpuUsage()
{
PDH_HQUERY query;
PDH_HCOUNTER counter;
PDH_FMT_COUNTERVALUE counterVal;
// 初始化 PDH(Performance Data Helper)查询
PdhOpenQuery(NULL, NULL, &query);
PdhAddCounter(query, L"\\GPU Engine(0)\\Utilization Percentage", NULL, &counter);
// 收集实时数据
PdhCollectQueryData(query);
PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, NULL, &counterVal);
// 关闭 PDH 查询
PdhCloseQuery(query);
return counterVal.doubleValue;
}
int main()
{
double gpuUsage = GetGpuUsage();
std::cout << "GPU使用率: " << gpuUsage << "%" << std::endl;
return 0;
}
```
使用Windows API中的Performance Data Helper(PDH)函数,可以通过指定特定的计数器路径来获取GPU的使用率数据。在示例代码中,我们使用了“\GPU Engine(0)\Utilization Percentage”计数器路径,其中“GPU Engine(0)”表示第一个GPU引擎,“Utilization Percentage”表示使用率百分比。通过调用 `PdhGetFormattedCounterValue` 函数,我们可以获取GPU使用率的值。在主函数中,我们输出了获取到的GPU使用率。
请注意,此代码仅适用于基于Windows的系统,并假设GPU驱动程序已正确安装。
阅读全文