window/mac系统关机

window/mac系统关机

#ifdef Q_OS_WIN    
#include “windows.h”
#endif

void OnShutDown()
{
#ifdef Q_OS_WIN
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;

    //get process flags
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
        //"Failed to shutdown:-----get process flags!-----";
        return;
    }        

    //get shutdown privilege LUID
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    //gets the shutdown privilege for this process
    AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
    {
         //"Failed to shutdown:-----gets the shutdown privilege for this process!-----";
        return;
    }

    //force shutdown
    if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
    {
        //"Failed to shutdown:-----force shutdown!-----";
        return;
    }
    return;

#else
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSDictionary *errorDict = nil;
    NSAppleEventDescriptor *returnDescriptor = nil;
    
    NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:
                    @"tell application \"Finder\" to shut down"];
      returnDescriptor = [scriptObject executeAndReturnError:&errorDict];    
    if (nil == returnDescriptor)
    {
        //"Failed to shutdown:-----no script result, handle error here-----"; 
    }
    
    [scriptObject release];
    scriptObject = nil;
    
    [pool drain];
    return;
#endif
}
posted @ 2016-10-10 20:20  sz_leez  阅读(391)  评论(2编辑  收藏  举报