Parent process name and process that launched me name ?

Thursday, June 29 2006 @ 07:50 AM BST

Contributed by: Admin

How to get parent process name and "process that launched me"' name ?

To get the parent process name in the Unix sense:


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	;
}
To get the "process that launched me" name:

NSString *parentProcessName(void)
{
   // Get our PSN
   OSStatus    err;
    ProcessSerialNumber    currPSN;
    err = GetCurrentProcess (&currPSN);
   if (!err) {
       // Get information about our process
       NSDictionary*    currDict = (NSDictionary*)ProcessInformationCopyDictionary (&currPSN, kProcessDictionaryIncludeAllInformationMask);
       
       // Get the PSN of the app that *launched* us.  Its not really the parent app, in the unix sense.
       long long    temp = [[currDict objectForKey:@"ParentPSN"] longLongValue];
       [currDict release];
       long long    hi = (temp >> 32) & 0x00000000FFFFFFFFLL;
       long long    lo = (temp >> 0) & 0x00000000FFFFFFFFLL;
       ProcessSerialNumber    parentPSN = {(unsigned long)hi, (unsigned long)lo};

       // Get info on the launching process
       NSDictionary*    parentDict = (NSDictionary*)ProcessInformationCopyDictionary (&parentPSN, kProcessDictionaryIncludeAllInformationMask);
       
       // Test the creator code of the launching app
       NSString *parentName = [[parentDict objectForKey: kIOBundleNameKey] retain];
       [parentDict release];

       return [parentName autorelease] ;
   }
   
   return nil;
}

0 comments



http://www.macosxguru.net/article.php?story=20060629075017862