Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: COM usage issue Pin
CPallini22-Feb-18 0:30
mveCPallini22-Feb-18 0:30 
QuestionCleaver macro (string concatenation?) or function to optimize size of array by turning it into a struct/linked list? Pin
arnold_w19-Feb-18 22:16
arnold_w19-Feb-18 22:16 
AnswerRe: Cleaver macro (string concatenation?) or function to optimize size of array by turning it into a struct/linked list? Pin
leon de boer20-Feb-18 7:53
leon de boer20-Feb-18 7:53 
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 
here see if this helps

It executes IPConfig and executes command.com to display a directory all redirected up to the windows console.

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

using namespace std;

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

/*---------------------------------------------------------------------------
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
			FreeConsole();
			PostQuitMessage(0);										// Post quit message
			break;

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




int SystemCapture(
	TCHAR*         CmdLine,    //Command Line
	TCHAR*         CmdRunDir,  //set to '.' for current directory
	uint32_t&      RetCode)    //Return Exit Code
{
	int                  Success;
	SECURITY_ATTRIBUTES  security_attributes;
	HANDLE               stdout_rd = INVALID_HANDLE_VALUE;
	HANDLE               stdout_wr = INVALID_HANDLE_VALUE;
	PROCESS_INFORMATION  process_info = { 0 };
	STARTUPINFO          startup_info = { 0 };
	thread               stdout_thread;

	security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
	security_attributes.bInheritHandle = TRUE;
	security_attributes.lpSecurityDescriptor = nullptr;

	if (!CreatePipe(&stdout_rd, &stdout_wr, &security_attributes, 0) ||
		!SetHandleInformation(stdout_rd, HANDLE_FLAG_INHERIT, 0)) {
		return -1;
	}

	startup_info.cb = sizeof(STARTUPINFO);
	startup_info.hStdInput = 0;
	startup_info.hStdOutput = stdout_wr;
	startup_info.hStdError = 0;

	if (stdout_rd)
		startup_info.dwFlags |= STARTF_USESTDHANDLES;

	Success = CreateProcess(
		nullptr,
		CmdLine,
		nullptr,
		nullptr,
		TRUE,
		0,
		nullptr,
		CmdRunDir,
		&startup_info,
		&process_info
	);
	CloseHandle(stdout_wr);

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

	if (stdout_rd) {

		stdout_thread = thread([&]() {
			DWORD  n;	
			for (;;) {
				const size_t bufsize = 256;
				char buffer[bufsize];
				n = 0;
				int Success = ReadFile(
					stdout_rd,
					buffer,
					(DWORD)bufsize-1,
					&n,
					nullptr
				);
				if (!Success || n == 0)
					break;
				buffer[n] = '\0';
				printf(&buffer[0]);
			}
		});
	}


	WaitForSingleObject(process_info.hProcess, INFINITE);
	if (!GetExitCodeProcess(process_info.hProcess, (DWORD*)&RetCode))
		RetCode = -1;

	CloseHandle(process_info.hProcess);

	if (stdout_thread.joinable())
		stdout_thread.join();

	CloseHandle(stdout_rd);

	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;

	std::cout << "Execute IPConfig and display result:" << std::endl;
	int            rc;
	uint32_t       RetCode;
	TCHAR szCmdLine[256] = _T("C:\\Windows\\System32\\ipconfig.exe");
	TCHAR szCmdDir[256] = _T(".");

	rc = SystemCapture(
		szCmdLine,								 //Command Line
		szCmdDir,                                //CmdRunDir
		RetCode                                  //Return Exit Code
	);



	std::cout << "\r\nExecute dir and display result:" << std::endl;
	TCHAR szCmdLine1[256] = _T("cmd.exe /C dir");
	rc = SystemCapture(
		szCmdLine1,								 //Command Line
		szCmdDir,                                //CmdRunDir
		RetCode                                  //Return Exit Code
	);


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

In vino veritas


modified 19-Feb-18 5:13am.

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

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.