溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C/C++中一次性執(zhí)行多個(gè)DOS命令

發(fā)布時(shí)間:2020-07-20 23:14:17 來源:網(wǎng)絡(luò) 閱讀:8051 作者:tywali 欄目:編程語言

起因

最近給公司的一個(gè)系統(tǒng)寫了個(gè)啟動(dòng)的腳本,但是領(lǐng)導(dǎo)說批處理這樣的腳本太low了,要使用EXE來啟動(dòng),未來還要使用加密工具對EXE進(jìn)行加密。

好吧,我就在網(wǎng)上到處找bat轉(zhuǎn)exe的工具,找了很久,都沒有找到合適的,只有一個(gè)用winrar制作自解壓包的方法還算可以,但是這玩意兒有兩個(gè)坑爹的問題:

  1. 使用了自定義圖標(biāo)后,安裝時(shí)會(huì)被360報(bào)告有***;
  2. 用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上找到管道的示例代碼,簡單修改了一下。
首先,將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è)對象的安全描述符,并指定檢索到指定這個(gè)結(jié)構(gòu)的句柄是否是可繼承的。
    // 這個(gè)結(jié)構(gòu)為很多函數(shù)創(chuàng)建對象時(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)輸出句柄,對于Windows控制臺應(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í)行效果如下圖:
C/C++中一次性執(zhí)行多個(gè)DOS命令

main.exe的原始目錄是D:\Workspace\research\C\Chrome\,執(zhí)行時(shí),首先執(zhí)行了cd..,退到上一層目錄,然后執(zhí)行dir,顯示上一層目錄的內(nèi)容,證明上面的代碼確實(shí)可以一次執(zhí)行多條DOS命令。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI