【问题标题】:OS-X: check if process runs from bundle or notOS-X:检查进程是否从包运行
【发布时间】:2015-08-18 04:35:01
【问题描述】:

我想检查给定的远程进程可执行文件是捆绑包的一部分还是只是一个独立的可执行文件。我的方法是一种非常直接的方法,它深深依赖于这样一个假设,即如果在 /[exec_name].app/ 子树中找到可执行文件,则它必须是它的包含包(见下文)

我希望找到相关的可可框架,涵盖所有可能的情况。

char path[MAXPATHLEN+1] = {0};
int ret = proc_pidinfo(pid, PROC_PIDPATHINFO, 0,&path, sizeof(path));
if (!ret) {
    std::string app_suffix(".app");
    unsigned long split_idx = path.rfind(app_suffix);
    if (split_idx != std::string::npos)
        cout << "found bundle : " << path.substr(0,split_idx+app_suffix.size());
    else 
       cout << " stand alone executable \n"; 
}

我还找到了另一种使用 applescript 将包从可执行文件名称中取出的方法,但我更喜欢在 cocoa 或 core-foundation 中找到等效项。

获取包标识符(苹果脚本):

osascript -e 'on run args
set output to {}
repeat with a in args
set end of output to id of app a
end
set text item delimiters to linefeed
output as text
end' Finder '[name of executable]’

从包标识符,很容易访问包本身(在可可中):

NSBundle* myBundle = [NSBundle bundleWithIdentifier:@"<bundle_identifier>"]

感谢您的帮助!

【问题讨论】:

    标签: c++ objective-c macos cocoa


    【解决方案1】:

    如果我正确理解您的问题,您只需要NSRunningApplication

    该类包括bundleIdentifierbundleURL 属性。

    【讨论】:

      【解决方案2】:

      在这些方面添加一些东西:

      if "your process here" using "api.bundle or whatever bundle you want to check"
      

      这只会检查一个进程

      【讨论】:

      • 您好,感谢您的回复。也许您可以编写确切的代码,以便我可以更好地理解您的方法。再次感谢
      猜你喜欢
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 2015-04-24
      • 2015-05-28
      • 2011-12-08
      相关资源
      最近更新 更多