VC调用CMD命令通过控制台管道获取输出结果
2012-01-18 01:39
781 查看
#include <windows.h> #include <tchar.h> #include <stdio.h> #define BUFSIZE 4096 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, hInputFile, hStdout; BOOL CreateChildProcess(VOID); VOID WriteToPipe(VOID); VOID ReadFromPipe(VOID); VOID ErrorExit(LPSTR); int _tmain(int argc, TCHAR *argv[]) { 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. 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"); // Ensure that the read handle to the child process's pipe for STDOUT is not inherited. SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0); // Create a pipe for the child process's STDIN. if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) ErrorExit("Stdin pipe creation failed\n"); // Ensure that the write handle to the child process's pipe for STDIN is not inherited. SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0); // Now create the child process. fSuccess = CreateChildProcess(); if (! fSuccess) ErrorExit("Create process failed with"); // Get a handle to the parent's input file. if (argc == 1) ErrorExit("Please specify an input file"); printf( "\nContents of %s:\n\n", argv[1]); hInputFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); if (hInputFile == INVALID_HANDLE_VALUE) ErrorExit("CreateFile 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() { TCHAR szCmdline[]=TEXT("child"); 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. ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); siStartInfo.cb = sizeof(STARTUPINFO); siStartInfo.hStdError = hChildStdoutWr; siStartInfo.hStdOutput = hChildStdoutWr; siStartInfo.hStdInput = hChildStdinRd; siStartInfo.dwFlags |= STARTF_USESTDHANDLES; // Create the child process. bFuncRetn = CreateProcess(NULL, szCmdline, // 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\n"); else { CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread); return bFuncRetn; } } VOID WriteToPipe(VOID) { DWORD dwRead, dwWritten; CHAR chBuf[BUFSIZE]; // Read from a file and write its contents to a pipe. for (;;) { if (! ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL) || dwRead == 0) break; if (! WriteFile(hChildStdinWr, chBuf, dwRead, &dwWritten, NULL)) break; } // Close the pipe handle so the child process stops reading. if (! CloseHandle(hChildStdinWr)) ErrorExit("Close pipe failed\n"); } 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("Closing handle failed"); // Read output from the child process, and write to parent's STDOUT. for (;;) { if( !ReadFile( hChildStdoutRd, chBuf, BUFSIZE, &dwRead, NULL) || dwRead == 0) break; if (! WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL)) break; } } VOID ErrorExit (LPSTR lpszMessage) { fprintf(stderr, "%s\n", lpszMessage); ExitProcess(0); }
相关文章推荐
- VC中调用CMD命令并获取调用结果
- Delphi通过管道执行外部命令行程序(cmd)并获取返回结果
- [转]用DELPHI写个简单例子,调用PING命令并且取回PING结果显示MSG BOX里,要通过管道技术取
- VC 匿名管道获取控制台程序的输出
- C#调用cmd执行命令获取返回结果
- 使用管道来获取CMD命令的结果
- 标准输出重定向加管道,获取system命令执行结果
- 【c/c++】如何调用【Window】cmd命令行命令并获取命令行的输出内容
- Delphi下调用CMD命令并获取结果
- C/C++ 通过管道的方式调用Windows ping命令 且 不显示黑窗口(cmd 窗口)
- JAVA中调用CMD命令,并输出执行结果
- C#通过获取CMD里ipconfig命令的结果来得到IP
- c#.NET 调用命令控制台 并获取执行结果
- iis php exec() system() 命令无法执行,通过cmd执行PHP可输出执行的cmd结果
- 通过管道获取cmd输出
- JAVA中调用CMD命令,并输出运行结果
- python调用外部子进程,通过管道实现异步标准输入和输出的交互
- VC中调用cmd命令的四种方式
- java使用ssh调用shell命令获取KVM数据(KVM需要通过libvirt管理)(二)
- 通过重定向来获取log4j的控制台输出