Getting path of a process
Getting path of a process
- Subject: Getting path of a process
- From: David Remahl <email@hidden>
- Date: Fri, 20 Sep 2002 13:47:24 +0200
Dear list,
I need to find the path of a process, actually an application, very early
after it got its process id.
My first approach was using the NSWorkspace method -launchedApplications,
but at that early point the newly launched application had not appeared in
that list yet.
I read this article: <
http://developer.apple.com/qa/qa2001/qa1123.html>. I
used the sample code for the function GetBSDProcessList(). I found the newly
launched process.
I then tried using the functions in Processes.h to get the PSN of the
process and get the path from that. It turned out that the PSN hadn't been
assigned either. NSWorkspace is probably layered on top of Processes.h.
Therefore I could not use GetNextProcess() to enumerate the apps either.
Back to the BSD approach. I had a look at the source for ps. Ps uses sysctl
with a mib shown below to get the list of arguments. Unfortunately, I cannot
decipher how the returned arglist is constructed from the ps source
(advanced user commands tarball in the darwin repository)...KERN_PROCARGS is
the only KERN_ define which isn't documented with even a single word in
sysctl.h!
int mib[4];
char *arguments;
int arguments_size = 4096;
int end_argc;
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS;
mib[2] = procList[i].kp_proc.p_pid;
mib[3] = 0;
arguments = (char *) malloc(arguments_size);
if (sysctl(mib, 3, arguments, &arguments_size, NULL, 0) < 0) {
NSLog(@"No args found");
}
else
{
end_argc = &arguments[arguments_size];
NSLog(@"%s",&arguments[1]);
}
The above outputs an empty string for about half of the running processes,
and "No args found" for the other half. If no args are found i should fall
back on using the command name.
Does anyone know of a better way to get the full path of a process? If the
started application is TextEdit, then i would like to get
/Applications/TextEdit.app, or
/Applications/TextEdit.app/Contents/MacOS/TextEdit
/ TIA, David Remahl
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.