Click here to Skip to main content
15,899,679 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
devgo9-Nov-09 7:28
devgo9-Nov-09 7:28 
QuestionRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
David Crow9-Nov-09 7:52
David Crow9-Nov-09 7:52 
GeneralRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
Randor 9-Nov-09 7:54
professional Randor 9-Nov-09 7:54 
GeneralRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
devgo9-Nov-09 9:18
devgo9-Nov-09 9:18 
GeneralRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
Randor 9-Nov-09 10:19
professional Randor 9-Nov-09 10:19 
GeneralRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
devgo9-Nov-09 10:56
devgo9-Nov-09 10:56 
GeneralRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
devgo11-Nov-09 9:15
devgo11-Nov-09 9:15 
GeneralRe: how to use createprocess to create a new console window(cmd.exe) and run commands Pin
Randor 11-Nov-09 16:34
professional Randor 11-Nov-09 16:34 
Hi Devgo,

In the future you should not reply to yourself. You are lucky that I review my old threads... I did not get notification of your response because you replied to yourself. Anyway I wrote a quick and dirty sample for you. In this example I am creating an instance of CMD.EXE and writind the Dir command to its STDIN pipe. I then use MessageBox to show the results.



C++
#include "stdafx.h"
#include <windows.h> 
#include <tchar.h>
#include <stdio.h>
#include <process.h>

#pragma comment(lib,"user32.lib")

HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_IN_Wr = NULL;
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;

unsigned __stdcall ReadCMDThread(void* pArguments)
{
	DWORD dwAvailable =0;
	DWORD bytesRead =0;
	CHAR szOut[4096] = {0};
	while(0 == bytesRead)
	{
		PeekNamedPipe(g_hChildStd_OUT_Rd,szOut,sizeof(szOut),&bytesRead,&dwAvailable,NULL);
		if(0 != bytesRead)
		{
			Sleep(1000);
			ReadFile(g_hChildStd_OUT_Rd,szOut,sizeof(szOut),&bytesRead,NULL);
			::MessageBoxA(NULL,szOut,"",MB_OK);
			break;
		}
	}
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	DWORD bytesRead;
	DWORD exitcode;
	PROCESS_INFORMATION pi;
	STARTUPINFO si;
	SECURITY_ATTRIBUTES sa;

	ZeroMemory(&sa,sizeof(sa));
	sa.nLength=sizeof(SECURITY_ATTRIBUTES);
	sa.bInheritHandle=TRUE;

	CreatePipe(&g_hChildStd_IN_Rd,&g_hChildStd_IN_Wr,&sa,0);
	CreatePipe(&g_hChildStd_OUT_Rd,&g_hChildStd_OUT_Wr,&sa,0);

	ZeroMemory(&si,sizeof(si));
	GetStartupInfo(&si);

	si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
	si.hStdOutput = g_hChildStd_OUT_Wr;
	si.hStdError = g_hChildStd_OUT_Wr;
	si.hStdInput = g_hChildStd_IN_Rd;
	si.wShowWindow = SW_HIDE;
	
	BOOL bSuccess = CreateProcess(
		_T("C:\\windows\\system32\\cmd.exe"),
		NULL,
		NULL,
		NULL,
		TRUE,
		CREATE_NEW_CONSOLE,
		NULL,
		NULL,
		&si,
		&pi);

	UINT threadID;
	HANDLE hThread = (HANDLE)_beginthreadex(NULL,0,&ReadCMDThread,NULL,0,&threadID);
	
	Sleep(500);

	if(INVALID_HANDLE_VALUE != hThread)
	{
		DWORD dwWritten = 0;
		CHAR szIN[4096];

		strcpy(szIN,"dir\n");
		WriteFile(g_hChildStd_IN_Wr,szIN,strlen(szIN),&dwWritten,NULL);
		
		WaitForSingleObject(hThread,INFINITE);
		CloseHandle(hThread);

		strcpy(szIN,"exit\n");
		WriteFile(g_hChildStd_IN_Wr,szIN,strlen(szIN),&dwWritten,NULL);
	}

	CloseHandle(g_hChildStd_IN_Rd);
	CloseHandle(g_hChildStd_IN_Wr);
	CloseHandle(g_hChildStd_OUT_Rd);
	CloseHandle(g_hChildStd_OUT_Wr);
	return 0;
}


The code is ugly and contains some Sleeps to allow CMD.EXE to initialize. I also use Sleep to wait for the Read pipe to become full. There are probably better ways to do this. Also... on my Windows7 workstation I had problems terminating CMD.exe by closing its handles. Using TerminateProcess is generally not desirable so I opted to write exit to the STDIN stream.

There are some other functions that you might want to get familiar with:

WriteConsoleInput Function[^]
GenerateConsoleCtrlEvent Function[^]

Good Luck,
-David Delaune
QuestionIs there a way to register an activeX ocx file with relative path and not full path? Pin
eyal_balla9-Nov-09 4:31
eyal_balla9-Nov-09 4:31 
AnswerRe: Is there a way to register an activeX ocx file with relative path and not full path? Pin
Chris Losinger9-Nov-09 5:34
professionalChris Losinger9-Nov-09 5:34 
QuestionHow do I get contents of long strings in VS 2008 debugger? Pin
Interrobang9-Nov-09 4:28
Interrobang9-Nov-09 4:28 
AnswerRe: How do I get contents of long strings in VS 2008 debugger? Pin
loyal ginger9-Nov-09 4:48
loyal ginger9-Nov-09 4:48 
AnswerRe: How do I get contents of long strings in VS 2008 debugger? Pin
Chris Losinger9-Nov-09 4:50
professionalChris Losinger9-Nov-09 4:50 
QuestionSimple CToolTipCtrl trouble. Pin
Nikz29-Nov-09 4:20
Nikz29-Nov-09 4:20 
AnswerRe: Simple CToolTipCtrl trouble. Pin
Code-o-mat9-Nov-09 5:24
Code-o-mat9-Nov-09 5:24 
GeneralRe: Simple CToolTipCtrl trouble. Pin
Nikz29-Nov-09 5:52
Nikz29-Nov-09 5:52 
GeneralRe: Simple CToolTipCtrl trouble. Pin
Code-o-mat9-Nov-09 5:59
Code-o-mat9-Nov-09 5:59 
GeneralRe: Simple CToolTipCtrl trouble. Pin
Nikz29-Nov-09 6:13
Nikz29-Nov-09 6:13 
GeneralRe: Simple CToolTipCtrl trouble. Pin
Code-o-mat9-Nov-09 7:30
Code-o-mat9-Nov-09 7:30 
GeneralRe: Simple CToolTipCtrl trouble. Pin
Nikz29-Nov-09 10:48
Nikz29-Nov-09 10:48 
Question'c' Program [modified] Pin
singh.niraj40@yahoo.com9-Nov-09 4:01
singh.niraj40@yahoo.com9-Nov-09 4:01 
AnswerRe: 'c' Program Pin
Chris Losinger9-Nov-09 4:03
professionalChris Losinger9-Nov-09 4:03 
AnswerRe: 'c' Program Pin
David Crow9-Nov-09 4:21
David Crow9-Nov-09 4:21 
JokeRe: 'c' Program Pin
Cedric Moonen9-Nov-09 4:32
Cedric Moonen9-Nov-09 4:32 
AnswerRe: 'c' Program Pin
Michael Schubert9-Nov-09 6:03
Michael Schubert9-Nov-09 6:03 

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.