某些进程的 GetProcessForPID 错误 -600

发布于 2024-12-23 10:19:31 字数 975 浏览 0 评论 0原文

我正在尝试访问一个进程,以便稍后使用 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 技术交流群。

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

发布评论

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

评论(2

七色彩虹 2024-12-30 10:19:31

并非每个进程都有进程序列号,只有应用程序有进程序列号。 GetProcessForPID 的文档没有这么说,但请参阅技术问答 QA1123。

编辑添加:这是该文档的引用......

问:如何获取 Mac OS X 上所有进程的列表?

答:嗯,这取决于你如何定义“过程”。如果您想列出所有正在运行的应用程序,您应该使用Carbon Process Manager 例程GetNextProcess。这将返回所有应用程序进程的列表,包括在 Carbon、Cocoa 和 Classic 环境中运行的进程。但是,这不会返回非应用程序(守护程序)进程的列表。

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...

Q: How do I get a list of all processes on Mac OS X?

A: Well, that depends on how you define "process". If you want to list all of the running applications you should use the Carbon Process Manager routine GetNextProcess. This will return a list of all application processes, including those running in the Carbon, Cocoa, and Classic environments. However, this doesn't return a list of non-application (daemon) processes.

素手挽清风 2024-12-30 10:19:31

您能给我们举一些有效和无效的进程名称示例吗?进程的名称是任意的,可能与应用程序的人类可读名称不匹配。例如,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.

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