c++ - 操作系统-X : check if process runs from bundle or not

标签 c++ objective-c macos cocoa

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

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

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 将 bundle 从可执行文件名称中取出的方法,但我更喜欢在 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]’

从包标识符,可以轻松访问包本身(在 cocoa 中):

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

感谢您的帮助!

最佳答案

如果我正确理解你的问题,你只想 NSRunningApplication .

该类包含 bundleIdentifierbundleURL 属性。

关于c++ - 操作系统-X : check if process runs from bundle or not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32035313/

相关文章:

c++ - 存在用户定义的移动构造函数/赋值时的隐式复制构造函数

c++ - 将内存中的 sqlite 数据库转换为 blob/char 数组

ios - 如何在 animateWithDuration block 期间利用 UIView 的变化值?

objective-c - 带渐变填充的 Delaunay 三角剖分?

swift - NSButton 以编程方式推上推下类型

c++ - 模板化助手函数调用具有特定功能的特定模板化子项

c++ - 如何通过终端 Mac 编译 C++ 程序

ios - setImage 总是拾取 1x 图像

xcode - 列出 iCloud 中可用的文件

eclipse - Unresolved inclusion : <iostream> in eclipse on mac