最近给公司的一个系统写了个启动的脚本,可是领导说批处理这样的脚本太low了,要使用EXE来启动,将来还要使用加密工具对EXE进行加密。windows
好吧,我就在网上处处找bat转exe的工具,找了好久,都没有找到合适的,只有一个用winrar制做自解压包的方法还算能够,可是这玩意儿有两个坑爹的问题:安全
因此最好的办法看来就是本身写一个exe了,考虑到我之前用过C,所以下载了Dev-Cpp这个工具来编写代码。ide
在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窗口中输入命令的状况:spa
CHAR cmds[] = "@ECHO OFF\n" "cd..\n" "dir\n"
再而后,原来的示例代码中是把批处理文件做为EXE的参数传递进来的,既然上面改成将批处理文件内容放到脚本里,代码中从文件中读取命令的那部分就要去掉了,这部分代码就很少说了。线程
完整的示例代码以下:code
#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); }
执行效果以下图:对象
main.exe的原始目录是D:\Workspace\research\C\Chrome\,执行时,首先执行了cd..,退到上一层目录,而后执行dir,显示上一层目录的内容,证实上面的代码确实能够一次执行多条DOS命令。