
MFC and DOS apps - Redirecting console output to an MFC app (Newbie)
This is almost what I want.
I can use a similar code which uses pipes to redirect output to an edit box.
This can be found at Code Guru
The code is like this:
// Handles to the console screen output buffer
HANDLE PipeReadHandle;
HANDLE PipeWriteHandle;
// Handles to the console input buffer
HANDLE PipeInputReadHandle;
HANDLE PipeInputWriteHandle;
// Staff already mentioned...........
PROCESS_INFORMATION ProcessInfo;
SECURITY_ATTRIBUTES SecurityAttributes;
STARTUPINFO StartupInfo;
BOOL Success;
ZeroMemory ( &InputRecord,sizeof(InputRecord));
ZeroMemory ( &KeyEventRecord,sizeof(KeyEventRecord));
//--------------------------------------------------------------------------
// Zero the structures.
//--------------------------------------------------------------------------
ZeroMemory( &StartupInfo, sizeof( StartupInfo ));
ZeroMemory( &ProcessInfo, sizeof( ProcessInfo ));
ZeroMemory( &SecurityAttributes, sizeof( SecurityAttributes ));
//--------------------------------------------------------------------------
// Create the two pipes.
//--------------------------------------------------------------------------
// First set the security attributes
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle = TRUE;
SecurityAttributes.lpSecurityDescriptor = NULL;
// Now Create pipe to read from the console screen output buffer
CreatePipe
(
&PipeReadHandle, // address of variable for read handle
&PipeWriteHandle, // address of variable for write handle
&SecurityAttributes, // pointer to security attributes
0 // number of bytes reserved for pipe (use default size)
);
// Also Create pipe to write to the consoles input buffer ( I added this)
CreatePipe
(
&PipeInputReadHandle,
&PipeInputWriteHandle,
&SecurityAttributes,0
);
// bla bla....
//--------------------------------------------------------------------------
// Set up members of STARTUPINFO structure.
//--------------------------------------------------------------------------
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESTDHANDLES/*STARTF_USESHOWWINDOW |*/;
StartupInfo.wShowWindow = SW_HIDE; // ***Like you said****
StartupInfo.hStdOutput = PipeWriteHandle;
StartupInfo.hStdError = PipeWriteHandle;
StartupInfo.hStdInput = PipeInputReadHandle; //**** Now I have access
to the input buffer
//--------------------------------------------------------------------------
--
// Create the console process.
//--------------------------------------------------------------------------
--
CreateProcess
NULL, // pointer to name of executable module
LPTSTR(m_szCommand), // command line
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes (use primary thread
security attributes)
TRUE, // inherit handles
0, // creation flags
NULL, // pointer to new environment block (use parent's)
m_szCurrentDirectory, // pointer to current directory name
&StartupInfo, // pointer to STARTUPINFO
&ProcessInfo // pointer to PROCESS_INFORMATION
);
.....
My code is something like this:
// used inside writefile()
LPDWORD EventsWritten=new unsigned long;
char Buffer[]="K\0"; // I want to send letter "K" to the console
buffer
// Actually send the letter
WriteFile( PipeInputWriteHandle , Buffer , 2 , EventsWritten , NULL)
This works well if I the DOS app parses the input buffer with a command like
getc() ( Used in Borland C++ 1.0 - It waits for enter after input )
However, this is not exactly what I want.
I want my 32 bit app to send keystrokes to the console's KEYBOARD input
buffer.
(There must be no need to press enter)
There MUST be a keybord input buffer which is different from the console's
input buffer because of the following fact:
When I send a character stream (like a buffer containing "MyText\0") to the
console's input buffer , it is being read by getc() .
However when my dos app processes plain keystrokes ( via a different
function called getbioskey() )
it must be some other buffer that is being read because my data don't get
past the dos app.
So my main question is the following :
*** how do I write to a console's keyboard buffer ? (if something like that
exists) ****
This is so frustrating and difficult to implement !
Any help would be highly appreciated !
Thaks again
George
Quote:
>I think what you want to do here is use the system() call to run your
program,
>and use redirection to a file for the output of your program, and then
parse it.
>Doing so *WILL* create a console window in your app. This can be avoided
using a
>create process call and setting up things correctly. It seems like the best
way
>to show the data would be use a message box after parsing. This syntax will
be
>something like...
>system ("program > out.txt");
>If this answers your question, and you need to avoid the console window,
use
>something like this...
> STARTUPINFO si;
> PROCESS_INFORMATION pi;
> ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
> ZeroMemory(&si,sizeof(STARTUPINFO));
> si.cb = sizeof(STARTUPINFO);
> si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
> si.wShowWindow= SW_HIDE;//NO WINDOW!
> si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
> si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
> si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
> SetLastError(0);
> int testing= ::CreateProcess(NULL, "myprog > out.txt", NULL, NULL,
>TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
> WaitForSingleObject(pi.hProcess, 5000);
> if ((GetLastError()) || (!testing))
> {//this message box could be used in your program
> LPVOID lpMsgBuf;
> FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
>FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL,
>SUBLANG_DEFAULT), // Default language
> (LPTSTR) &lpMsgBuf, 0, NULL );
> MessageBox( NULL, (LPCTSTR) lpMsgBuf, "GetLastError",
>MB_OK|MB_ICONINFORMATION );
>}
>> Greetings
>> An important part of my final year project is using a DOS program ,
inside
>> Windows 9x.
>> The program is written in Borland C++ 1.0 and it's impossible to be
ported
>> to win32 because it performs hardware access using far and near pointers
(as
>> you may know this is prohibited inside Visual C++)
>> So, I am stuck with a console application inside Win9x.The major problem
is
>> the interface which has to be consistent with other MFC apps I have
written.
>> So, I was wondering if it is possible to implement a basic MFC interface
to
>> this ms-dos prompt program (like a window or a dialog).
>> Redirecting console output to an MFC application window would also be
great
>> ,but is it possible?.
>> Any suggestions would be highly appreciated.
>> Thanks in advance
>> George Sgouros