Click here to Skip to main content
15,887,027 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec18-Feb-18 19:13
Łukasz Gęsieniec18-Feb-18 19:13 
AnswerRe: How to redirect WriteFile func writes to console Pin
leon de boer18-Feb-18 19:59
leon de boer18-Feb-18 19:59 
GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec18-Feb-18 20:10
Łukasz Gęsieniec18-Feb-18 20:10 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer18-Feb-18 21:06
leon de boer18-Feb-18 21:06 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer18-Feb-18 22:45
leon de boer18-Feb-18 22:45 
GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec19-Feb-18 0:34
Łukasz Gęsieniec19-Feb-18 0:34 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer19-Feb-18 5:48
leon de boer19-Feb-18 5:48 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer19-Feb-18 6:19
leon de boer19-Feb-18 6:19 
Okay so this spawns the thread before it executes the command passed in.
It should spit the console even if the command doesn't terminate, which I still find strange.

#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <tchar.h>

FILE* OutStream;
FILE* InStream;
FILE* ErrStream;

struct execData {
	TCHAR* CmdLine;
	TCHAR* CmdRunDir;
	PROCESS_INFORMATION  process_info;
	STARTUPINFO  startup_info;
};

struct execData		 data = { 0 };

/*---------------------------------------------------------------------------
Application handler.
---------------------------------------------------------------------------*/
LRESULT CALLBACK GUIDemoHandler (HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
	switch (Msg) {
		case WM_CREATE:
			AllocConsole();  //Allocate a console
			freopen_s(&InStream, "CONIN$", "r", stdin); // Redirect standard in
			freopen_s(&OutStream, "CONOUT$", "w", stdout);  // Redirect standard out
			freopen_s(&ErrStream, "CONOUT$", "w", stderr);  // Redirect standard error
			break;
		case WM_DESTROY:											// WM_DESTROY MESSAGE
			if (data.process_info.hProcess) TerminateProcess(data.process_info.hProcess, 0);
			FreeConsole();
			PostQuitMessage(0);										// Post quit message
			break;

		default: return DefWindowProc(Wnd, Msg, wParam, lParam);	// Default handler
	};// end switch case
	return 0;
}

DWORD WINAPI ExecThreadFunction(LPVOID lpParam)
{
	int  Success;
	struct execData* data = (struct execData*)lpParam;
	Success = CreateProcess(
		nullptr,
		data->CmdLine,
		nullptr,
		nullptr,
		TRUE,
		0,
		nullptr,
		data->CmdRunDir,
		&data->startup_info,
		&data->process_info
	);

	if (!Success) {
		CloseHandle(data->process_info.hProcess);
		CloseHandle(data->process_info.hThread);
		return -4;
	}
	else {
		CloseHandle(data->process_info.hThread);
	}

	WaitForSingleObject(data->process_info.hProcess, INFINITE);

	CloseHandle(data->process_info.hProcess);
	
	data->process_info.hProcess = 0;
	return (0);
}



int SystemCapture(
	TCHAR*         CmdLine,    //Command Line
	TCHAR*         CmdRunDir)  //set to '.' for current directory
{
	SECURITY_ATTRIBUTES  security_attributes;             

	data.CmdLine = CmdLine;
	data.CmdRunDir = CmdRunDir;
	security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
	security_attributes.bInheritHandle = TRUE;
	security_attributes.lpSecurityDescriptor = nullptr;
	data.startup_info.cb = sizeof(STARTUPINFO);
	data.startup_info.hStdInput = 0;
	data.startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	data.startup_info.hStdError = 0;
	data.startup_info.dwFlags |= STARTF_USESTDHANDLES;

	CreateThread(NULL, 0, ExecThreadFunction, (LPVOID)&data, 0, NULL);
	
	return 0;
}



const TCHAR* ClassName = _T("GUI_CONSOLE_APP");

