如何强制杀死另一个应用程序在可可Mac OS X 10.5 [英] How to force kill another application in cocoa Mac OS X 10.5

查看:578
本文介绍了如何强制杀死另一个应用程序在可可Mac OS X 10.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个任务,从我的应用程序,我需要杀死另一个我的应用程序,问题是其他应用程序有终止确认对话框 / strong>(没有要保存的关键数据,仅确认用户意图退出)。

I've this task, from my application i need to kill another my application, the problem is that the other application has a Termination Confirm Dialog (there is no critical data to save, only confirmation of user intent to quit).


  • 将使用:

  • On 10.6+ you will use:

bool TerminatedAtLeastOne = false;

// For OS X >= 10.6 NSWorkspace has the nifty runningApplications-method.
if ([NSRunningApplication respondsToSelector:@selector(runningApplicationsWithBundleIdentifier:)]) {
    for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.company.applicationName"]) {
        [app forceTerminate];
        TerminatedAtLeastOne = true;
    }
    return TerminatedAtLeastOne;
}


  • p>

  • but on <10.6 this commonly used Apple Event:

    // If that didn‘t work either... then try using the apple event method, also works for OS X < 10.6.
    AppleEvent event = {typeNull, nil};
    const char *bundleIDString = "com.company.applicationName";
    
    OSStatus result = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication, typeApplicationBundleID, bundleIDString, strlen(bundleIDString), kAutoGenerateReturnID, kAnyTransactionID, &event, NULL, "");
    
    if (result == noErr) {
        result = AESendMessage(&event, NULL, kAENoReply|kAEAlwaysInteract, kAEDefaultTimeout);
        AEDisposeDesc(&event);
    }
    return result == noErr;
    

    无法强制退出!!!

    那么你可以使用什么呢?

    So what can you use?

    推荐答案

    我已经在 上挖掘的代码

    // If that didn‘t work then try shoot it in the head, also works for OS X < 10.6.
    NSArray *runningApplications = [[NSWorkspace sharedWorkspace] launchedApplications];
    NSString *theName;
    NSNumber *pid;
    for ( NSDictionary *applInfo in runningApplications ) {
        if ( (theName = [applInfo objectForKey:@"NSApplicationName"]) ) {
            if ( (pid = [applInfo objectForKey:@"NSApplicationProcessIdentifier"]) ) {
                //NSLog( @"Process %@ has pid:%@", theName, pid );    //test
                if( [theName isEqualToString:@"applicationName"] ) {
                    kill( [pid intValue], SIGKILL );
                    TerminatedAtLeastOne = true;
                }
            }
        }
    }
    return TerminatedAtLeastOne;
    

    这篇关于如何强制杀死另一个应用程序在可可Mac OS X 10.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