开发者

Know which Mac process is loading my library framework

开发者 https://www.devze.com 2023-04-12 17:59 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号