某些进程的 GetProcessForPID 错误 -600
我正在尝试访问一个进程,以便稍后使用 CGEventPostToPSN 发送密钥。但我的问题是我无法获取正在运行的进程。我正在使用以下代码:
pid_t pid = GetPIDForProcessName("myprocess");
NSLog(@"PID: %d", pid);
ProcessSerialNumber psn = { 0, pid };
OSStatus status = GetProcessForPID(pid, &psn);
if (status != noErr) {
NSLog(@"OSStatus KO PID: %d %d %d", status, noErr, pid);
}else{
NSLog(@"OSStatus OK PID: %d %d %d", status, noErr, pid);
CGEventRef keyDown;
keyDown = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, TRUE);
CGEventRef keyUp;
keyUp = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, FALSE);
CGEventPostToPSN(&psn, keyDown);
CFRelease(keyDown);
CGEventPostToPSN(&psn, keyUp);
CFRelease(keyUp);
}
我正在使用“GetPID.h”类来获取整数形式的 PID,这似乎工作正常。但我在 GetPIDForProcessName 状态中收到错误 600“没有具有指定进程序列号的合格进程”。
我已经测试了一些进程,此代码可以工作,但不能与其他进程一起工作。我没有非常明显的进程之间的区别,
非常感谢您的任何建议。
I am trying to access a process to send a key with CGEventPostToPSN later. But my problem is that I can not get the running process. I am using the following code:
pid_t pid = GetPIDForProcessName("myprocess");
NSLog(@"PID: %d", pid);
ProcessSerialNumber psn = { 0, pid };
OSStatus status = GetProcessForPID(pid, &psn);
if (status != noErr) {
NSLog(@"OSStatus KO PID: %d %d %d", status, noErr, pid);
}else{
NSLog(@"OSStatus OK PID: %d %d %d", status, noErr, pid);
CGEventRef keyDown;
keyDown = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, TRUE);
CGEventRef keyUp;
keyUp = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, FALSE);
CGEventPostToPSN(&psn, keyDown);
CFRelease(keyDown);
CGEventPostToPSN(&psn, keyUp);
CFRelease(keyUp);
}
I am using the class "GetPID.h" to get the PID as an integer, and this seems to work correctly. But I get an error 600 "No eligible process with specified process serial number" in GetPIDForProcessName status.
I have tested some processes and this code works, but does not work with others. I have no very clear difference between processes can be
Thank you very much for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并非每个进程都有进程序列号,只有应用程序有进程序列号。 GetProcessForPID 的文档没有这么说,但请参阅技术问答 QA1123。
编辑添加:这是该文档的引用......
Not every process has a process serial number, just the apps. The docs for
GetProcessForPID
fail to say that, but see Technical Q&A QA1123.Edit to add: Here's a quote from that document...
您能给我们举一些有效和无效的进程名称示例吗?进程的名称是任意的,可能与应用程序的人类可读名称不匹配。例如,Time Machine 作为“
backupd
”在后台运行。如果您告诉我们您想要实现的目标是什么,我们也许可以建议另一种方法来实现它。
Can you give us examples of process names that do and don't work? The name of a process is arbitrary and might not match the human-readable name of the application. For instance, Time Machine runs in the background as "
backupd
".If you tell us what it is you're trying to accomplish, we might be able to suggest an alternate way to accomplish it.