C/C++中一次性執(zhí)行多個(gè)DOS命令的實(shí)現(xiàn)思路
起因
最近給公司的一個(gè)系統(tǒng)寫了個(gè)啟動(dòng)的腳本,但是領(lǐng)導(dǎo)說批處理這樣的腳本太low了,要使用EXE來啟動(dòng),未來還要使用加密工具對(duì)EXE進(jìn)行加密。
好吧,我就在網(wǎng)上到處找bat轉(zhuǎn)exe的工具,找了很久,都沒有找到合適的,只有一個(gè)用winrar制作自解壓包的方法還算可以,但是這玩意兒有兩個(gè)坑爹的問題:
使用了自定義圖標(biāo)后,安裝時(shí)會(huì)被360報(bào)告有木馬;
用winrar制作的exe,其本質(zhì)還是解壓后執(zhí)行,解壓后的文件其實(shí)可以在系統(tǒng)臨時(shí)目錄下找到,因此以后想要加密其實(shí)很容易就會(huì)被破解;
所以最好的辦法看來就是自己寫一個(gè)exe了,考慮到我以前用過C,因此下載了Dev-Cpp這個(gè)工具來編寫代碼。
思路
在C語言中執(zhí)行DOS命令的方法很多,如:ShellExecute, WinExec, CreateProcess等,但是這些接口都是只能一次執(zhí)行一條命令,在我的啟動(dòng)腳本里有很多命令,有一些是設(shè)置環(huán)境變量的,這樣就沒法在代碼中一條條執(zhí)行腳本中的命令,必須要找到一個(gè)辦法可以一次性執(zhí)行多條命令。
在網(wǎng)上找了很久,最終確定使用CreateProcess,同時(shí)要使用管道技術(shù)。也就是使用CreateProcess創(chuàng)建一個(gè)cmd進(jìn)程,然后通過輸入管道將待執(zhí)行的命令傳遞給cmd進(jìn)程,通過輸出管道獲取cmd進(jìn)程的輸出信息,因?yàn)槭峭ㄟ^管道進(jìn)行,所以可以模擬在DOS窗口一行行輸入命令,從而實(shí)現(xiàn)執(zhí)行多條DOS命令了。
實(shí)現(xiàn)
從MSDN上找到管道的示例代碼,簡(jiǎn)單修改了一下。
首先,將CreateProcess的參數(shù)改為啟動(dòng)cmd:
char cmdLine[] = "cmd";
// Create the child process.
bFuncRetn = CreateProcess(NULL,
cmdLine, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
然后,將原來批處理里面的腳本復(fù)制一下,放到一個(gè)變量里(這里我改了一下,沒有用我實(shí)際的腳本,因?yàn)槟莻€(gè)不通用,不適合做例子),注意,每一行最后要加上回車符\n,這樣才能正確模擬DOS窗口中輸入命令的情況:
CHAR cmds[] = "@ECHO OFF\n"
"cd..\n"
"dir\n"
再然后,原來的示例代碼中是把批處理文件作為EXE的參數(shù)傳遞進(jìn)來的,既然上面改為將批處理文件內(nèi)容放到腳本里,代碼中從文件中讀取命令的那部分就要去掉了,這部分代碼就不多說了。
完整的示例代碼如下:
#include <stdio.h>
#include <windows.h>
#define BUFSIZE 4096
HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
hInputFile, hStdout;
BOOL CreateChildProcess(VOID);
VOID WriteToPipe(VOID);
VOID ReadFromPipe(VOID);
VOID ErrorExit(const char *);
VOID ErrMsg(LPTSTR, BOOL);
int main(int argc, char *argv[]) {
// SECURITY_ATTRIBUTES結(jié)構(gòu)包含一個(gè)對(duì)象的安全描述符,并指定檢索到指定這個(gè)結(jié)構(gòu)的句柄是否是可繼承的。
// 這個(gè)結(jié)構(gòu)為很多函數(shù)創(chuàng)建對(duì)象時(shí)提供安全性設(shè)置
SECURITY_ATTRIBUTES saAttr;
BOOL fSuccess;
// Set the bInheritHandle flag so pipe handles are inherited.
// 設(shè)置句柄為可繼承的,使得子線程可以使用父線程
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Get the handle to the current STDOUT.
// 取得當(dāng)前應(yīng)用的標(biāo)準(zhǔn)輸出句柄,對(duì)于Windows控制臺(tái)應(yīng)用來說,一般是輸出到屏幕
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
// Create a pipe for the child process's STDOUT.
// 創(chuàng)建一個(gè)用于輸出操作的匿名管道。
if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
ErrorExit("Stdout pipe creation failed\n");
// Create noninheritable read handle and close the inheritable read handle.
// 將輸出管道的句柄綁定到當(dāng)前進(jìn)程
fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
GetCurrentProcess(), &hChildStdoutRdDup , 0,
FALSE,
DUPLICATE_SAME_ACCESS);
if( !fSuccess )
ErrorExit("DuplicateHandle failed");
CloseHandle(hChildStdoutRd);
// Create a pipe for the child process's STDIN.
// 創(chuàng)建一個(gè)用于輸入操作的匿名管道。
if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
ErrorExit("Stdin pipe creation failed\n");
// Duplicate the write handle to the pipe so it is not inherited.
// 將輸入管道的句柄綁定到當(dāng)前進(jìn)程
fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
GetCurrentProcess(), &hChildStdinWrDup, 0,
FALSE, // not inherited
DUPLICATE_SAME_ACCESS);
if (! fSuccess)
ErrorExit("DuplicateHandle failed");
CloseHandle(hChildStdinWr);
// Now create the child process.
// 創(chuàng)建DOS子進(jìn)程
fSuccess = CreateChildProcess();
if (! fSuccess)
ErrorExit("Create process failed");
// Write to pipe that is the standard input for a child process.
WriteToPipe();
// Read from pipe that is the standard output for child process.
ReadFromPipe();
return 0;
}
BOOL CreateChildProcess() {
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
BOOL bFuncRetn = FALSE;
// Set up members of the PROCESS_INFORMATION structure.
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
// Set up members of the STARTUPINFO structure.
// 設(shè)定DOS進(jìn)程的標(biāo)準(zhǔn)輸入、輸出和錯(cuò)誤信息的管道
// 使用前面創(chuàng)建的值,DOS窗口的輸入輸出都會(huì)被定向到本應(yīng)用中
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = hChildStdoutWr;
siStartInfo.hStdOutput = hChildStdoutWr;
siStartInfo.hStdInput = hChildStdinRd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
char cmdLine[] = "cmd";
// Create the child process.
bFuncRetn = CreateProcess(NULL,
cmdLine, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
if (bFuncRetn == 0)
ErrorExit("CreateProcess failed");
else {
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
return bFuncRetn;
}
}
VOID WriteToPipe(VOID) {
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
CHAR cmds[] = "@ECHO ON\n"
"cd..\n"
"dir\n";
WriteFile(hChildStdinWrDup, cmds, sizeof(cmds), &dwWritten, NULL);
// Close the pipe handle so the child process stops reading.
if (! CloseHandle(hChildStdinWrDup))
ErrorExit("Close pipe failed");
}
VOID ReadFromPipe(VOID) {
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
// Close the write end of the pipe before reading from the
// read end of the pipe.
if (!CloseHandle(hChildStdoutWr))
ErrorExit("CloseHandle failed");
// Read output from the child process, and write to parent's STDOUT.
// 獲取子線程,即DOS窗口的輸出,顯示到標(biāo)準(zhǔn)輸出設(shè)備上
for (;;) {
if( !ReadFile( hChildStdoutRdDup, chBuf, BUFSIZE, &dwRead,
NULL) || dwRead == 0) break;
if (! WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL))
break;
}
}
VOID ErrorExit (const char *lpszMessage) {
fprintf(stderr, "%s\n", lpszMessage);
ExitProcess(0);
}
執(zhí)行效果如下圖:
main.exe的原始目錄是D:\Workspace\research\C\Chrome\,執(zhí)行時(shí),首先執(zhí)行了cd..,退到上一層目錄,然后執(zhí)行dir,顯示上一層目錄的內(nèi)容,證明上面的代碼確實(shí)可以一次執(zhí)行多條DOS命令。
欄 目:C語言
下一篇:C++ 17標(biāo)準(zhǔn)正式發(fā)布! 更簡(jiǎn)單地編寫和維護(hù)代碼
本文標(biāo)題:C/C++中一次性執(zhí)行多個(gè)DOS命令的實(shí)現(xiàn)思路
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1020.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


