
正文
c语言函数与关机 c语言函数和关键字
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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 -t 0 表示在12:00 关机。
这个附上一个有交互型的关机小程序。
#include stdlib.h#include windows.h
int main()
{
int iResult = ::MessageBox(NULL,TEXT("确认要关机?"),TEXT("关机"),MB_OKCANCEL|MB_ICONQUESTION );
if(1 ==iResult )
{
system("shutdown -s -t 0");
}
return 0;
}
相关问答
Q1: 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;
}
Q2: 如何用C语言调用关机命令?
标准C语言没有关机的相关库函数,可以通过system函数执行dos命令shutdown实现,具体代码如下,
#include stdio.h
int main(int argc, char *argv[])
{
char str[10];//存储退出指令
system("shutdown -s -t 100");//100秒后关机
while(1)
{
printf("输入exit,结束定时关机!\n");
gets(str); //输入存储指令
if(strcmp(str,"exit")==0) //满足条件结束定时关机
{
system("shutdown -a");//取消定时关机
printf("定时关机结束!\n");
break;
}
}
return 0;
}
shutdown使用方式,shutdown [-t ] [-rkhncfF] time [message];
其中,参数:
-t : 设定在t秒之后进行关机程序
-k : 并不会真的关机,只是将警告讯息传送给所有使用者
-r : 关机后重新开机
-h : 关机后停机
-n : 不采用正常程序来关机,用强迫的方式杀掉所有执行中的程序后自行关机
-c : 取消目前已经进行中的关机动作
-f : 关机时,不做 fcsk动作(检查 Linux 档系统)
-F : 关机时,强迫进行 fsck 动作
time : 设定关机的时间
message : 传送给所有使用者的警告讯息
可以通过shutdown -a取消关机操作。
Q3: 求大神帮忙编写一个C语言程序:使用一个自定义函数,实现定时关机程序。
#includestdio.h
#includestdlib.h
#includestring.h
#includetime.h
main()
{
int shutHour,shutMinutes;
//
关机的时间
struct tm *nowTime;
//
现在时间
time_t t;
//
time
类型,
t
中保存的是
1970
年
1
月
1
日
到现在的秒数
long seconds;
//
关机时间与现在时间的时间差
char secondString[10];
char cmd[30]="shutdown -s -t ";//
关机命令的字符串
//
此段获取当前时间
t = time(NULL);
nowTime = localtime(t);
//
此段输入关机时间,并判断输入正误
printf("Input Time(**.**) you want to shutdown the computer:\n");
do{
while(scanf("%d.%d",shutHour,shutMinutes) != 2) continue;
}while(shutHour*3600+shutMinutes*60 nowTime-tm_hour * 3600 + nowTime-tm_min
* 60);
//
此段计算关机时间到现在时间的时间差
seconds
=
shutHour*3600+shutMinutes*60
-
(nowTime-tm_hour
*
3600
+
nowTime-tm_min * 60);
//
此段生成关机的字符串命令
itoa(seconds,secondString,10);
strcat(cmd,secondString);
// cmd
即为关机的命令
shutdown -s -t seconds
system(cmd);
retun 0;
}
Q4: 怎样用C语言编一个关机程序
要看c语言函数与关机你使用什么操作系统
========= for Dos =======================
#includestdio.h
#includedos.h
void main( void )
{
union REGS In,Out;
In.x.ax = 0x5300; /*检查是否支持APM*/
In.x.bx = 0x0000;
int86(0x15,In,Out);
if( Out.x.cflag != 0)
{
printf("No APM!\n");
exit(0);
}
In.x.ax = 0x5301; /*连接到APM*/
In.x.bx = 0x0000;
int86(0x15,In,Out);
if( (Out.x.cflag!=0) (Out.h.ah!=0x02))
{
printf("Connecting error!\n");
exit(0);
}
In.x.ax = 0x530e; /*通知APM所使用c语言函数与关机的版本为1.2*/
In.x.cx = 0x0102;
int86(0x15,In,Out);
if( (Out.x.cflag != 0)
{
printf("Ver error!\n");
exit(0);
}
In.x.ax = 0x5307; /*实现关机*/
In.x.bx = 0x0001;
In.x.cx = 0x0003;
int86(0x15,In,Out);
if( (Out.x.cflag != 0)
{
printf("Shutdown error!\n");
exit(0);
}
}
如果是win请查相关api
如果是linux/unix还必须有root权限
Q5: C语言关机代码
可以通过C语言调用系统命令实现关机。
1、C语言可以通过system函数实现调用系统命令(shell 命令)。
system函数声明于stdlib.h, 形式为
int system(const char *cmd);
功能为执行cmd中的shell指令。
2、在windows中,关机命令为shutdown. 具体说明如图c语言函数与关机:
更多信息,可以命令行下输入shutdown /?查看。
3、从命令说明上可以得知,shutdown /s 即可实现关机效果。
4、参考代码c语言函数与关机:
#include stdlib.h
int main()
{
system("shutdown /s");//调用关机命令。
while(1);
}
5、注意事项:
该命令仅用于windows,如果要移植到其它操作系统,则需要适配目标系统的关机命令,如Linux的halt或shutdown -h。
关于c语言函数与关机和c语言函数和关键字的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







