Is it possible to create a process, an MFC application, and from that MFC application send message back to the programme that create the process ?
The goal is to be able to run the MFC application with a UI or in a console. So I create a console app (a.com) that launch the MFC application (a.exe) with a swith to run in console mode, but how can the MFC app send message back to a.com so that message get
display in the console ?
Thanks.
-
-
By message you mean Windows Message? WM_USER+X ?
Or you mean to pass a string which will be display in the console? -
Why would the console app be called a.com? Com executables are strictly a DOS concept, they don't exist in Windows.
For exchanging data between two processes, look into Interprocess Communication mechanisms such as pipes or shared memory. -
ido.ran said:By message you mean Windows Message? WM_USER+X ?Or you mean to pass a string which will be display in the console?
I want to pass string to be display in the console.
The app.com and app.exe is a trick that I read from the Internet to have the same app in console and UI, but both app contain all the business logic. It is not what I want. I can create a console.exe and app.exe. It doesn't matter. -
A far less complicated way is just to use the Console class in .NET or standard out in a native executable.cro said:ido.ran said:*snip*I want to pass string to be display in the console.
The app.com and app.exe is a trick that I read from the Internet to have the same app in console and UI, but both app contain all the business logic. It is not what I want. I can create a console.exe and app.exe. It doesn't matter. -
AndyC said:
A far less complicated way is just to use the Console class in .NET or standard out in a native executable.cro said:*snip*cout has no effect in a MFC application, there is no console to print the text to.
-
Ah, yes, MFC. There is some sample code for attaching to the console here. That should do the trick.cro said:AndyC said:*snip*cout has no effect in a MFC application, there is no console to print the text to.
-
Thanks !AndyC said:
Ah, yes, MFC. There is some sample code for attaching to the console here. That should do the trick.cro said:*snip* -
There's something that didn't work. If I wan't to save it in a file, the text gets display in the console and the file is not created at all.
c:\>UI.exe >test.txt
This is the test code that I add to the InitInstance method:HANDLE hStdout;
LPSTR lpszPrompt1 = "Hello world !";
DWORD cWritten;if (!AttachConsole(ATTACH_PARENT_PROCESS))
{
AfxMessageBox(_T("Erreur: ATTACH_PARENT_PROCESS"), MB_OK);
exit(1);
}
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdout == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Erreur: INVALID_HANDLE_VALUE"), MB_OK);
exit(1);
}
if (!WriteFile(
hStdout, // output handle
lpszPrompt1, // prompt string
lstrlenA(lpszPrompt1), // string length
&cWritten, // bytes written
NULL))
{
AfxMessageBox(_T("Erreur: WriteFile"), MB_OK);
exit(1);
}return FALSE;
-
> c:\>UI.exe >test.txtcro said:There's something that didn't work. If I wan't to save it in a file, the text gets display in the console and the file is not created at all.
c:\>UI.exe >test.txt
This is the test code that I add to the InitInstance method:HANDLE hStdout;
LPSTR lpszPrompt1 = "Hello world !";
DWORD cWritten;if (!AttachConsole(ATTACH_PARENT_PROCESS))
{
AfxMessageBox(_T("Erreur: ATTACH_PARENT_PROCESS"), MB_OK);
exit(1);
}
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdout == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Erreur: INVALID_HANDLE_VALUE"), MB_OK);
exit(1);
}
if (!WriteFile(
hStdout, // output handle
lpszPrompt1, // prompt string
lstrlenA(lpszPrompt1), // string length
&cWritten, // bytes written
NULL))
{
AfxMessageBox(_T("Erreur: WriteFile"), MB_OK);
exit(1);
}return FALSE;
Hmmm... what happens if you do
c:\>UI.exe 2>&1 > test.txt
-
The text display in the console and the file is empty.Matthew van Eerde said:
> c:\>UI.exe >test.txtcro said:*snip*
Hmmm... what happens if you do
c:\>UI.exe 2>&1 > test.txt -
Finaly I found a solution starting from a Process class that I found on this page.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.