C/C++中一次性执行多个DOS命令

简介:

起因

最近给公司的一个系统写了个启动的脚本,但是领导说批处理这样的脚本太low了,要使用EXE来启动,未来还要使用加密工具对EXE进行加密。

好吧,我就在网上到处找bat转exe的工具,找了很久,都没有找到合适的,只有一个用winrar制作自解压包的方法还算可以,但是这玩意儿有两个坑爹的问题:

  1. 使用了自定义图标后,安装时会被360报告有木马;
  2. 用winrar制作的exe,其本质还是解压后执行,解压后的文件其实可以在系统临时目录下找到,因此以后想要加密其实很容易就会被破解;

所以最好的办法看来就是自己写一个exe了,考虑到我以前用过C,因此下载了Dev-Cpp这个工具来编写代码。

思路

在C语言中执行DOS命令的方法很多,如:ShellExecute, WinExec, CreateProcess等,但是这些接口都是只能一次执行一条命令,在我的启动脚本里有很多命令,有一些是设置环境变量的,这样就没法在代码中一条条执行脚本中的命令,必须要找到一个办法可以一次性执行多条命令。

在网上找了很久,最终确定使用CreateProcess,同时要使用管道技术。也就是使用CreateProcess创建一个cmd进程,然后通过输入管道将待执行的命令传递给cmd进程,通过输出管道获取cmd进程的输出信息,因为是通过管道进行,所以可以模拟在DOS窗口一行行输入命令,从而实现执行多条DOS命令了。

实现

从MSDN上找到管道的示例代码,简单修改了一下。
首先,将CreateProcess的参数改为启动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

然后,将原来批处理里面的脚本复制一下,放到一个变量里(这里我改了一下,没有用我实际的脚本,因为那个不通用,不适合做例子),注意,每一行最后要加上回车符\n,这样才能正确模拟DOS窗口中输入命令的情况

    CHAR cmds[] = "@ECHO OFF\n"
                    "cd..\n"
                    "dir\n"

再然后,原来的示例代码中是把批处理文件作为EXE的参数传递进来的,既然上面改为将批处理文件内容放到脚本里,代码中从文件中读取命令的那部分就要去掉了,这部分代码就不多说了。

完整的示例代码如下:

#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结构包含一个对象的安全描述符,并指定检索到指定这个结构的句柄是否是可继承的。
    // 这个结构为很多函数创建对象时提供安全性设置
    SECURITY_ATTRIBUTES saAttr;
    BOOL fSuccess;

    // Set the bInheritHandle flag so pipe handles are inherited.
    // 设置句柄为可继承的,使得子线程可以使用父线程
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;

    // Get the handle to the current STDOUT.
    // 取得当前应用的标准输出句柄,对于Windows控制台应用来说,一般是输出到屏幕
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    // Create a pipe for the child process's STDOUT.
    // 创建一个用于输出操作的匿名管道。
    if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
        ErrorExit("Stdout pipe creation failed\n");

    // Create noninheritable read handle and close the inheritable read handle.
    // 将输出管道的句柄绑定到当前进程
    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.
    // 创建一个用于输入操作的匿名管道。
    if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
        ErrorExit("Stdin pipe creation failed\n");

    // Duplicate the write handle to the pipe so it is not inherited.
    // 将输入管道的句柄绑定到当前进程
    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.
    // 创建DOS子进程
    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.
    // 设定DOS进程的标准输入、输出和错误信息的管道
    // 使用前面创建的值,DOS窗口的输入输出都会被定向到本应用中
    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窗口的输出,显示到标准输出设备上
    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);
}

执行效果如下图:
C/C++中一次性执行多个DOS命令

main.exe的原始目录是D:\Workspace\research\C\Chrome\,执行时,首先执行了cd..,退到上一层目录,然后执行dir,显示上一层目录的内容,证明上面的代码确实可以一次执行多条DOS命令。






本文转自 tywali 51CTO博客,原文链接:http://blog.51cto.com/lancelot/2046167,如需转载请自行联系原作者

目录
相关文章
|
1月前
|
存储 Shell Linux
【Shell 命令集合 磁盘维护 】Linux 创建DOS文件系统 mkdosfs命令使用指南
【Shell 命令集合 磁盘维护 】Linux 创建DOS文件系统 mkdosfs命令使用指南
31 2
|
1月前
|
算法 Linux Shell
【Shell 命令集合 磁盘管理 】Linux 于挂入MS-DOS文件系统 mmount 命令使用指南
【Shell 命令集合 磁盘管理 】Linux 于挂入MS-DOS文件系统 mmount 命令使用指南
26 0
|
3月前
|
关系型数据库 数据库连接 Windows
windows 常用的dos命令
windows 常用的dos命令
64 0
|
3月前
|
关系型数据库 MySQL 数据库
Python tk dos命令备份mysql数据库
Python tk dos命令备份mysql数据库
25 0
|
1月前
|
存储 Linux Shell
【Shell 命令集合 磁盘维护 】Linux 创建MS-DOS文件系统 mkfs.msdos命令使用教程
【Shell 命令集合 磁盘维护 】Linux 创建MS-DOS文件系统 mkfs.msdos命令使用教程
29 0
|
4月前
|
Java 程序员 Shell
Java(一)java跨平台原理及dos常用命令
Java(一)java跨平台原理及dos常用命令
20 1
|
5月前
|
应用服务中间件 nginx Windows
windows下DOS命令杀掉Nginx应用进程
windows下DOS命令杀掉Nginx应用进程
|
6月前
|
缓存 安全 网络协议
花无涯带你走进黑客世界7 Dos命令
为什么电影中的黑客几乎绝不用鼠标,都是键盘一通乱敲?第七章,开始实战各方面的软件安装和平台的了解, 把术语讲清楚慢慢一步步的来过渡了解与学习,帮助更多人学习到黑客攻防,学会保护自己和身边的人,主要是谈谈小白如何慢慢学习网络安全相关知识,有正确得价值观,做正确的事情,欢迎各界人士给建议。相信每一个对计算机感兴趣的童鞋都有着一颗黑客的心,我希望通过一系列的文章让大家了解黑客和网络安全。
|
1月前
|
存储 Shell Linux
【Shell 命令集合 磁盘管理 】Linux 设置或者显示MS-DOS文件系统中的卷标 mlabel命令使用教程
【Shell 命令集合 磁盘管理 】Linux 设置或者显示MS-DOS文件系统中的卷标 mlabel命令使用教程
38 0
|
5月前
|
缓存 网络协议 网络安全
Dos常用命令及解释大全
前言 DOS是"磁盘操作系统"(Disk Operating System)的缩写。它是一种早期的操作系统,最初在20世纪80年代广泛用于个人计算机。DOS主要用于控制计算机硬件和管理文件系统,提供一组命令行界面(CLI)用于用户与操作系统进行交互。DOS的一个著名版本是微软的MS-DOS,它是IBM PC和兼容机中使用的主要操作系统。随着图形用户界面的兴起,DOS逐渐被更先进的操作系统如Windows所取代。
443 0

热门文章

最新文章