main.cpp:25:25: error: no matching function for call to 'PdhAddCounterW' pdh.h:1348:37: note: expanded from macro 'PdhAddCounter' pdh.h:337:1: note: candidate function not viable: no known conversion from 'const char [42]' to 'LPCWSTR' (aka 'const wchar_t *') for 2nd argument
时间: 2024-03-07 11:50:17 浏览: 139
这个错误提示显示 PdhAddCounter 函数的第二个参数需要传递一个 LPCWSTR 类型的字符串,但是您传递的是一个 const char[] 类型的字符串,需要将其转换为 LPCWSTR 类型的字符串。
您可以使用 MultiByteToWideChar() 函数将 const char[] 类型的字符串转换为 LPCWSTR 类型的字符串,示例代码如下:
```C++
const char* counterPath = "\\Processor(_Total)\\% Processor Time";
int counterPathLength = strlen(counterPath) + 1;
int counterPathWideLength = MultiByteToWideChar(CP_ACP, 0, counterPath, counterPathLength, NULL, 0);
LPCWSTR counterPathWide = new WCHAR[counterPathWideLength];
MultiByteToWideChar(CP_ACP, 0, counterPath, counterPathLength, (LPWSTR)counterPathWide, counterPathWideLength);
PDH_HCOUNTER hCounter;
PdhAddCounter(hQuery, counterPathWide, 0, &hCounter);
// 使用完毕后需要释放内存
delete[] counterPathWide;
```
这里将 const char[] 类型的字符串 counterPath 转换为 LPCWSTR 类型的字符串 counterPathWide,并使用 PdhAddCounter() 函数添加计数器。注意,使用完毕后需要释放 counterPathWide 的内存。
阅读全文