Macos CbundlecopyexecutableURL不返回绝对URL
CFBundleCopyExecutableURL函数有时返回绝对URL,有时返回no。 如果在CbundlecopyexecutableURL之前调用CbundlecopyexecutableArchitectures,那么url是绝对的(为什么?)。如何强制此函数始终返回绝对URL? 谢谢Macos CbundlecopyexecutableURL不返回绝对URL,macos,cocoa,macos-carbon,Macos,Cocoa,Macos Carbon,CFBundleCopyExecutableURL函数有时返回绝对URL,有时返回no。 如果在CbundlecopyexecutableURL之前调用CbundlecopyexecutableArchitectures,那么url是绝对的(为什么?)。如何强制此函数始终返回绝对URL? 谢谢 在源代码中,添加这一额外的行,它引用 “CFURLCopyAbsoluteURL”将相对URL转换为绝对URL 您的代码中可能还应该有一些错误检查行(例如,在继续之前确保URL不为NULL,等等) 另外,
在源代码中,添加这一额外的行,它引用 “
CFURLCopyAbsoluteURL
”将相对URL转换为绝对URL
您的代码中可能还应该有一些错误检查行(例如,在继续之前确保URL不为NULL,等等)
另外,我想提到的一个样式是对象和变量声明(例如,您的“
text
”)通常是大写的(例如“CFStringRef
”、“NSString
”等)。参数和变量名以小写开头。此外,“文本
”令人困惑。只需将其命名为“CFStringRef
”。谢谢,“text”类型是my string类,它的名称类似于javascript规则。嗯,也许我应该将其命名为text\t(根据类似于wchar\t、char16\t等c规则)。
text getBundleExeutableUrl(text bundlePath)
{
CFURLRef url = CFURLCreateFromFileSystemRepresentation(0,
(char*)bundlePath.c_str(), bundlePath.length(), false);
CFBundleRef bundle = CFBundleCreate(0, url);
CFBundleCopyExecutableArchitectures(bundle); // this is necessary to get absolute path
CFURLRef exeUrl = CFBundleCopyExecutableURL(bundle);
CFStringRef srExe = CFURLCopyFileSystemPath(exeUrl, 0);
text bundleExe = srExe;
CFRelease(srExe);
CFRelease(exeUrl);
CFRelease(url);
CFRelease(bundle);
return bundleExe;
}
CFURLRef exeUrl = CFBundleCopyExecutableURL(bundle);
CFURLRef absoluteURL = CFURLCopyAbsoluteURL(exeURL);
CFStringRef srExe = CFURLCopyFileSystemPath(absoluteURL, 0);
CFRelease(absoluteURL); // don't forget to release what you create