Windows 종료 함수 WinAPI

회사에서 코딩하다가 하드웨어 스펙이 딸려서 플그램이 48시간 정도 지나면 OS가 뻗어버리는 문제가 발생해서 매일 자정이 되면 셧다운 시켜버리는 방식을 도입 -_-;

그래서 찾은 코드 여기에 기록해 놓는다.
XP에서는 그냥 함수 호출 하면 되는데 비스타 부터는 관리자 권한을 받아야지만 셧다운됨.

그래서 덩달아 찾은 코드

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if(!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if(GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if(!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return FALSE;
HANDLE hToken; TOKEN_PRIVILEGES tkp; // Get a token for this process. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE; // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if(GetLastError() != ERROR_SUCCESS) return FALSE; // Shut down the system and force all applications to close. if(!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) return FALSE;
 HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
 
// Get a token for this process.
    if(!OpenProcessToken(GetCurrentProcess(),
            TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return FALSE;
 
// Get the LUID for the shutdown privilege.
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1; // one privilege to set
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
 
// Get the shutdown privilege for this process.
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if(GetLastError() != ERROR_SUCCESS)
        return FALSE;
 
// Shut down the system and force all applications to close.
    if(!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
        return FALSE;

ExitWindowsEx(UINT uFlags, DWORD dwReserved);

flag값
EWX_LOGOFF : 로그오프
EWX_POWEROFF : 전원 끄기
EWX_REBOOT : 시스템 리부트
EWX_SHUTDOWN : 셧다운


Comments

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다