bool ProcessHelper::GetProcessPSN(const QString& path, ProcessSerialNumber& psn) {
    psn.highLongOfPSN = kNoProcess;
    psn.lowLongOfPSN = kNoProcess;

    char buffer[400];
    CFStringRef key = CFSTR("CFBundleExecutable");

    OSStatus status = GetNextProcess(&psn);
    while (status == 0){
        CFDictionaryRef processInfoDict =
            ProcessInformationCopyDictionary(&psn, kProcessDictionaryIncludeAllInformationMask);

        CFStringRef value = (CFStringRef) CFDictionaryGetValue(processInfoDict, key);

        CFIndex length = CFStringGetLength(value);
        CFIndex maxSize =
            CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
        CFStringGetCString(value, buffer, maxSize, kCFStringEncodingUTF8);

        QString curBundle;
        curBundle += buffer;
        if (path.compare(curBundle) == 0)
            return true;

        status = GetNextProcess(&psn);
    }

    return false;
}
void get_my_path(char s[PATH_MAX])
{
    ProcessSerialNumber psn;
    GetCurrentProcess(&psn);
    CFDictionaryRef dict;
    dict = ProcessInformationCopyDictionary(&psn, 0xffffffff);
    CFStringRef value = (CFStringRef)CFDictionaryGetValue(dict,
                CFSTR("CFBundleExecutable"));
    CFStringGetCString(value, s, PATH_MAX - 1, kCFStringEncodingUTF8);
}
Esempio n. 3
0
CFStringRef WBProcessCopyBundleIdentifier(ProcessSerialNumber *psn) {
  CFStringRef identifier = NULL;
  CFDictionaryRef infos = ProcessInformationCopyDictionary(psn, (UInt32)kProcessDictionaryIncludeAllInformationMask);
  if (infos) {
    identifier = CFDictionaryGetValue(infos, kCFBundleIdentifierKey);
    if (identifier)
      CFRetain(identifier);
    CFRelease(infos);
  }
  return identifier;
}
Esempio n. 4
0
ProcessSerialNumber WBProcessGetProcessWithProperty(CFStringRef property, CFPropertyListRef value) {
  ProcessSerialNumber serialNumber = {kNoProcess, kNoProcess};
  if (!value)
    return serialNumber;

  while (procNotFound != GetNextProcess(&serialNumber))  {
    CFDictionaryRef info = ProcessInformationCopyDictionary(&serialNumber, (UInt32)kProcessDictionaryIncludeAllInformationMask);
    if (info) {
      CFPropertyListRef procValue = CFDictionaryGetValue(info, property);
      if (procValue && (CFEqual(procValue, value))) {
        CFRelease(info);
        break;
      }
      CFRelease(info);
    }
  }
  return serialNumber;
}
Esempio n. 5
0
char*
bufprint_app_dir(char*  buff, char*  end)
{
    ProcessSerialNumber psn;
    CFDictionaryRef     dict;
    CFStringRef         value;
    char                s[PATH_MAX];
    char*               x;

    GetCurrentProcess(&psn);
    dict  = ProcessInformationCopyDictionary(&psn, 0xffffffff);
    value = (CFStringRef)CFDictionaryGetValue(dict,
                                             CFSTR("CFBundleExecutable"));
    CFStringGetCString(value, s, PATH_MAX - 1, kCFStringEncodingUTF8);
    x = strrchr(s, '/');
    if (x == 0) goto fail;
    *x = 0;

    return bufprint(buff, end, "%s", s);
fail:
    fprintf(stderr,"cannot locate application directory\n");
    exit(1);
    return end;
}
Esempio n. 6
0
CFDictionaryRef getProcessInfo(CFStringRef targetCreator, CFStringRef targetName, CFStringRef targetIdentifier) {
	/* find an applicarion process specified by theSignature(creator type) from runnning process.
	if target application can be found, get information of the process and return as a result
	*/
	CFIndex nKey = 3;
	CFMutableArrayRef keyList = CFArrayCreateMutable(NULL,nKey,&kCFTypeArrayCallBacks);
	CFArrayAppendValue(keyList, CFSTR("FileCreator"));
	CFArrayAppendValue(keyList, CFSTR("CFBundleIdentifier"));
	CFArrayAppendValue(keyList, CFSTR("CFBundleName"));

	CFIndex firstKeyIndex;
	
	if (targetCreator != NULL) {
		firstKeyIndex = 0;
	}
	else if (targetIdentifier != NULL) {
		firstKeyIndex = 1;
	}
	else {
		firstKeyIndex = 2;
	}
	if (targetCreator == NULL) targetCreator = (CFStringRef)kCFNull;
	if (targetName == NULL) targetName = (CFStringRef)kCFNull;
	if (targetIdentifier == NULL) targetIdentifier = (CFStringRef)kCFNull;

	CFMutableArrayRef valueList = CFArrayCreateMutable(NULL, nKey,&kCFTypeArrayCallBacks);
	CFArrayAppendValue(valueList, targetCreator);
	CFArrayAppendValue(valueList, targetIdentifier);
	CFArrayAppendValue(valueList, targetName);
	
	CFStringRef dictKey = CFArrayGetValueAtIndex(keyList, firstKeyIndex);
	CFArrayRemoveValueAtIndex(keyList, firstKeyIndex);
	CFStringRef targetValue = CFArrayGetValueAtIndex(valueList, firstKeyIndex);
	CFArrayRemoveValueAtIndex(valueList, firstKeyIndex);
	
	Boolean isFound = false;
	ProcessSerialNumber psn = {kNoProcess, kNoProcess};
	CFDictionaryRef pDict = NULL;
	CFComparisonResult isSameStr;
	
	OSErr err = GetNextProcess(&psn);
	while( err == noErr) {
		pDict = ProcessInformationCopyDictionary(&psn, kProcessDictionaryIncludeAllInformationMask);
		CFStringRef dictValue = CFDictionaryGetValue(pDict, dictKey);
		if (dictValue != NULL) {
			isSameStr = CFStringCompare(dictValue,targetValue,0);

			if (isSameStr == kCFCompareEqualTo){
				isFound = true;
				for (CFIndex i=0; i < 2; i++) {
					CFStringRef secondValue = CFArrayGetValueAtIndex(valueList,i);
					if (secondValue != (CFStringRef)kCFNull) {
						CFStringRef nextKey = CFArrayGetValueAtIndex(keyList, i);
						dictValue = CFDictionaryGetValue(pDict,nextKey);
						isSameStr = CFStringCompare(dictValue,secondValue,0);
						if (isSameStr != kCFCompareEqualTo) {
							isFound = false;
							break;
						}
					}
				}
				
				if (isFound) break;
			}
		}
		
		CFRelease(pDict);
		err = GetNextProcess (&psn);
	}
	
	CFRelease(keyList);
	CFRelease(valueList);
	
	if (isFound) {
#if useLog	
		printf("getProcessInfo found PSN high:%d, low:%d \n", psn.highLongOfPSN, psn.lowLongOfPSN);
#endif
        CFMutableDictionaryRef p_dict_psn = CFDictionaryCreateMutableCopy (NULL, 0, pDict);
		CFDictionaryAddValue(p_dict_psn, CFSTR("PSNLow"), 
									CFNumberCreate(NULL, kCFNumberSInt32Type, &(psn.lowLongOfPSN)));
		CFDictionaryAddValue(p_dict_psn, CFSTR("PSNHigh"), 
									CFNumberCreate(NULL, kCFNumberSInt32Type, &(psn.highLongOfPSN)));
		CFRelease(pDict);
		return p_dict_psn;
	}
	else{
		return nil;
	}
}