📄 subject_16963.htm
字号:
<p>
序号:16963 发表者:Biter 发表日期:2002-10-06 18:21:44
<br>主题:如何调用应用程序?
<br>内容:如何在WIN32图形界面程序里运行DOS或WIN32控制台程序,且不让弹出控制台窗口而直接获取<BR>输出字符?(有没有叫一个创建I/O端口那么一说?)谢谢帮忙!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:啊志 回复日期:2002-10-06 18:52:04
<br>内容:不会,不过可能重定向是个办法,猜猜而已
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2002-10-07 09:43:44
<br>内容:可以调用,且将DOS窗口隐掉,WINZIP就是这么干的。你可以去找找资料。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:黄飚 回复日期:2002-10-07 11:57:07
<br>内容:1先回答第一个问题,给分后再回答第二个问题(我要花时间去搞一下)<BR> WinExec(" *.exe",SW_HIDE);<BR> <BR>或这CreaeProgress()<BR><BR>2.......
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2002-10-07 13:21:31
<br>内容:CreaeProgress()??
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-07 17:09:43
<br>内容:CreateProgress写错了
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:子龙 回复日期:2002-10-08 12:59:11
<br>内容:#include <stdio.h> <BR>#include <windows.h> <BR> <BR>#define BUFSIZE 4096 <BR> <BR>HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup, <BR> hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup, <BR> hInputFile, hSaveStdin, hSaveStdout; <BR> <BR>BOOL CreateChildProcess(VOID); <BR>VOID WriteToPipe(VOID); <BR>VOID ReadFromPipe(VOID); <BR>VOID ErrorExit(LPTSTR); <BR>VOID ErrMsg(LPTSTR, BOOL); <BR> <BR>DWORD main(int argc, char *argv[]) <BR>{ <BR> SECURITY_ATTRIBUTES saAttr; <BR> BOOL fSuccess; <BR> <BR>// Set the bInheritHandle flag so pipe handles are inherited. <BR> <BR> saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); <BR> saAttr.bInheritHandle = TRUE; <BR> saAttr.lpSecurityDescriptor = NULL; <BR> <BR> // The steps for redirecting child process's STDOUT: <BR> // 1. Save current STDOUT, to be restored later. <BR> // 2. Create anonymous pipe to be STDOUT for child process. <BR> // 3. Set STDOUT of the parent process to be write handle to <BR> // the pipe, so it is inherited by the child process. <BR> // 4. Create a noninheritable duplicate of the read handle and<BR> // close the inheritable read handle. <BR> <BR>// Save the handle to the current STDOUT. <BR> <BR> hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); <BR> <BR>// Create a pipe for the child process's STDOUT. <BR> <BR> if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) <BR> ErrorExit("Stdout pipe creation failed\n"); <BR> <BR>// Set a write handle to the pipe to be STDOUT. <BR> <BR> if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr)) <BR> ErrorExit("Redirecting STDOUT failed"); <BR> <BR>// Create noninheritable read handle and close the inheritable read <BR>// handle. <BR><BR> fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,<BR> GetCurrentProcess(), &hChildStdoutRdDup , 0,<BR> FALSE,<BR> DUPLICATE_SAME_ACCESS);<BR> if( !fSuccess )<BR> ErrorExit("DuplicateHandle failed");<BR> CloseHandle(hChildStdoutRd);<BR><BR> // The steps for redirecting child process's STDIN: <BR> // 1. Save current STDIN, to be restored later. <BR> // 2. Create anonymous pipe to be STDIN for child process. <BR> // 3. Set STDIN of the parent to be the read handle to the <BR> // pipe, so it is inherited by the child process. <BR> // 4. Create a noninheritable duplicate of the write handle, <BR> // and close the inheritable write handle. <BR> <BR>// Save the handle to the current STDIN. <BR> <BR> hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); <BR> <BR>// Create a pipe for the child process's STDIN. <BR> <BR> if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) <BR> ErrorExit("Stdin pipe creation failed\n"); <BR> <BR>// Set a read handle to the pipe to be STDIN. <BR> <BR> if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd)) <BR> ErrorExit("Redirecting Stdin failed"); <BR> <BR>// Duplicate the write handle to the pipe so it is not inherited. <BR> <BR> fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, <BR> GetCurrentProcess(), &hChildStdinWrDup, 0, <BR> FALSE, // not inherited <BR> DUPLICATE_SAME_ACCESS); <BR> if (! fSuccess) <BR> ErrorExit("DuplicateHandle failed"); <BR> <BR> CloseHandle(hChildStdinWr); <BR> <BR>// Now create the child process. <BR> <BR> if (! CreateChildProcess()) <BR> ErrorExit("Create process failed"); <BR> <BR>// After process creation, restore the saved STDIN and STDOUT. <BR> <BR> if (! SetStdHandle(STD_INPUT_HANDLE, hSaveStdin)) <BR> ErrorExit("Re-redirecting Stdin failed\n"); <BR> <BR> if (! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout)) <BR> ErrorExit("Re-redirecting Stdout failed\n"); <BR> <BR>// Get a handle to the parent's input file. <BR> <BR> if (argc > 1) <BR> hInputFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, <BR> OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); <BR> else <BR> hInputFile = hSaveStdin; <BR> <BR> if (hInputFile == INVALID_HANDLE_VALUE) <BR> ErrorExit("no input file\n"); <BR> <BR>// Write to pipe that is the standard input for a child process. <BR> <BR> WriteToPipe(); <BR> <BR>// Read from pipe that is the standard output for child process. <BR> <BR> ReadFromPipe(); <BR> <BR> return 0; <BR>} <BR> <BR>BOOL CreateChildProcess() <BR>{ <BR> PROCESS_INFORMATION piProcInfo; <BR> STARTUPINFO siStartInfo; <BR> <BR>// Set up members of the PROCESS_INFORMATION structure. <BR> <BR> ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );<BR> <BR>// Set up members of the STARTUPINFO structure. <BR> <BR> ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );<BR> siStartInfo.cb = sizeof(STARTUPINFO); <BR> <BR>// Create the child process. <BR> <BR> return CreateProcess(NULL, <BR> "child", // command line <BR> NULL, // process security attributes <BR> NULL, // primary thread security attributes <BR> TRUE, // handles are inherited <BR> 0, // creation flags <BR> NULL, // use parent's environment <BR> NULL, // use parent's current directory <BR> &siStartInfo, // STARTUPINFO pointer <BR> &piProcInfo); // receives PROCESS_INFORMATION <BR>}<BR> <BR>VOID WriteToPipe(VOID) <BR>{ <BR> DWORD dwRead, dwWritten; <BR> CHAR chBuf[BUFSIZE]; <BR> <BR>// Read from a file and write its contents to a pipe. <BR> <BR> for (;;) <BR> { <BR> if (! ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL) || <BR> dwRead == 0) break; <BR> if (! WriteFile(hChildStdinWrDup, chBuf, dwRead, <BR> &dwWritten, NULL)) break; <BR> } <BR> <BR>// Close the pipe handle so the child process stops reading. <BR> <BR> if (! CloseHandle(hChildStdinWrDup)) <BR> ErrorExit("Close pipe failed\n"); <BR>} <BR> <BR>VOID ReadFromPipe(VOID) <BR>{ <BR> DWORD dwRead, dwWritten; <BR> CHAR chBuf[BUFSIZE]; <BR> HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); <BR><BR>// Close the write end of the pipe before reading from the <BR>// read end of the pipe. <BR> <BR> if (!CloseHandle(hChildStdoutWr)) <BR> ErrorExit("Closing handle failed"); <BR> <BR>// Read output from the child process, and write to parent's STDOUT. <BR> <BR> for (;;) <BR> { <BR> if( !ReadFile( hChildStdoutRdDup, chBuf, BUFSIZE, &dwRead, <BR> NULL) || dwRead == 0) break; <BR> if (! WriteFile(hSaveStdout, chBuf, dwRead, &dwWritten, NULL)) <BR> break; <BR> } <BR>} <BR> <BR>VOID ErrorExit (LPTSTR lpszMessage) <BR>{ <BR> fprintf(stderr, "%s\n", lpszMessage); <BR> ExitProcess(0); <BR>} <BR> <BR>// The code for the child process. <BR><BR>#include <windows.h> <BR>#define BUFSIZE 4096 <BR> <BR>VOID main(VOID) <BR>{ <BR> CHAR chBuf[BUFSIZE]; <BR> DWORD dwRead, dwWritten; <BR> HANDLE hStdin, hStdout; <BR> BOOL fSuccess; <BR> <BR> hStdout = GetStdHandle(STD_OUTPUT_HANDLE); <BR> hStdin = GetStdHandle(STD_INPUT_HANDLE); <BR> if ((hStdout == INVALID_HANDLE_VALUE) || <BR> (hStdin == INVALID_HANDLE_VALUE)) <BR> ExitProcess(1); <BR> <BR> for (;;) <BR> { <BR> // Read from standard input. <BR> fSuccess = ReadFile(hStdin, chBuf, BUFSIZE, &dwRead, NULL); <BR> if (! fSuccess || dwRead == 0) <BR> break; <BR> <BR> // Write to standard output. <BR> fSuccess = WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL); <BR> if (! fSuccess) <BR> break; <BR> } <BR>} <BR>Creating a Child Process with Redirected Input and Output
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Biter 回复日期:2002-10-09 19:47:47
<br>内容:感谢子龙的代码,但是我想做的只是用一下别人的控制台程序,我不能自己编写子进程。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Biter 回复日期:2002-10-09 19:52:31
<br>内容:模板(STL)不知是否试过了我 的第二个问题,在此表示感谢!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -