Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

GetExecutablePath

By: LordAro on Sep 18th, 2012  |  syntax: C++  |  size: 0.74 KB  |  views: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }