【发布时间】:2011-04-02 15:12:53
【问题描述】:
我正在开发 Qt/C++ 应用程序,我需要简单的函数来检索我在 Mac OS X 上的用户空闲时间(以秒为单位)。
我找到了这个检测用户空闲时间的代码。
#include <IOKit/IOKitLib.h>
/**
Returns the number of seconds the machine has been idle or -1 if an error occurs.
The code is compatible with Tiger/10.4 and later (but not iOS).
*/
int64_t SystemIdleTime(void) {
int64_t idlesecs = -1;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj) {
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
idlesecs = (nanoseconds >> 30); // Divide by 10^9 to convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
}
如何将此代码转换为 C++,以用于我的 Qt/C++ 项目?
【问题讨论】:
-
有什么问题?这是直接的 C 语言,已经可以在 C++ 项目中使用。
-
我的链接器有问题:“_IOServiceGetMatchingServices”等符号未找到。它编译没有问题,但不想链接
标签: c++ macos qt operating-system