Red de conocimiento informático - Material del sitio web - Cómo escribir un programa de apagado forzado de computadora en C o C

Cómo escribir un programa de apagado forzado de computadora en C o C

#includelt; Windows.hgt;

#includelt;tchar.hgt;

#includelt;assert.hgt;TCHAR* szQuestion=TEXT("¿Qué es 1000*10?")

TCHAR* szAnswer=TEXT("10000");

WNDPROC oldEditProc=NULL

WNDPROC oldButtonProc=NULL; HFONT hFont=NULL;

HWND hTestDlgWnd=NULL, hEditWnd=NULL, hButtonWnd=NULL;

LRESULT CALLBACK TestDlgProc(HWND hWnd, mensaje UINT, WPARAM wParam, LPARAM lParam); /p>

LLAMADA DE RESULTADO MyEditProc(HWND hWnd, mensaje UINT, WPARAM wParam, LPARAM lParam);

LLAMADA DE RESULTADO MyButtonProc(HWND hWnd, mensaje UINT, WPARAM wParam, LPARAM lParam);

BOOL SystemReboot(); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmd(); int LPSTR lpCmdLine, int nShowCmd)

{

WNDCLASS wndclass={ 0};

TCHAR* szClassName=TEXT("TestDlg");

MSG msg={0}; COLORREF color=GetSysColor(COLOR_ACTIVEBORDER); HBRUSH hBrush=CreateSolidBrush(color);

afirmar(hBrush); hFont=(HFONT) GetStockObject(DEFAULT_GUI_FONT);

LOGFONT ={0}; > GetObject(hFont, sizeof(LOGFONT), & logfont);

DeleteObject(hFont);

hFont=NULL;

logfont.lfHeight=12;

_tcscpy(logfont.lfFaceName, TEXT("宋体"));

hFont= CreateFontIndirect(amp;logfont);

afirmar(hFont); .style=CS_NOCLOSE;

wndclass.hInstance=hInstance;

wndclass.cbWndExtra=0;

wndclass.cbClsExtra=0;

p>

wndclass.lpfnWndProc= TestDlgProc;

wndclass.lpszMenuName=NULL;

wndclass.lpszClassName=szClassName;

if(!RegisterClass(amp ; wndclass))

{

MessageBox(NULL, TEXT("¡Error en el registro de la ventana!"), TEXT("Advertencia"), MB_OK);

return S_FALSE;

}DWORD style=WS_OVERLAPPEDWINDOW amp;~ (WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME

int cx=GetSystemMetrics(SM_CXFULLSCREEN);

int cy); =GetSystemMetrics(SM_CYFULLSCREEN);

int iTestDlgWidth=500;

int iTestDlgHeight=230

hTestDlgWnd=CreateWindowEx(WS_EX_TOOLWINDOW,

szClassName,

TEXT("Responda la pregunta: "),

estilo,

cx/2-iTestDlgWidth/2,

cy /2-iTestDlgHeight/2,

iTestDlgWidth,

iTestDlgHeight,

NULL,

NULL,

hInstancia,

NULL);

afirmar(hTestDlgWnd);

::SendMessage(hTestDlgWnd, WM_SETFONT, (WPARAM)hFont, 1); WS_CHILD | ES_ MULTILINE | ES_LEFT | ES_AUTOHSCROLL | ES_AUTOVSCROLL;

hEditWnd=CreateWindow(

TEXTO("Editar"),

TEXTO(""),

p>

estilo,

10,

40,

iTestDlgWidth-20,

100,

hTestDlgWnd,

NULL,

hInstance,

0

afirmar); ( hEditWnd) ;

oldEditProc=(WNDPROC)SetWindowLong(hEditWnd, GWL_WNDPROC, (LONG)MyEditProc

afirmar(oldEditProc

: .SendMessage); (hEditWnd, WM_SETFONT, (WPARAM)hFont, 1 estilo=WS_CHILD;

hButtonWnd=CreateWindow(TEXT("Botón"),

TEXTO("OK"),

estilo,

10,

150,

60,

20,

hTestDlgWnd,

NULL,

hInstance,

0

);

afirmar (hButtonWnd);

oldButtonProc=(WNDPROC)SetWindowLong(hButtonWnd, GWL_WNDPROC, (LONG)MyButtonProc);

afirmar(oldButtonProc);

: SendMessage(hButtonWnd, WM_SETFONT, (WPARAM)hFont, 1); ShowWindow(hTestDlgWnd, SW_SHOW); p> ShowWindow(hEditWnd, SW_SHOW);

ShowWindow(hButtonWnd, SW_SHOW);

UpdateWindow(hTestDlgWnd); ::SetFocus(hEditWnd); (GetMessage(amp;msg,NULL,0,0))

{

TranslateMessage(amp;msg);

DispatchMessage(amp;msg);

}

if(hBrush)

DeleteObject(hBrush).

if(hFont)

DeleteObject (hFont);

return msg.wParam;

}LRESULT CALLBACK TestDlgProc(HWND hWnd, mensaje UINT, WPARAM wParam, LPARAM lParam)

{< / p>

cambiar(mensaje)

{

caso WM_CLOSE:

{

DestroyWindow(hWnd);

p>

}

descanso

caso WM_DESTROY:

{

PostQuitMessage(0); /p>

}

descanso

caso WM_PAINT:

{

PAINTSTRUCT ps={0}

p>

HDC hDC=NULL;

hDC=BeginPaint(hWnd, amp; ps

HFONT hOldFont=(HFONT)SelectObject(hDC, hFont); p>

int oldMode=SetBkMode(hDC, TRANSPARENT);

TextOut(hDC, 10, 5, szQuestion, _tcslen(szQuestion));

SelectObject(h).

DC, hOldFont);

SetBkMode (hDC, oldMode);

EndPaint (hWnd, amperio; ps); p>

p>

}

Devuelve DefWindowProc(hWnd, mensaje, wParam, lParam);

}LRESULT CALLBACK MyEditProc(HWND hWnd, mensaje UINT, WPARAM wParam); , LPARAM lParam)

{

Return (*oldEditProc)(hWnd message, wParam, lParam

}LRESULT CALLBACK MyButtonProc(HWND hWnd, mensaje UINT); , WPARAM wParam, LPARAM lParam)

{

switch( mensaje)

{

caso WM_LBUTTONDOWN:

{

TCHAR buf[512]={0};

GetWindowText(hEditWnd, buf, 512); if(!_tcscmp(buf, szAnswer))

{

MessageBox(hWnd, TEXT("Has respondido correctamente."), TEXT("¡Has respondido correctamente!"), TEXT("Pregunta"), MB_OK);

:: SendMessage(hTestDlgWnd, WM_CLOSE, 0, 0

}

else

{

MessageBox(hWnd); , TEXT("¡Respondiste incorrectamente! ¡Prepárate para ser castigado! !"), TEXT("Pregunta"), MB_OK);

SystemReboot()

}

}

break;

}

return (*oldButtonProc)(hWnd, mensaje, wParam, lParam); p>}BOOL SystemReboot()

{

HANDLE hToken

TOKEN_PRIVILEGES tkp; OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, y hToken))

{

return(

}

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, & tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1

tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

AjustarTokenPrivileges(hToken, FALSE, & tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (GetLastError() != ERROR_SUCCESS)

{

Devuelve FALSO

} if (!ExitWindowsEx(EWX_REBOOT, 0))

{

Devuelve FALSO; >

Devuelve TRUE;

}Modificar las preguntas y respuestas en TCHAR* szQuestion=TEXT("1000*10 es igual a ¿qué?");

TCHAR* szAnswer=TEXT ( "10000");

Configuración. He respondido esta pregunta 3 veces.