ASPiK SDK
getpluginbundle.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __getpluginbundle__
6 #define __getpluginbundle__
7 
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
11 #if MAC
12 #include <dlfcn.h>
13 #include <CoreFoundation/CFBundle.h>
14 #include <string>
15 
16 //-----------------------------------------------------------------------------
17 static CFBundleRef GetPluginBundle ()
18 {
19  CFBundleRef pluginBundle = 0;
20  Dl_info info;
21  if (dladdr ((const void*)GetPluginBundle, &info))
22  {
23  if (info.dli_fname)
24  {
25  std::string name;
26  name.assign (info.dli_fname);
27  for (int i = 0; i < 3; i++)
28  {
29  size_t delPos = name.find_last_of ('/');
30  if (delPos == std::string::npos)
31  {
32  fprintf (stdout, "Could not determine bundle location.\n");
33  return 0; // unexpected
34  }
35  name.erase (delPos, name.length () - delPos);
36  }
37  CFURLRef bundleUrl = CFURLCreateFromFileSystemRepresentation (0, (const UInt8*)name.c_str (), name.length (), true);
38  if (bundleUrl)
39  {
40  pluginBundle = CFBundleCreate (0, bundleUrl);
41  CFRelease (bundleUrl);
42  }
43  }
44  }
45  return pluginBundle;
46 }
47 
48 #endif
49 
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 
54 #endif // __getpluginbundle__