/* ------------------------------------------------------------------------
   The application entry point
   -----------------------------------------------------------------------*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
	MSG Msg;
	HWND Wnd;

	WNDCLASSEX WndClass = { 0 };
	WndClass.cbSize = sizeof(WNDCLASSEX);							// Size of this record
	WndClass.style = CS_OWNDC;										// Class styles
	WndClass.lpfnWndProc = &GUIDemoHandler;							// Handler for this class
	WndClass.cbClsExtra = 0;										// No extra class data
	WndClass.cbWndExtra = 0;										// No extra window data
	WndClass.hInstance = GetModuleHandle(NULL);						// This instance
	WndClass.hIcon = LoadIcon(0, IDI_APPLICATION);					// Set icon
	WndClass.hCursor = LoadCursor(0, IDC_ARROW);					// Set cursor
	WndClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);	// Set background brush
	WndClass.lpszMenuName = NULL;									// No menu yet
	WndClass.lpszClassName = ClassName;								// Set class name
	RegisterClassEx(&WndClass);										// Register the class				
   	Wnd = CreateWindowEx(0, ClassName, _T("GUI Console Demo Program"), 
		WS_VISIBLE | WS_OVERLAPPEDWINDOW, 
		50, 50, 500, 400,
		0, 0, 0, NULL);												// Create a window

	_tprintf(_T("This will go to the console\r\n"));
	std::cout << "Second line to console\r\n" << std::endl;

	int            rc;
	TCHAR szCmdDir[256] = _T(".");
	std::cout << "\r\nExecute console app:" << std::endl;
	TCHAR szCmdLine1[256] = _T("ConsoleApp.exe");    // You strange non terminating app
	rc = SystemCapture(
		szCmdLine1,								 //Command Line
		szCmdDir                               //CmdRunDir
	);

 	while (GetMessage(&Msg, 0, 0, 0)){								// Get messages
		TranslateMessage(&Msg);										// Translate each message
		DispatchMessage(&Msg);										// Dispatch each message
	};
	return (0);
}


I made a strange console app that just spins the cursor to test it worked which was called ConsoleApp.exe hence my name above
static const char Spin[4] = { '|', '/', '-', '\\' };



int main()
{

	int i = 0;
	while (1) {
		printf("Deadloop %c\r", Spin[i]);
		for (long j = 0; j < 10000000; j++) {};
		i++;
		i %= 4;
	}

    return 0;
}

In vino veritas


modified 19-Feb-18 13:37pm.

GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec19-Feb-18 9:06
Łukasz Gęsieniec19-Feb-18 9:06 
GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec19-Feb-18 19:53
Łukasz Gęsieniec19-Feb-18 19:53 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer19-Feb-18 20:14
leon de boer19-Feb-18 20:14 
GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec19-Feb-18 22:34
Łukasz Gęsieniec19-Feb-18 22:34 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer20-Feb-18 6:18
leon de boer20-Feb-18 6:18 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer20-Feb-18 6:33
leon de boer20-Feb-18 6:33 
GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec20-Feb-18 23:32
Łukasz Gęsieniec20-Feb-18 23:32 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer21-Feb-18 7:12
leon de boer21-Feb-18 7:12 
GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec21-Feb-18 19:57
Łukasz Gęsieniec21-Feb-18 19:57 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer22-Feb-18 14:19
leon de boer22-Feb-18 14:19 
GeneralRe: How to redirect WriteFile func writes to console Pin
Łukasz Gęsieniec22-Feb-18 19:50
Łukasz Gęsieniec22-Feb-18 19:50 
GeneralRe: How to redirect WriteFile func writes to console Pin
Peter_in_278022-Feb-18 20:21
professionalPeter_in_278022-Feb-18 20:21 
GeneralRe: How to redirect WriteFile func writes to console Pin
leon de boer22-Feb-18 21:06
leon de boer22-Feb-18 21:06 
Questionproblem defining a function inside a structure in c Pin
Member 1367832014-Feb-18 5:40
Member 1367832014-Feb-18 5:40 
AnswerRe: problem defining a function inside a structure in c Pin
David Crow14-Feb-18 6:09
David Crow14-Feb-18 6:09 
AnswerRe: problem defining a function inside a structure in c Pin
CPallini14-Feb-18 10:44
mveCPallini14-Feb-18 10:44 
Questionsetting Boundary condition X^2-Y^2 for solving Laplace equation for NxN grid. Pin
Member 1362564310-Feb-18 17:23
Member 1362564310-Feb-18 17:23 

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.