Skip to content

Instantly share code, notes, and snippets.

@nickdowell
Last active October 2, 2015 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickdowell/2236186 to your computer and use it in GitHub Desktop.
Save nickdowell/2236186 to your computer and use it in GitHub Desktop.
GetPluginBundle()
#include <CoreFoundation/CoreFoundation.h>
#include <dlfcn.h>
static CFBundleRef GetPluginBundle()
{
static CFBundleRef bundle;
if (bundle) {
return bundle;
}
Dl_info dl_info = {};
if (dladdr((void *)(&GetPluginBundle), &dl_info) == 0) {
return NULL;
}
char *end = strstr(dl_info.dli_fname, "/Contents/MacOS/");
if (!end) {
return NULL;
}
CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)dl_info.dli_fname, end - dl_info.dli_fname, TRUE);
if (!url) {
return NULL;
}
bundle = CFBundleCreate(kCFAllocatorDefault, url);
CFRelease(url);
return bundle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment