检索当前/自己的bundle的路径
Retrieving path to current/own bundle
在编写跨平台模块时,我需要一种方法来检索模块目录的路径(因此我可以访问模块文件夹中的文件,如配置和资源)。我的模块,目前被编译为一个bundle,可以被任意进程加载。获取模块的路径在windows中很容易完成,如下所示:
GetModuleFileName(GetModuleHandle(0), buf, size); // copies the path of the current DLL into buf
我还没能在OSX上找到类似的方法。我尝试了_NSGetExecutablePath()
,但它只检索主程序的路径。
但是,它也获得主可执行文件的路径。在OSx上这样做的常用方法是什么?
对
编辑:grady player向我展示了方法:)我使用NSBundle编译了以下代码:
/*
this could be anything apparantly
*/
@interface dummyObject : NSObject
- (void) dummyMethod;
@end
@implementation dummyObject
- (void) dummyMethod {
}
@end
int GetBundlePath(char * buf, int bufSize)
{
NSString * path = [[NSBundle bundleForClass:[dummyObject class]]bundlePath];
const char * intString = [path UTF8String];
int length = strlen(intString);
int smallestLength = length > bufSize ? bufSize : length;
memcpy(buf, intString, smallestLength);
[path release];
return smallestLength;
}
[[NSBundle bundleForClass:[thisClass class]]bundlePath];
将用于动态库…
或者如果你正在寻找你的主
[[NSBundle mainBundle] bundlePath];
也bundleURL…参见NSBundle Documentation
那么你的资源应该是相对于你的主包的。你可以使用
[[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponents:@[@"Contents",@"Resources"]];
相关文章:
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++A*算法并不总是在路径中具有目标节点
- 从函数角度看ID到文件路径的内部与外部映射
- boost xml parsingl将xml的路径作为变量发送
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 如何使用cppcheck处理半相对包含路径
- 在C++中设置基于操作系统的文件路径
- 基于编译器选项的编译二进制路径
- 按边长度递归搜索图中所有可行路径
- 使用变量值作为 PlaySound 中的路径
- 如何转换真实路径 CString c++
- 从 GUID 获取 USB 卷路径
- 查找 GCD:并非所有控制路径都返回值
- C++17 文件系统::remove_all 带有通配符路径
- 在带有尾部斜杠的路径上返回 std::filesystem::create_directories() 的值
- 获取当前正在运行的 exe 名称(不是路径)
- 如何从 Skia 路径几何体中获取网格?
- CMake 错误"源似乎不包含 CMakeLists.txt",路径/库连接问题
- 检索当前/自己的bundle的路径