可以通過C語言調(diào)用系統(tǒng)命令實(shí)現(xiàn)關(guān)機(jī)。
創(chuàng)新互聯(lián)建站專注于上高網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供上高營(yíng)銷型網(wǎng)站建設(shè),上高網(wǎng)站制作、上高網(wǎng)頁設(shè)計(jì)、上高網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造上高網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供上高網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
1、C語言可以通過system函數(shù)實(shí)現(xiàn)調(diào)用系統(tǒng)命令(shell 命令)。
system函數(shù)聲明于stdlib.h, 形式為
int system(const char *cmd);
功能為執(zhí)行cmd中的shell指令。
2、在windows中,關(guān)機(jī)命令為shutdown. 具體說明如圖:
更多信息,可以命令行下輸入shutdown /?查看。
3、從命令說明上可以得知,shutdown /s 即可實(shí)現(xiàn)關(guān)機(jī)效果。
4、參考代碼:
#include?stdlib.h
int?main()
{
system("shutdown?/s");//調(diào)用關(guān)機(jī)命令。
while(1);
}
5、注意事項(xiàng):
該命令僅用于windows,如果要移植到其它操作系統(tǒng),則需要適配目標(biāo)系統(tǒng)的關(guān)機(jī)命令,如Linux的halt或shutdown -h。
標(biāo)準(zhǔn)C語言沒有關(guān)機(jī)的相關(guān)庫函數(shù),可以通過system函數(shù)執(zhí)行dos命令shutdown實(shí)現(xiàn),具體代碼如下,
#include stdio.h
int main(int argc, char *argv[])
{
char str[10];//存儲(chǔ)退出指令
system("shutdown -s -t 100");//100秒后關(guān)機(jī)
while(1)
{
printf("輸入exit,結(jié)束定時(shí)關(guān)機(jī)!\n");
gets(str); //輸入存儲(chǔ)指令
if(strcmp(str,"exit")==0) //滿足條件結(jié)束定時(shí)關(guān)機(jī)
{
system("shutdown -a");//取消定時(shí)關(guān)機(jī)
printf("定時(shí)關(guān)機(jī)結(jié)束!\n");
break;
}
}
return 0;
}
shutdown使用方式,shutdown [-t ] [-rkhncfF] time [message];
其中,參數(shù):
-t : 設(shè)定在t秒之后進(jìn)行關(guān)機(jī)程序
-k : 并不會(huì)真的關(guān)機(jī),只是將警告訊息傳送給所有使用者
-r : 關(guān)機(jī)后重新開機(jī)
-h : 關(guān)機(jī)后停機(jī)
-n : 不采用正常程序來關(guān)機(jī),用強(qiáng)迫的方式殺掉所有執(zhí)行中的程序后自行關(guān)機(jī)
-c : 取消目前已經(jīng)進(jìn)行中的關(guān)機(jī)動(dòng)作
-f : 關(guān)機(jī)時(shí),不做 fcsk動(dòng)作(檢查 Linux 檔系統(tǒng))
-F : 關(guān)機(jī)時(shí),強(qiáng)迫進(jìn)行 fsck 動(dòng)作
time : 設(shè)定關(guān)機(jī)的時(shí)間
message : 傳送給所有使用者的警告訊息
可以通過shutdown -a取消關(guān)機(jī)操作。
通過C語言實(shí)現(xiàn)關(guān)機(jī),有兩種方式:
1 通過system函數(shù),調(diào)用dos的關(guān)機(jī)命令。
通過stdlib.h中的
int system(char *cmd);
可以執(zhí)行dos命令cmd。
dos下關(guān)機(jī)的命令為shutdown -s,于是嗲用
system("shutdown -s");
即可實(shí)現(xiàn)關(guān)機(jī)操作。
2 通過調(diào)用windows提供的api函數(shù),來實(shí)現(xiàn)關(guān)機(jī):
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語言中實(shí)現(xiàn)關(guān)機(jī)的代碼如下
#include?stdlib.h
int?main()?
{
system("shutdown?-s?-f?-t?0");
return?0;?
}
system是標(biāo)準(zhǔn)庫的一個(gè)函數(shù),用來執(zhí)行一些外部命令。。
這里shutdown 其實(shí)是DOS命令,這里通過system調(diào)用它便可關(guān)機(jī),而不用那繁雜的 API 。
shutdown 還可實(shí)現(xiàn)定時(shí)關(guān)機(jī),比如 at 12:00 shutdown -s -t? 0? 表示在12:00 關(guān)機(jī)。
這個(gè)附上一個(gè)有交互型的關(guān)機(jī)小程序。
#include??stdlib.h#include??windows.h
int?main()
{?
int?iResult?=?::MessageBox(NULL,TEXT("確認(rèn)要關(guān)機(jī)?"),TEXT("關(guān)機(jī)"),MB_OKCANCEL|MB_ICONQUESTION?);?
if(1?==iResult?)?
{
system("shutdown?-s?-t?0");???
}
return?0;
}
網(wǎng)站標(biāo)題:c語言函數(shù)與關(guān)機(jī) c語言編寫關(guān)機(jī)程序
分享路徑:http://jinyejixie.com/article18/dopcggp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站改版、商城網(wǎng)站、自適應(yīng)網(wǎng)站、云服務(wù)器、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)