VCL 形成写入 stdout 的应用程序
我的公司有一个带有文档对象模型的大型 Windows 应用程序:打开应用程序、打开文件、进行一些更改、保存、关闭。我试图从顶部切掉 GUI 并创建一个控制台应用程序,它接受一些参数、打开文件、执行一些有用的操作、保存、关闭文件并终止。由于存在大量遗留代码,我被迫使用 VCL 表单应用程序并从命令行(或批处理脚本)启动它。我确实需要能够打印到标准输出,以便我可以写出状态消息,响应“--version”和“-?”等选项。我花了整个上午的时间在谷歌上搜索这个主题,但没有找到任何有用的东西。
该应用程序是使用 VCL 在 CodeGear C++ Builder 2007 中编写的。
My company has a large Windows application with a document-object model: open application, open a file, make some changes, save, close. I'm attempting to cut the GUI off of the top and create a console application that accepts some parameters, opens a file, does something useful, saves, closes the file, and terminates. Because there is a large amount of legacy code, I'm forced to use VCL forms application and launch it from the command line (or batch script). I really need to be able to print to stdout so I can write out status messages, respond to options like "--version" and "-?". I've spent all morning doing Google searches on this topic and I haven't found anything that's helpful.
The application is written in CodeGear C++ Builder 2007 using the VCL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想显示控制台窗口,您可以调用 AllocConsole 和 FreeConsole 然后您可以像通常使用控制台应用程序一样调用 WriteLn('xxx') 。但是,如果您从命令行运行此应用程序,它仍然会创建一个新控制台,并且标准输出将转到新控制台而不是调用控制台。
AllocConsole 和 FreeConsole 的原型是在 Windows 单元中。
If you just want to show a console window you can call AllocConsole and FreeConsole Then you can just call WriteLn('xxx') like you normally would with a Console application. However, if you run this application from the command line it will still create a new console and the standard output will go to the new console rather than the calling console.
AllocConsole and FreeConsole are prototyped in the Windows unit.
您可以在 GUI 程序中写入 STDOUT,因为没有控制台,所以通常不会有任何输出,除非它是从实际控制台启动的。或者,查看
GetStdHandle()
和WriteConsole()
函数Win32 API。如果GetStdHandle()
返回有效的句柄,然后你就可以写入它。如果您的 GUI 应用程序是由另一个想要出于自身目的拦截您的 STDOUT 输出的应用程序启动的,那么这尤其有用。You can write to STDOUT in a GUI program, there just usually won't be any output since there is no Console, unless it is launched from an actual Console. Alternatively, look at the
GetStdHandle()
andWriteConsole()
functions in the Win32 API. IfGetStdHandle()
returns a valid handle, then you can write to it. This is particlarly useful if your GUI app is launched by another app that wants to intercept your STDOUT output for its own purposes.在项目文件中使用 {$APPTYPE CONSOLE}。这将分配一个控制台(即使您的应用程序仍然基于表单)。
或者,您可以根据项目 (.DPR) 文件中的命令行参数进行分支(Delphi 代码如下 - 由您决定转换为 C++ Builder 等效项)。您仍然需要 APPTYPE 定义,或者需要使用 Win32 API 控制台函数来创建自己的控制台(请参阅 MSDN 控制台功能 了解更多信息):
Use {$APPTYPE CONSOLE} in your project file. This will allocate a console (even though your app is still based on forms).
Alternatively, you can do branching based on command-line parameters in the project (.DPR) file (Delphi code follows - up to you to convert to C++ Builder equivalent). You'll still need the APPTYPE definition, or you'll need to use Win32 API console functions to create your own console (see MSDN Console Functions for more info):