조회 수 7996 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
필요하실 분도 있을 듯 해서 2001년도에 짰던 것 같네요.
윈도우즈2000 계열의 파워 끄는 소스코드입니다.
VC6.0으로 짰고, 프로그램을 하실 줄 아시면 뭐하는 것인 지 금방 아실 듯.

PwrTest.h
#if !defined(AFX_PWRTEST_H__149C02D0_B545_4699_A4A5_DFDB719EFBFB__INCLUDED_)
#define AFX_PWRTEST_H__149C02D0_B545_4699_A4A5_DFDB719EFBFB__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "resource.h"

/////////////// Jiseok
int PowerOff();
int Reboot();
int GetWinVersion(OSVERSIONINFO* windowsversion);
int DoPowerOff(OSVERSIONINFO* windowsversion);
int DoReboot(OSVERSIONINFO* windowsversion);
int AquirePrivileges(OSVERSIONINFO* windowsversion);
////////////// end Jiseok

#endif 
//!defined(AFX_PWRTEST_H__149C02D0_B545_4699_A4A5_DFDB719EFBFB__INCLUDED_)

PwrTest.cpp
// PwrTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "PwrTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// The one and only application object
//

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        cerr << _T("Fatal Error: MFC initialization failed") << endl;
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.

        //if (PowerOff()!=0)
        if (Reboot()!=0)
            return 1;

        CString strHello;
        strHello.LoadString(IDS_HELLO);
        cout << (LPCTSTR)strHello << endl;
    }

    return nRetCode;
}

int PowerOff()
{
    OSVERSIONINFO Version;

    if (GetWinVersion(&Version)!=0)
        return -1;
    if (DoPowerOff(&Version)!=0)
        return -1;
    return 0;
}

int Reboot()
{
    OSVERSIONINFO Version;

    if (GetWinVersion(&Version)!=0)
        return -1;
    if (DoReboot(&Version)!=0)
        return -1;
    return 0;
}

int GetWinVersion(OSVERSIONINFO* windowsversion)
{
    windowsversion->dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
    if (!GetVersionEx(windowsversion))
    {
        printf("Error! getting version info");
        return -1;
    }
    return 0;
}

int DoPowerOff(OSVERSIONINFO* windowsversion)
{
    UINT mode;

    if (windowsversion->dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
        mode=EWX_SHUTDOWN;
    else
        mode=EWX_POWEROFF;

    if (windowsversion->dwPlatformId==VER_PLATFORM_WIN32_NT)
        {
        // privilege 얻기
        if (AquirePrivileges(windowsversion)!=0)
        {
            return -1;
        }//if 
    }// if
  
    if (!ExitWindowsEx(mode,0))    // 끄기
    {
        printf("error ExitWindowsEx");
        return -1;
    }
    return 0;
}

      
int DoReboot(OSVERSIONINFO* windowsversion)
{
    UINT mode;

    mode=EWX_REBOOT;

    if (windowsversion->dwPlatformId==VER_PLATFORM_WIN32_NT)
        {
        // privilege 얻기
        if (AquirePrivileges(windowsversion)!=0)
        {
            return -1;
        }//if 
    }// if
  
    if (!ExitWindowsEx(mode,0))    // 끄기
    {
        printf("error ExitWindowsEx");
        return -1;
    }
    return 0;
}

int AquirePrivileges(OSVERSIONINFO* windowsversion)
{
    HANDLE current_thread,token;
    TOKEN_PRIVILEGES privs;
    LUID luid;

    current_thread=GetCurrentProcess();
    if (!OpenProcessToken(current_thread,TOKEN_ADJUST_PRIVILEGES,&token))
    {
        printf("Error! opening process token");
        return -1;
    }
    if (!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid))
    {
        printf("Error! looking up privilege");
        return -1;
    }
    privs.PrivilegeCount=1;
    privs.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    privs.Privileges[0].Luid=luid;
    if (!AdjustTokenPrivileges(token,FALSE,&privs,0,NULL,NULL))
    {
        printf("Error! getting privileges");
        return -1;
    }
    return 0;
}
[출처] http://kldp.org/node/28417
?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
1013 Develop [c] 포인터 학습용 예제 소스 코드 file hooni 2013.04.23 21087
1012 Develop [c] 이진트리/트리 순회법 코드(전위/중위/후위) 5 file hooni 2015.07.02 21065
1011 PPT [doc] 발표 자료 ㅎㅎtalk4neo file hooni 2013.04.23 21027
1010 PPT [ppt] 시스템보안 연구실 ppt 템플릿.. (CIST) file hooni 2013.04.23 20934
1009 System/OS [doc] 코코아 프로그래밍 for MACOS 관련 내 분량.. file hooni 2013.04.23 20907
1008 Develop git 브런치 배우기 (링크) hooni 2013.07.09 20701
1007 Develop [c++] 디렉토리 내의 파일 찾기 FindFirstFile() 함수 6 hooni 2013.04.23 20663
1006 System/OS asx미디어 정보 기록.. hooni 2003.04.23 20605
1005 Develop [opengl] 컴퓨터 그래픽스 강의 자료(수업자료) file hooni 2003.04.23 20524
1004 Develop [android] [번역] 안드로이드 Android Cloud to Device Messaging(C2DM) hooni 2013.04.23 20481
1003 Develop [php] substr() 한글 자를 때 깨짐 방지 hooni 2014.01.09 20440
1002 System/OS [linux] 꿀통(honeyd) 설치하기.. 메뉴얼 과정 6 file hooni 2006.04.23 20305
1001 Etc [web] 제로보드 XE 템플릿에서 if문에 대해서 알아봅시다 hooni 2013.08.16 20252
1000 Develop [ios] UIView에서 상위 UIViewController 가져오기 hooni 2013.09.27 20240
999 Develop [c] UTF-8을 EUC-KR로 변환.. (iconv) file hooni 2013.04.23 20199
998 Develop [c] AES 알고리즘 (암호화/복호화) file hooni 2003.04.23 20145
Board Pagination Prev 1 ... 9 10 11 12 13 ... 74 Next
/ 74