通过C语言实现关机,有两种方式:
1 通过system函数,调用dos的关机命令。
通过stdlib.h中的
int system(char *cmd);
可以执行dos命令cmd。
dos下关机的命令为shutdown -s,于是嗲用
system("shutdown -s");
即可实现关机操作。
2 通过调用windows提供的api函数,来实现关机:
void shut_down_windows()
{
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,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE |
SHTDN_REASON_FLAG_PLANNED))
return FALSE;
return TRUE;
}
system(“shutdown -r”);直接调用cmd命令就可以了
有一个at hh:mm shutdown是用来建立一个到hh:mm执行shutdown命令的任务
at 23:05 shutdown /f
这是我做的定时关机小程序,跟c语言命令行窗口不太一样