PDA

View Full Version : Path of library / GetModuleHandle() equivalent


Logodae
2006.05.11, 06:58 PM
I'm in the process of porting a Windows plugin to the Mac, and I'm looking for the equivalent of GetModuleHandle(). Basically, I need to be able to get the path of the plugin, which is a dynamically linked library, at runtime. If anyone could point me in the right direction, I'd appreciate it.

-Morgan

OneSadCookie
2006.05.12, 01:26 AM
CFBundleCopyBundleURL

Logodae
2006.05.12, 02:51 PM
Okay, but on what?

My original solution was to use CFBundleGetMainBundle() and then CFBundleCopyBundleURL(), but that only gets me the application path, not the plugin path...

Edit: CFBundleGetBundleWithIdentifier() looks promising...

Edit2: Or not. A library isn't actually a bundle, I suspect.

Thanks anyway.

OneSadCookie
2006.05.12, 06:32 PM
What kind of a library? You can use CFBundleGetBundleWithIdentifier to get the system frameworks, and CFBundleCreate + CFBundleLoadExecutable to load a plug-in. CFBundleCopyBundleURL is the function you want.

OneSadCookie
2006.05.12, 06:40 PM
Oh, I see -- your code is the plug-in, loaded by some host application you have no control over.

Hmm.

How is your plug-in structured? You make it sound as if it's a "naked" executable file, which would be odd for a native Mac app...

OneSadCookie
2006.05.12, 06:59 PM
This kind of works:

main.c:

#include <dlfcn.h>

int main(int argc, const char *argv[]) {
void *handle = dlopen("test.bundle", RTLD_LAZY);
void (*test)(void) = dlsym(handle, "test");
test();
dlclose(handle);
return 0;
}


test.c:

#include <stdio.h>
#include <dlfcn.h>

void test(void) {
Dl_info info;
int result = dladdr(test, &info);

printf("dladdr returns %d; gives %s\n",
result,
info.dli_fname);
}


gcc main.c
gcc -bundle test.c -o test.bundle
./a.out

But it returns the path that the plug-in was loaded with (in this case, a relative one).