Getting the messages back to the same command line from where the MFC application was launched

217 Views Asked by At

I am executing an MFC Application from command line which takes four command line arguments.One of the argument is the directory path. If the path is wrong then I want to show a Message "Bad Path" on the same command line

Note : For showing I don't want to take a new command line .

1

There are 1 best solutions below

1
On BEST ANSWER

Basically that's not supported. There is sort of known "workaround" for that, using AttachConsole(-1) to attach parent process console. Has disadvantages of course (like, parent console will not wait for your EXE to terminate, because it's not a "console" app). Anyways, basic idea:

void WriteToParentConsole()
{
    if (AttachConsole(-1))
    {
        char msg[] = "Bad Path!";
        WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, sizeof(msg), NULL, NULL);

        // send ENTER (optional)
        // ::SendMessage(GetConsoleWindow(), WM_CHAR, VK_RETURN, 0);

        FreeConsole();
    }
}

You can check out this article for example, or just google something on AttachConsole/GUI vs Console for more information: http://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/