如何从根进程监视所有登录用户中进程的状态

时间:2012-06-06 07:51:55

标签: objective-c cocoa macos-carbon

我在根进程(守护程序)中使用Carbon Event Manager来监视用户进程(我的应用程序)的状态。但是我只获得kEventAppLaunched用户进程(我的应用程序),我在那里吃午根进程(守护进程)。如何在所有登录用户中获取kEventAppLaunched用户进程。

static OSStatus CarbonEventHandler(
                                   EventHandlerCallRef inHandlerCallRef, 
                                   EventRef            inEvent, 
                                   void *              inUserData
                                   )
{
    ProcessSerialNumber psn;
    CFStringRef processName = NULL ;

id lUserData;
if (inUserData) {
    lUserData= (id)inUserData;
}

OSStatus status; 
(void) GetEventParameter(
                         inEvent, 
                         kEventParamProcessID, 
                         typeProcessSerialNumber, 
                         NULL, 
                         sizeof(psn), 
                         NULL, 
                         &psn
                         );
switch ( GetEventKind(inEvent) ) {
    case kEventAppLaunched:

        status =    CopyProcessName(&psn , &processName);
        if (processName != NULL) {

            if([(NSString *)processName isEqualToString:@"myApp"])
            {
                //initialize application


            }

            CFRelease(processName); 
        }

            break;

        default:
            break;
    }
    return noErr;
}
- (void)InstallCarbonEventsForMonitor
{


    static EventHandlerRef sCarbonEventsRef = NULL;
    static const EventTypeSpec kEvents[] = {
        { kEventClassApplication, kEventAppLaunched },
        { kEventClassApplication, kEventAppTerminated }
    };

    if (sCarbonEventsRef == NULL) {
        (void) InstallEventHandler( GetApplicationEventTarget(), (EventHandlerUPP) CarbonEventHandler,
                                   GetEventTypeCount(kEvents),
                                   kEvents,
                                   self,
                                   &sCarbonEventsRef
                                   );

    }

}

0 个答案:

没有答案