Contribute  :  Calendar  :  Advanced Search  :  Site Statistics  :  Directory  :  Files to download  :  Links for developers  :  Privacy policy  :  Web Resources  :  Polls  
    MacOSX Guru Site Information, tips and tricks, FAQ for Mac OS X developers    
 Welcome to MacOSX Guru Site
 Wednesday, September 20 2017 @ 05:23 PM BST

Parent process name and process that launched me name ?

   
Unix under OS X FAQHow 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;
}
 

What's Related

Story Options

Parent process name and process that launched me name ? | 0 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
 Copyright © 2017 MacOSX Guru Site
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.07 seconds