1

I created a C++ library that I compile as a .framework so other apps can call into it. I'd like to get the path of the calling .app file from within the library. How can I do this?

In Windows, I simply call GetModuleFileName with the processID as NULL and it returns the parent process. I want to do the equivalent on Mac.

Thanks!

simon.d
  • 2,471
  • 3
  • 33
  • 51

1 Answers1

1

You can use sysctl(CTL_KERN, KERN_PROC ...) as documented on this MacOSX Guru page.

int getprocessname( pid_t inPID, char *outName, size_t inMaxLen)
{
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, inPID };

if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
    return -1 ;
else
    strncpy(outName, info.kp_proc.p_comm, inMaxLen) ;

return 0    ;
}

See also this code which retrieves all the structures you need.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • This is good but I'm only getting the process name. How can I get the process's full path? The reason I ask is because I want to calculate a checksum on it. – simon.d Oct 14 '11 at 18:27
  • Got my answer here: http://stackoverflow.com/questions/799679/programatically-retrieving-the-absolute-path-of-an-os-x-command-line-app – simon.d Oct 18 '11 at 19:18