3

I has a mfc application which I am launching through command prompt. Where I will enter some specified syntax. If I had given wrong syntax, as of now I am showing a message box. But instead of showing message can I write the same message to the same console where I am trying to launch my application?

Can anyone kindly let me know how we can write to the console from an MFC application.

Siva
  • 1,281
  • 2
  • 19
  • 41
  • 2
    possible duplicate of [how do I write to stdout from an MFC program?](http://stackoverflow.com/questions/5094502/how-do-i-write-to-stdout-from-an-mfc-program) – IInspectable Jul 25 '15 at 19:54

3 Answers3

1

I think Console::WriteLine() and AttachConsole() can do the trick

example:

#include "windows.h"
#pragma comment(lib, "kernel32.lib")

[STAThread]
int main()
{
    AttachConsole(-1);  //Use the console of the parent of the current process.

    Console::WriteLine("wrongsyntax");  //This will write to the command prompt

    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 
    Application::Run(gcnew Project::MyForm);
}
J3soon
  • 3,013
  • 2
  • 29
  • 49
  • You may want to check the return value from `AttachConsole` and use a MessageBox instead if it fails, just in case the process wasn't started from a console. – Mark Ransom Jul 24 '15 at 17:13
  • How exactly is your proposed solution *"an MFC application"*, as requested in the question? – IInspectable Jul 25 '15 at 19:48
0

This is useful:

How do I get the application exit code from a Windows command line?

Start /wait program.exe
Echo %errorlevel%

So, bring up prompt, run app, app sets an error number in InitInstance and closes, then you display error number.

Community
  • 1
  • 1
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
-2

You can use cout to display data on the console, e.g .:

std::cout<< "Hello World"
spenibus
  • 4,339
  • 11
  • 26
  • 35
Rohit Hajare
  • 125
  • 1
  • 7