听说最近流行写自动关机,我也写一个水一贴
[C++] 纯文本查看 复制代码 #include <windows.h>
#include <stdio.h>
#define SeShutdownPrivilege 0x13
typedef int(__stdcall *PRtlAdjustPrivilege)(ULONG, BOOLEAN, BOOLEAN, PBOOLEAN);
BOOL fun;
DWORD WINAPI NewThread(LPVOID Lp)
{
DWORD Sec = (DWORD)Lp;
while (TRUE)
{
system("cls");
printf("距离自动关机还有%d秒\n", Sec--);
printf("按回车键取消自动关机\n");
Sleep(1000);
if (0==Sec)
{
if (fun)
ExitWindowsEx(EWX_POWEROFF, 0);
else
ExitWindowsEx(EWX_SHUTDOWN, 0);
break;
}
}
return 0;
}
int main()
{
DWORD Sec;
BOOLEAN enAbled = 0;
printf("请设置自动关机延时(秒):");
scanf("%d", &Sec);
getchar();
printf("请选择关机方式:1为快速关机,其他数字为安全关机:");
scanf("%d", &fun);
getchar();
if (1 == fun)
fun = TRUE;
else
fun = FALSE;
PRtlAdjustPrivilege RtlAdjustPrivilege =
(PRtlAdjustPrivilege)GetProcAddress(GetModuleHandle("ntdll"), "RtlAdjustPrivilege");
if (RtlAdjustPrivilege)
{
RtlAdjustPrivilege(SeShutdownPrivilege, TRUE, FALSE, &enAbled);
CreateThread(NULL, 0, NewThread, (LPVOID)Sec, 0, NULL);
}
getchar();
return 0;
} |