我在 Visual Studio 2005 中有一个混合模式 C++-CLI 程序,它设置为使用 /SUBSYSTEM:Windows。一般来说,它是一个图形应用程序,从它的快捷方式或通过注册到它的文件类型启动。
但是,在极少数情况下,用户希望使用参数从命令行运行它。我可以很好地访问参数,它在写入控制台时,以响应从命令行启动带有参数的程序,我看不到Console::WriteLine
有任何效果。
我究竟做错了什么?
我在 Visual Studio 2005 中有一个混合模式 C++-CLI 程序,它设置为使用 /SUBSYSTEM:Windows。一般来说,它是一个图形应用程序,从它的快捷方式或通过注册到它的文件类型启动。
但是,在极少数情况下,用户希望使用参数从命令行运行它。我可以很好地访问参数,它在写入控制台时,以响应从命令行启动带有参数的程序,我看不到Console::WriteLine
有任何效果。
我究竟做错了什么?
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 How to output to the console in C++/Windows may be useful.
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