简体   繁体   English

从Windows gui程序输出到命令行

[英]output to command line from windows gui program

I have a win32 gui based exe which is GDI based - 我有一个基于GDI的Win32 GUI exe-

The program accepts command line arguments when it starts, and I need a way to print out a message if the command line is incorrect. 该程序在启动时会接受命令行参数,如果命令行不正确,我需要一种打印消息的方法。

At the moment, I am displaying an error message using MessageBox, and would like to know how to print out this error message if the user started the program from the dos command line. 目前,我正在使用MessageBox显示一条错误消息,并且想知道如果用户从dos命令行启动该程序,则如何打印此错误消息。 - -

:) :)

ok this does what I want, but it requires me to press enter otherwise I dont see the current path displayed.. 好的,这符合我的要求,但是需要我按enter键,否则我看不到显示的当前路径。

         void RedirectIOToConsole()
         {
         int hConHandle;
         FILE *fp;
         AttachConsole(-1);
         hConHandle=_open_osfhandle(GetStdHandle(STD_OUTPUT_HANDLE),_O_TEXT);
         fp=_fdopen(hConHandle,"w");
         fprintf(fp,"\ntesting\n");
         fflush(fp);
         _close(hConHandle);
         FreeConsole();
         }

Most applications that need to accomplish this will create a *.com version of their app (which will sit alongside the .exe). 大多数需要完成此操作的应用程序将创建其应用程序的* .com版本(位于.exe旁边)。 All shortcuts will point to the .exe, but if the user happens to type the name on a command line, then the .com version will end up executing (as CMD will search for .com files before .exe files) The .com version is often a thin wrapper which will invoke the .exe and wait for it to finish or utilize shared .dll files, but it exists purely so that output can be funneled to the proper console window and won't "release" it until the application finishes. 所有快捷方式都将指向.exe,但是如果用户碰巧在命令行上键入名称,则.com版本将最终执行(因为CMD会在.exe文件之前搜索.com文件)。通常是一个薄包装程序,它将调用.exe并等待其完成或使用共享的.dll文件,但是它纯粹是存在的,因此可以将输出漏斗到适当的控制台窗口,直到应用程序“释放”为止饰面。

As an example, Microsoft's Visual Studio uses this approach: 例如,Microsoft的Visual Studio使用以下方法:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE>dir devenv.*
 Volume in drive C has no label.
 Volume Serial Number is A482-59BA

 Directory of C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE

07/26/2012  05:05 PM             7,680 devenv.com
07/26/2012  07:08 PM           406,976 devenv.exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM