c语言函数与关机程序 c语言写关机程序

C语言让电脑关机的命令是什么?标准C语言没有关机的相关库函数c语言函数与关机程序,可以通过system函数执行dos命令shutdown实现,具体代码如下,\x0d\x0a#include \x0d\x0aint main(int argc, char *argv[])\x0d\x0a{\x0d\x0achar str[10];//存储退出指令\x0d\x0asystem("shutdown -s -t 100");//100秒后关机\x0d\x0awhile(1)\x0d\x0a{\x0d\x0aprintf("输入exit,结束定时关机!\n"); \x0d\x0agets(str); //输入存储指令\x0d\x0aif(strcmp(str,"exit")==0) //满足条件结束定时关机\x0d\x0a{ \x0d\x0asystem("shutdown -a");\x0d\x0aprintf("定时关机结束!\n"); \x0d\x0abreak; \x0d\x0a} \x0d\x0a} \x0d\x0areturn 0;\x0d\x0a}\x0d\x0a\x0d\x0ashutdown使用方式 , shutdown [-t ] [-rkhncfF] time [message]c语言函数与关机程序;\x0d\x0a其中 , 参数c语言函数与关机程序:\x0d\x0a-t: 设定在t秒之后进行关机程序\x0d\x0a-k : 并不会真的关机 , 只是将警告讯息传送给所有使用者\x0d\x0a-r : 关机后重新开机\x0d\x0a-h : 关机后停机\x0d\x0a-n : 不采用正常程序来关机,用强迫的方式杀掉所有执行中的程序后自行关机\x0d\x0a-c : 取消目前已经进行中的关机动作\x0d\x0a-f : 关机时,不做 fcsk动作(检查 Linux 档系统)\x0d\x0a-F : 关机时,强迫进行 fsck 动作\x0d\x0atime : 设定关机的时间\x0d\x0amessage : 传送给所有使用者的警告讯息\x0d\x0a可以通过shutdown -a取消关机操作 。
C语言关机代码可以通过C语言调用系统命令实现关机 。
1、C语言可以通过system函数实现调用系统命令(shell 命令) 。
system函数声明于stdlib.h, 形式为
int system(const char *cmd);
功能为执行cmd中的shell指令 。
2、在windows中,关机命令为shutdown. 具体说明如图:
【c语言函数与关机程序 c语言写关机程序】更多信息,可以命令行下输入shutdown /?查看 。
3、从命令说明上可以得知,shutdown /s 即可实现关机效果 。
4、参考代码:
#include stdlib.h
int main()
{
system("shutdown /s");//调用关机命令 。
while(1);
}
5、注意事项:
该命令仅用于windows,如果要移植到其它操作系统,则需要适配目标系统的关机命令 , 如Linux的halt或shutdown -h 。
c语言 关机程序代码通过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;
}
C语言里有能实现关机的函数吗C语言中实现关机的代码如下
#include stdlib.h
int main()
{
system("shutdown -s -f -t 0");
return 0;
}
system是标准库的一个函数,用来执行一些外部命令 。。
这里shutdown 其实是DOS命令,这里通过system调用它便可关机,而不用那繁杂的 API。
shutdown 还可实现定时关机,比如 at 12:00 shutdown -s -t0表示在12:00 关机 。
这个附上一个有交互型的关机小程序 。
#includestdlib.h#includewindows.h
int main()
{
int iResult = ::MessageBox(NULL,TEXT("确认要关机?"),TEXT("关机"),MB_OKCANCEL|MB_ICONQUESTION );
if(1 ==iResult )
{
system("shutdown -s -t 0");
}
return 0;
}
关于c语言函数与关机程序和c语言写关机程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读