关于控制台程序的命令写入,请指教。

ExitWindows 2005-12-05 11:24:52
我想在一个对话框程序中,按下某按钮后执行“cmd”,然后写入“dir\r\n”命令,程序报告写入了5个字节,但是cmd窗口没反应。
大侠能否看看我的这段代码有什么毛病,谢谢。
void CCmdPipeDlg::OnButtonDo()
{
// TODO: Add your control notification handler code here
//创建单个进程,通过管道写入命令
SECURITY_ATTRIBUTES sat = {0};
sat.nLength = sizeof( SECURITY_ATTRIBUTES );
sat.lpSecurityDescriptor = NULL;
sat.bInheritHandle = TRUE;
HANDLE hPipeRead1=NULL, hPipeWrite1=NULL;
HANDLE hPipeRead2=NULL, hPipeWrite2=NULL;
BOOL bPipeOk1 = CreatePipe( &hPipeRead1, &hPipeWrite1, &sat, NULL );
BOOL bPipeOk2 = CreatePipe( &hPipeRead2, &hPipeWrite2, &sat, NULL );
if( !bPipeOk1 || !bPipeOk2 )
{
return;
}
STARTUPINFO StartUpInfo = {0};
PROCESS_INFORMATION ProInfo = {0};
StartUpInfo.cb = sizeof( StartUpInfo );
StartUpInfo.lpReserved = NULL;
StartUpInfo.dwFlags = 0;
StartUpInfo.cbReserved2 = 0;
StartUpInfo.lpReserved2 = NULL;
StartUpInfo.lpDesktop = NULL;
StartUpInfo.lpTitle = NULL;
StartUpInfo.dwX = 0;
StartUpInfo.dwY = 0;
StartUpInfo.dwXSize = 0;
StartUpInfo.dwYSize = 0;
StartUpInfo.dwXCountChars = 0;
StartUpInfo.dwYCountChars = 0;
StartUpInfo.dwFillAttribute = 0;
StartUpInfo.dwFlags = NULL;
StartUpInfo.wShowWindow = SW_SHOW;
StartUpInfo.hStdInput = hPipeRead1;
StartUpInfo.hStdOutput = hPipeWrite2;//NULL;
StartUpInfo.hStdError = hPipeWrite2;//NULL;
BOOL bProOk = CreateProcess( NULL, "cmd",
NULL, NULL, TRUE, NULL, NULL, NULL,
&StartUpInfo, &ProInfo );
if( !bProOk )
{
return;
}
CloseHandle( hPipeRead1 );
CloseHandle( hPipeWrite2 );

char szCmdLine[1024] = "dir\r\n";
// 写入命令
ULONG uWrited = 0;
BOOL bWriteOk = WriteFile( hPipeWrite1, szCmdLine, strlen(szCmdLine), &uWrited, NULL );
char szWrited[256] = {0};
sprintf( szWrited, "writed: %d", uWrited );
MessageBox( szWrited );

// 关闭进程
CloseHandle( hPipeRead2 );
CloseHandle( hPipeWrite1 );
CloseHandle( ProInfo.hProcess);

return;
}
...全文
198 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixiaosan 2005-12-05
  • 打赏
  • 举报
回复
http://www.codeguru.com/Cpp/W-D/console/

Redirection
38062708 2005-12-05
  • 打赏
  • 举报
回复
回复人: laiyiling(【龙工一号●CSDN】) ( ) 信誉:498 2005-12-05 15:03:00 得分: 0


QuickWin - Console Application Into a Window
http://www.codeguru.com/Cpp/W-D/console/redirection/article.php/c541/


--------------------------------------------

看起来不错哦哦
hjunxu 2005-12-05
  • 打赏
  • 举报
回复
上面是msdn的例子。
hjunxu 2005-12-05
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <windows.h>

#define BUFSIZE 4096

HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
hInputFile, hSaveStdin, hSaveStdout;

BOOL CreateChildProcess(VOID);
VOID WriteToPipe(VOID);
VOID ReadFromPipe(VOID);
VOID ErrorExit(LPTSTR);
VOID ErrMsg(LPTSTR, BOOL);

DWORD main(int argc, char *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;

// The steps for redirecting child process's STDOUT:
// 1. Save current STDOUT, to be restored later.
// 2. Create anonymous pipe to be STDOUT for child process.
// 3. Set STDOUT of the parent process to be write handle to
// the pipe, so it is inherited by the child process.
// 4. Create a noninheritable duplicate of the read handle and
// close the inheritable read handle.

// Save the handle to the current STDOUT.

hSaveStdout = 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");

// Set a write handle to the pipe to be STDOUT.

if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr))
ErrorExit("Redirecting STDOUT failed");

// 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);

// The steps for redirecting child process's STDIN:
// 1. Save current STDIN, to be restored later.
// 2. Create anonymous pipe to be STDIN for child process.
// 3. Set STDIN of the parent to be the read handle to the
// pipe, so it is inherited by the child process.
// 4. Create a noninheritable duplicate of the write handle,
// and close the inheritable write handle.

// Save the handle to the current STDIN.

hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);

// Create a pipe for the child process's STDIN.

if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
ErrorExit("Stdin pipe creation failed\n");

// Set a read handle to the pipe to be STDIN.

if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd))
ErrorExit("Redirecting Stdin failed");

// 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.

fSuccess = CreateChildProcess();
if (! fSuccess)
ErrorExit("Create process failed");

// After process creation, restore the saved STDIN and STDOUT.

if (! SetStdHandle(STD_INPUT_HANDLE, hSaveStdin))
ErrorExit("Re-redirecting Stdin failed\n");

if (! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout))
ErrorExit("Re-redirecting Stdout failed\n");

// Get a handle to the parent's input file.

if (argc > 1)
hInputFile = CreateFile(argv[1], GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
else
hInputFile = hSaveStdin;

if (hInputFile == INVALID_HANDLE_VALUE)
ErrorExit("no input file\n");

// 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.

ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);

// Create the child process.

bFuncRetn = CreateProcess(NULL,
"child", // 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");
return 0;
} 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(hChildStdinWrDup, chBuf, dwRead,
&dwWritten, NULL)) break;
}

// Close the pipe handle so the child process stops reading.

if (! CloseHandle(hChildStdinWrDup))
ErrorExit("Close pipe failed\n");
}

VOID ReadFromPipe(VOID)
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

// 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( hChildStdoutRdDup, chBuf, BUFSIZE, &dwRead,
NULL) || dwRead == 0) break;
if (! WriteFile(hSaveStdout, chBuf, dwRead, &dwWritten, NULL))
break;
}
}

VOID ErrorExit (LPTSTR lpszMessage)
{
fprintf(stderr, "%s\n", lpszMessage);
ExitProcess(0);
}

// The code for the child process.

#include <windows.h>
#define BUFSIZE 4096

VOID main(VOID)
{
CHAR chBuf[BUFSIZE];
DWORD dwRead, dwWritten;
HANDLE hStdin, hStdout;
BOOL fSuccess;

hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if ((hStdout == INVALID_HANDLE_VALUE) ||
(hStdin == INVALID_HANDLE_VALUE))
ExitProcess(1);

for (;;)
{
// Read from standard input.
fSuccess = ReadFile(hStdin, chBuf, BUFSIZE, &dwRead, NULL);
if (! fSuccess || dwRead == 0)
break;

// Write to standard output.
fSuccess = WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL);
if (! fSuccess)
break;
}
}

Kudeet 2005-12-05
  • 打赏
  • 举报
回复
QuickWin - Console Application Into a Window
http://www.codeguru.com/Cpp/W-D/console/redirection/article.php/c541/

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