Click here to Skip to main content
16,012,821 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Drag and Drop applications in MFC Pin
Ganesh_T16-Jun-06 1:49
Ganesh_T16-Jun-06 1:49 
AnswerRe: Drag and Drop applications in MFC Pin
David Crow16-Jun-06 2:50
David Crow16-Jun-06 2:50 
QuestionAbout button Pin
sdhtyjnniutnbjnhbghb15-Jun-06 23:43
sdhtyjnniutnbjnhbghb15-Jun-06 23:43 
Questionadding objects Pin
Di Troia15-Jun-06 23:13
Di Troia15-Jun-06 23:13 
AnswerRe: adding objects Pin
Cedric Moonen15-Jun-06 23:15
Cedric Moonen15-Jun-06 23:15 
GeneralRe: adding objects [modified] Pin
Maxwell Chen16-Jun-06 0:21
Maxwell Chen16-Jun-06 0:21 
GeneralRe: adding objects Pin
toxcct16-Jun-06 0:28
toxcct16-Jun-06 0:28 
QuestionCreateProcess + CreatePipe works partially Pin
nmx_de15-Jun-06 23:11
nmx_de15-Jun-06 23:11 
Hi all.

It's me again with another question regarding calling a process out of my program and reuse the output of it. I managed to get the output of the child process by using CreatePipe and CreateProcess (like described in the MSDN article Creating a Child Process with Redirected Input and Output). The problem is: i can't run any program i like. I tried with ipconfig and it works as a charm, when i tried dir, i get the filenames of the directory, but not as expected. I can't get the same output as in the command shell.

Example (i call a simple dir command from my program):
<br />
ExecCommand cmd;<br />
char* myCmd = cmd.Execute("dir C:\\");<br />


myCmd:
AUTOEXEC.BAT   NTDETECT.COM   hiberfil.sys
bla.bat   bla2.bat   blubb.exe
...   ...   ...


shouldn't the dir command return something like:
 Volume in drive C is System
 Volume Serial Number is D415-C6D2

 Directory of C:\

05.04.2006  12:12                 0 AUTOEXEC.BAT
05.04.2006  12:12                 0 CONFIG.SYS
06.04.2006  10:34    <DIR>          bla
...


And what about the return of a directory that contains a lot of files (like C:\WINDOWS\system32\)
My application freezes when i do char* myCmd = cmd.Execute("dir C:\\WINDOWS\\system32\\")

Thanks in advance,
n.


<br />
#include "stdafx.h"<br />
#include "ExecCommand.h"<br />
<br />
#define BUFSIZE 4096<br />
 <br />
HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, hInputFile, hStdout;<br />
<br />
//////////////////////////////////////////////////////////////////////<br />
// Construction/Destruction<br />
//////////////////////////////////////////////////////////////////////<br />
<br />
ExecCommand::ExecCommand()<br />
{<br />
<br />
}<br />
<br />
ExecCommand::~ExecCommand()<br />
{<br />
<br />
}<br />
 <br />
<br />
char* ExecCommand::Execute(char* szCmdline)<br />
{<br />
	SECURITY_ATTRIBUTES saAttr;<br />
	bool fSuccess = false;<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 />
	// Get the handle to the current STDOUT.<br />
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);<br />
	<br />
	// Create a pipe for the child process's STDOUT.<br />
	if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))<br />
		ErrorExit("Stdout pipe creation failed\n");<br />
	<br />
	// Ensure the read handle to the pipe for STDOUT is not inherited.<br />
	SetHandleInformation(hChildStdoutRd, HANDLE_FLAG_INHERIT, 0);<br />
<br />
	// Now create the child process.<br />
	fSuccess = CreateChildProcess(szCmdline);<br />
	if (! fSuccess)<br />
		ErrorExit("Create process failed with");<br />
	<br />
