• Advertisement

Archived

This topic is now archived and is closed to further replies.

Reading Output from a Child Process

This topic is 5103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m using MFC to make a dialog-based app. I''m trying to get it so that if the user presses the OK button, a console app is started and the output into the console is redirected into a variable in my app (probably putting it into a CString wouldn''t be bad). I have gotten as far as the code below, but I don''t know what to put in the line filled with ''/'' so that I can get the Output into one of my own vars. The ReadConsoleOutput function doesn''t look bad, but if I use that, how do I found out what''s already been read and what''s new in the console? Thx in advance
CMyDlg dlg;
m_pMainWnd = &dlg;
int nResponse = 0;

STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

MSG msg;

do
{
	nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		ZeroMemory(&si, sizeof(si));
		si.cb = sizeof(si);
		ZeroMemory(&pi, sizeof(pi));

		if (!AllocConsole())
		{
			GLBLERRORMSGBOX("Failed to Allocate a Console");
		}
		else
		{
			HANDLE hCon = CreateFile("CONOUT$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE , &sa, OPEN_EXISTING, NULL, NULL);
			if (!hCon)
			{
				GLBLERRORMSGBOX("Could not Get Console Output Handle");
			}
			else
			{
				if (!CreateProcess(NULL, dlg.m_sCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
				{
					GLBLERRORMSGBOX("Failed to Create New Process for ds1 Editor");
				}
				else
				{
					while (WaitForSingleObject(pi.hProcess, 0) != WAIT_OBJECT_0)
					{
//////////////////////////////////////////////////////////
					}

					CloseHandle(pi.hProcess);
					CloseHandle(pi.hThread);

					CloseHandle(hCon);

					FreeConsole();
				}
			}
		}
	}
	while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {}
} while (nResponse == IDOK);
 
------------------------------------------------------ Regret Nothing - Learn Everything

Share this post


Link to post
Share on other sites
Advertisement
I already found that article a while ago. It doesn''t help me because in that article the Child Process also Reads and writes to the pipe. I''m calling a program wich "thinks" it''s running to the stdout buffer, and I''m trying to get a handle for that buffer (Wich I have managed with the CreateFile() function in my code) and then read what the other program is writing to console. As I said, the ReadConsoleOuput function reads that output, but then how do I know what I''ve already read and what not?

------------------------------------------------------
Regret Nothing - Learn Everything

Share this post


Link to post
Share on other sites
I already found that article a while ago. It doesn''t help me because in that article the Child Process also Reads and writes to the pipe. I''m calling a program wich "thinks" it''s writing to the stdout buffer, and I''m trying to get a handle for that buffer (Wich I have managed with the CreateFile() function in my code) and then read what the other program is writing to console. As I said, the ReadConsoleOuput function reads that output, but then how do I know what I''ve already read and what not?

Edit: typos

------------------------------------------------------
Regret Nothing - Learn Everything

Share this post


Link to post
Share on other sites
I think you missed the point. The child process is reading to and from what it thinks is standard input and output, which is exactly what you described your child process is doing.

Share this post


Link to post
Share on other sites

  • Advertisement