Advertisement
LordAro

GetExecutablePath

Sep 18th, 2012
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. /**
  2.  * Get the location of the executable
  3.  * @return the location of the executable, as a string.
  4.  */
  5. const char *GetExecutableLocation()
  6. {
  7.    const char *path;
  8.    char buf[1024];
  9.    
  10.    #if defined (WIN32) || defined (WIN64)
  11.       GetModuleFileName(path, &size);
  12.    #elif defined (__APPLE__)
  13.       _NSGetExecutablePath(path, &size);
  14.    #elif defined(UNIX)
  15.       if (readlink("/proc/self/exe", buf, sizeof(buf)) == -1) path = buf;
  16.    #elif defined(__FreeBSD__)
  17.       int mib[4];
  18.       mib[0] = CTL_KERN;
  19.       mib[1] = KERN_PROC;
  20.       mib[2] = KERN_PROC_PATHNAME;
  21.       mib[3] = -1;
  22.       sysctl(mib, 4, buf, sizeof(buf), NULL, 0);
  23.       path = buf;
  24.    #elif defined(SUNOS)
  25.       path = getexecname();
  26.    #endif
  27.    return path;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement