使用性能计数器获取正常运行时间的权限问题

发布于 2024-11-15 15:08:57 字数 968 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-11-22 15:08:57

因此,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:

Only the administrator of the computer or users in the Performance Logs User Group can log and view counter data. Users in the Administrator group can log and view counter data only if the tool they use to log and view counter data is started from a Command Prompt window that is opened with Run as administrator.... Users in the Performance Monitoring Users group can view counter data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文