使用性能计数器获取正常运行时间的权限问题
我正在尝试使用 C++ 中的性能计数器读取系统正常运行时间。我想至少支持 XP 和 Windows 7。
以下代码在 Windows XP 上运行良好.....但
HQUERY hQuery; HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE Value;
int ret = 0;
if (PdhOpenQuery(NULL, 0, &hQuery) == ERROR_SUCCESS) {
if ((status = PdhAddCounter(hQuery, queryURI, 0, &hCounter)) == ERROR_SUCCESS) {
if ((status = PdhCollectQueryData(hQuery)) == ERROR_SUCCESS) {
if ((status = PdhGetFormattedCounterValue(hCounter, PDH_FMT_LARGE, NULL, &Value)) == ERROR_SUCCESS) {
ret = (DWORD)(Value.largeValue);
}
}
PdhRemoveCounter(hCounter);
}
PdhCloseQuery(hQuery);
}
return ret;
在 Windows 7 上失败。具体来说, PdhCollectQueryData 返回 PDH_NO_DATA 无论我是否以管理员身份运行。
如何获取 Windows 7 和 XP 上的系统正常运行时间?我预计时间会比 GetTickCount 的 49 天溢出时间长得多,而且如果可能的话,我宁愿不为 XP 提供单独的 PDH 版本,也不为 7 提供单独的 GetTickCount64 版本...
I'm trying to read the system uptime using performance counters in C++. I want to support both XP and Windows 7 at minimum.
The following code works fine on Windows XP...
HQUERY hQuery; HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE Value;
int ret = 0;
if (PdhOpenQuery(NULL, 0, &hQuery) == ERROR_SUCCESS) {
if ((status = PdhAddCounter(hQuery, queryURI, 0, &hCounter)) == ERROR_SUCCESS) {
if ((status = PdhCollectQueryData(hQuery)) == ERROR_SUCCESS) {
if ((status = PdhGetFormattedCounterValue(hCounter, PDH_FMT_LARGE, NULL, &Value)) == ERROR_SUCCESS) {
ret = (DWORD)(Value.largeValue);
}
}
PdhRemoveCounter(hCounter);
}
PdhCloseQuery(hQuery);
}
return ret;
..but it fails on Windows 7. Specifically, PdhCollectQueryData returns PDH_NO_DATA regardless of whether or not i run as administrator.
How can i get the system uptime on both Windows 7 and XP? I expect the times to be much larger than the 49-day overflow of GetTickCount, and i would rather not have separate PDH versions for XP and GetTickCount64 versions for 7 if at all possible...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,PdhCollectQueryData 的帮助表明,如果执行查询的进程缺少允许查询的适当提升令牌,则可以返回 PDH_NO_DATA。看看您是否可以准确检查进程本身已分配的用户权限,无论您是否以管理员身份登录。 Windows 7 对这个概念有很多细粒度,尤其是在 UAC 打开的情况下。使用操作系统创建的本地管理员帐户和使用操作系统创建的本地管理员帐户之间也存在区别。就帐户最终获得的权限而言,管理员组的成员,尽管我在性能计数器上没有遇到过特定的权限。
例如,尝试在该过程中明确“以管理员身份运行”,并确保您使用的管理员帐户确实具有该权限(从您的问题中我不确定您是否已经尝试过此操作)。尝试使用性能日志用户组中的用户帐户。尝试安装操作系统时创建的帐户。尝试关闭 UAC。希望这些能够帮助确定问题的根源。
来自有关该主题的 Microsoft 帮助 :
So the help for PdhCollectQueryData indicates that PDH_NO_DATA can be returned if the process doing the query lacks the appropriate elevated token to allow the query. See if you can check exactly what user permissions the process itself has been allocated, regardless of whether you are logged in as admin or not. Windows 7 has a lot of granularity to this concept, especially with UAC turned on. There can be a distinction also between the local Administrator account created with the OS & a member of the Administrators group in terms of what permissions the account ends up with, though I've not encountered a specific one on performance counters.
Try an explicit 'Run as administrator' on the process, for example, and ensure the administrator account you're using really does have that permission (I'm not sure from your question whether you have already tried this or not). Try a user account in the Performance Logs User Group. Try the account that was created when the OS was installed. Try with UAC off. These hopefully should help nail down the source of the problem.
From the Microsoft help on the subject: