如何在Mac OS上的C/C++/Objective-C中找到SystemUIServer进程的PID
How can I find out the PID of the SystemUIServer process in C/C++/Objective-C on Mac OS?
我需要找出Mac OS上SystemUIServer进程的pid,以便将其移交给AXUIElementCreateApplication(pid);
在shell上,这很容易通过ps实现,但我如何在C/C++或Objective-C中实现呢?
我会检查所有正在运行的进程。
pid_t resultPid = -1;
NSArray *runningApplications = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in runningApplications) {
pid_t pid = [app processIdentifier];
if (pid != ((pid_t)-1)) {
AXUIElementRef appl = AXUIElementCreateApplication(pid);
id result = nil;
if(AXUIElementCopyAttributeValue(appl, (CFStringRef)NSAccessibilityTitleAttribute, (void *)&result) == kAXErrorSuccess) {
if([((NSString*)result) isEqualToString:@"SystemUIServer"]){
resultPid = pid;
break;
}
}
}
}
您还可以使用Apple的UIElementUtilities(它有助于管理AXUIElementRef实例)来获取进程的名称。
感谢Sudo、雅虎和谷歌,我找到了以下解决方案:
#include <libproc.h>
int getPid(const char* processname)
{
pid_t resultPid = -1;
NSArray *runningApplications = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in runningApplications) {
pid_t pid = [app processIdentifier];
if (pid != ((pid_t)-1)) {
char nameBuffer[512];
proc_name(pid, nameBuffer, sizeof(nameBuffer));
if(!strcmp(processname,nameBuffer)) {
resultPid=pid;
break;
}
}
}
return resultPid;
}
相关文章:
- 使用 VID、PID、+SN 查找 USB 端口号
- 对于四轴飞行器,PID中I控制器的理想值应该是多少
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- 两个进程可以通过跟踪附加到同一个 PID 吗?
- 如何获取进程的整数PID?
- Linux:获取孙子的pid C++
- 在 C 中获取背景外壳命令的 PID
- 通过 pid 窗口判断进程是否存在
- PID 不使用 fork() 定义类型
- 我想获取我在C++中创建的每个进程的 PID,并在特定时间后终止每个进程
- 从某些 PID C++ 开始
- MacOS - 如何使用 C/C++ 获取具有 PID 的进程用户/所有者
- 如何在Mac OSX C 中找到任何过程的PID
- (LINX-IPC 协议)如何确定具有 PID 的进程是否在C++中运行
- 如何使用 /dev/xxx 路径中的C++获取 Ubuntu 中设备的 USB VID、PID 和序列号
- 使用提升从 pid 获取"binary file"/"executable"的路径?
- C Windows-如何从其PID(错误)获取过程路径
- 将PID从孩子转移到父母
- Linux获取通过execl脚本启动的程序的PID
- 如何在Mac OS上的C/C++/Objective-C中找到SystemUIServer进程的PID