3
CFBundleCopyExecutableURL函數有時會返回一個絕對URL,有時會返回絕對URL。如果在CFBundleCopyExecutableURL之前調用CFBundleCopyExecutableArchitectures,那麼url是絕對的(爲什麼?) 。如何強制這個函數總是返回絕對URL? 謝謝。CFBundleCopyExecutableURL不返回絕對URL
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;
}
謝謝,「文本」類型是我的字符串類,它的名字就像javascript規則。 –
嗯,也許我應該將其命名爲text_t(通過c規則,如wchar_t,char16_t,...)。 –