views:

42

answers:

2

I have a mixed mode C++-CLI program in Visual Studio 2005 that is set to use the /SUBSYSTEM:Windows. Generally speaking it is a graphical application that is launched from its shortcut or through the filetype registered to it.

However, there is a rare occasion where a user will want to run it from the command line with arguments. I can access the arguments just fine, its when it comes to writing to the console, in response to the program being launched from the command line with arguments, where I don't see Console::WriteLine having any effect.

What am I doing wrong?

+1  A: 

This one's annoying, I agree. You're not doing anything wrong, it's a quirk of the way Windows is set up.

It is possible to solve this, at least in some cases, see http://blogs.msdn.com/junfeng/archive/2004/02/06/68531.aspx . I've not come across anybody else who's actually used these methods though.

Most people IME just create two versions of the executable with different names, one for batch users ("myapp.exe") and one for when it's run from the start menu ("myappw.exe").

For more information, some of the suggestions at http://stackoverflow.com/questions/587767/how-to-output-to-console-in-c-windows may be useful.

Andy Mortimer
A: 

It's an old problem - see http://www.codeproject.com/KB/cpp/EditBin.aspx for solutions

You can also reopen the streams to a console

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE /*hPrevInst*/, LPSTR cmd_line, int showmode)
{
  AllocConsole(); //create a console
  ifstream conin("con");   // not sure if this should be "con:" ?
  ofstream conout("con");
  cout.rdbuf(conout.rdbuf()); 
  cerr.rdbuf(conout.rdbuf());      
  cin.rdbuf(conin.rdbuf());


  FreeConsole();
  return 0;
}

edit: sorry this is pure C++, don't know about C++/cli

Martin Beckett