我想检查给定的远程进程可执行文件是 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 .
该类包含 bundleIdentifier
和 bundleURL
属性。
关于c++ - 操作系统-X : check if process runs from bundle or not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32035313/