본문 바로가기

Windows/MFC

윈도우 종료

[code]void CShutdown::SetShutDownMode(UINT l_bMode)
{
    if(l_bMode == LOGOFF)
        OnWindowsLogOff();
    else if(l_bMode == REBOOT)
        SystemShutdown(TRUE);
    else if(l_bMode == SHUTDOWN)
        SystemShutdown(FALSE);
}

BOOL CShutdown::SystemShutdown(BOOL l_bReboot)
{
    if(GetWindowsSystemType())        // If This OS is like WinNT, The Return value is TRUE
    {
        HANDLE hToken;             // handle to process token
        TOKEN_PRIVILEGES tkp;     // pointer to token structure

        CString l_sErrorCode;

        if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
            AfxMessageBox("OpenProcessToken failed.");

        LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
        tkp.PrivilegeCount = 1;
        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);

        if (GetLastError() != ERROR_SUCCESS)
            AfxMessageBox("AdjustTokenPrivileges enable failed.");

        if(!InitiateSystemShutdown(    NULL,
                        NULL,    //"System Shutdown , so You must Save datas of Each Application ",
                        0,
                        TRUE,
                        l_bReboot))        //<--이 함수가 2번째 Argument에 NULL을 입력해도 NT에서만 메세지가 나타나는지 검사할것.
        {                            //또다른 함수로써 ExitWindowsEx가 있음 이 함수의 Flag로써는 종료시 EWX_SHUTDOWN을 이용하면 됨
            l_sErrorCode.Format("%d",GetLastError());
            if(l_sErrorCode != "1115")
            {
                AfxMessageBox("Abort System Shutdown! Error Code is "+l_sErrorCode);
                return FALSE;
            }
        }

        return TRUE;
    }
    else
    {
        if(l_bReboot)
            return ExitWindowsEx(EWX_REBOOT,0);
        else
            return ExitWindowsEx(EWX_SHUTDOWN,0);
    }
}

BOOL CShutdown::OnWindowsLogOff()
{
    return ExitWindowsEx(EWX_LOGOFF,0);
}

BOOL CShutdown::GetWindowsSystemType()
{
    UINT l_uiVersion = GetVersion();

    if (l_uiVersion < 0x80000000)                // Windows NT
        return TRUE;
    else
        return FALSE;
}

[/code]

'Windows > MFC' 카테고리의 다른 글

윈도우 종류알기 OSVERSIONINFO  (0) 2013.10.02
윈도우 종료 (Windows 2k)  (0) 2013.10.02
현재시간 CTime::GetCurrentTime  (0) 2013.10.02
GetDlgItem 컨트롤 disabled enabled  (0) 2013.10.02
프로그램 실행 WinExec  (0) 2013.10.02