在编写跨平台模块时,我需要一种方法来检索模块目录的路径(因此我可以访问模块文件夹中的文件,如配置和资源)。我的模块,当前编译为一个包,由任意进程加载。在Windows中很容易获得模块的路径,如下所示:
GetModuleFileName(GetModuleHandle(0), buf, size); // copies the path of the current DLL into buf
我还没能在OSX上找到类似的方法。我试过_NSGetExecutablePath()
但它只检索主程序的路径。还尝试了the method here。
但是,它也获得了主要可执行文件的路径。在OSx上执行此操作的常用方法是什么?
此致
编辑:
格雷迪播放器向我展示了方式:)我使用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;
}