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 fr开发者_如何学JAVAom 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!
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.
精彩评论