<br />
	// Read from pipe that is the standard output for child process.<br />
	return ReadFromPipe();<br />
	<br />
}<br />
<br />
bool ExecCommand::CreateChildProcess(char* szCmdline)<br />
{ <br />
<br />
	PROCESS_INFORMATION piProcInfo;<br />
	STARTUPINFO siStartInfo;<br />
	bool bFuncRetn = false;<br />
	<br />
	// Set up members of the PROCESS_INFORMATION structure. <br />
	ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));<br />
	<br />
	// Set up members of the STARTUPINFO structure.<br />
	ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));<br />
	siStartInfo.cb = sizeof(STARTUPINFO);<br />
	siStartInfo.hStdError = hChildStdoutWr;<br />
	siStartInfo.hStdOutput = hChildStdoutWr;<br />
	siStartInfo.hStdInput = hChildStdinRd;<br />
	siStartInfo.wShowWindow = SW_HIDE; <br />
	siStartInfo.dwFlags |= STARTF_USESTDHANDLES;<br />
	siStartInfo.dwFlags |= STARTF_USESHOWWINDOW;<br />
	<br />
	// Create the child process. <br />
	bFuncRetn = CreateProcess(NULL, <br />
		szCmdline,     // 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 />
<br />
	if (bFuncRetn == 0)	ErrorExit("CreateProcess failed\n");<br />
	else<br />
	{<br />
		WaitForSingleObject(piProcInfo.hProcess, INFINITE);<br />
		CloseHandle(piProcInfo.hProcess);<br />
		CloseHandle(piProcInfo.hThread);<br />
		return bFuncRetn;<br />
	}<br />
}<br />
<br />
char* ExecCommand::ReadFromPipe()<br />
{<br />
	DWORD dwRead, dwWritten;<br />
	static char chBuf[BUFSIZE];<br />
	<br />
	// Close the write end of the pipe before reading from the<br />
	// read end of the pipe.<br />
	if (!CloseHandle(hChildStdoutWr))<br />
		ErrorExit("Closing handle failed");<br />
	<br />
	// Read output from the child process, and write to parent's STDOUT.<br />
	for (;;)<br />
	{<br />
		if (! ReadFile(hChildStdoutRd, chBuf, BUFSIZE, &dwRead, NULL) || dwRead == 0) break;<br />
		if (! WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL)) break;<br />
	}<br />
<br />
	return chBuf;<br />
}<br />
<br />
void ExecCommand::ErrorExit(LPTSTR lpszMessage)<br />
{<br />
	fprintf(stderr, "%s\n", lpszMessage);<br />
	ExitProcess(0);<br />
}<br />

AnswerRe: CreateProcess + CreatePipe works partially Pin
David Crow16-Jun-06 2:54
David Crow16-Jun-06 2:54 
Questionusing variables in insert query for Access Pin
shuchigo_jane15-Jun-06 22:18
shuchigo_jane15-Jun-06 22:18 
AnswerRe: using variables in insert query for Access Pin
Cedric Moonen15-Jun-06 22:31
Cedric Moonen15-Jun-06 22:31 
GeneralRe: using variables in insert query for Access Pin
shuchigo_jane15-Jun-06 22:40
shuchigo_jane15-Jun-06 22:40 
GeneralRe: using variables in insert query for Access Pin
Cedric Moonen15-Jun-06 22:49
Cedric Moonen15-Jun-06 22:49 
GeneralRe: using variables in insert query for Access Pin
shuchigo_jane15-Jun-06 22:56
shuchigo_jane15-Jun-06 22:56 
GeneralRe: using variables in insert query for Access Pin
Cedric Moonen15-Jun-06 22:59
Cedric Moonen15-Jun-06 22:59 
GeneralRe: using variables in insert query for Access Pin
shuchigo_jane15-Jun-06 23:10
shuchigo_jane15-Jun-06 23:10 
GeneralRe: using variables in insert query for Access Pin
Cedric Moonen15-Jun-06 23:14
Cedric Moonen15-Jun-06 23:14 
GeneralRe: using variables in insert query for Access Pin
shuchigo_jane15-Jun-06 23:17
shuchigo_jane15-Jun-06 23:17 
GeneralRe: using variables in insert query for Access Pin
Cedric Moonen15-Jun-06 23:19
Cedric Moonen15-Jun-06 23:19 
GeneralRe: using variables in insert query for Access Pin
David Crow16-Jun-06 3:03
David Crow16-Jun-06 3:03 
AnswerRe: using variables in insert query for Access Pin
Viorel.15-Jun-06 22:58
Viorel.15-Jun-06 22:58 
AnswerRe: using variables in insert query for Access Pin
Milton Karimbekallil15-Jun-06 23:56
Milton Karimbekallil15-Jun-06 23:56 
GeneralRe: using variables in insert query for Access Pin
shuchigo_jane16-Jun-06 0:02
shuchigo_jane16-Jun-06 0:02 
GeneralRe: using variables in insert query for Access Pin
Viorel.16-Jun-06 1:38
Viorel.16-Jun-06 1:38 
QuestionFileHeader Pin
Rinu_Raj15-Jun-06 20:56
Rinu_Raj15-Jun-06 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.