可选地保持附加到控制台的Windows应用程序 [英] Windows application that optionally remains attached to console

查看:142
本文介绍了可选地保持附加到控制台的Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序(用C编写,用MSVC Express版本编译,32位模式),它有两种主要的操作模式:




  • 窗口模式 - 创建窗口并在其中绘制内容(即分形)。

  • 基准模式 - > - benchmark 作为参数,不要创建窗口,而只是向stdout打印一些基准统计信息。



在开发期间,我编译为控制台应用程序,并使用SDL创建窗口和执行其他GUI功能。所以基准模式运行良好(没有创建窗口),图形模式只有一个逗留的控制台窗口。



但是对于我的发布编译,我已经启用Windows子系统控制台。 (如此问题中所述)。这工作伟大,除非我突然发现我不能运行基准了。 :o



我只是想知道,是否有一种方法让应用程序在运行时选择(例如基于命令行),哪种子系统行为使用?



我在Windows(浏览器,记事本,winword)中对EXE文件做了一些实验,当运行时没有任何东西打印到控制台像/?这样的参数(大多数Windows控制台应用程序支持)。所以它看起来不像它,但我认为这是值得询问这里,万一有一个特殊的技巧。



更新。 ,不,你不能。感谢您的答案。



其他学术问题。这是否意味着子系统选择标记在EXE标题中,系统检查这个和设置窗口或连接到它运行的控制台?我不太了解EXE加载,但我很想知道这里的一些细节。



结论四个好的解决方案(加上两个半解决方案,共五个:p)可供选择:


  1. 使用控制台子系统,

  2. 使用Windows系统,并在基准模式下运行时使用AllocConsole。不完美,如果fractal.exe从现有的控制台运行,所以我会把它作为一半的解决方案; - )。

  3. 每个子系统只有一个可执行文件,fractal.exe和fractalgui.exe

  4. 有两个(或多个)可执行文件,其中一个执行工作,并将其传递给另一个,以便在控制台或窗口中显示。需要一些关于如何划分程序以及如何在它们之间进行通信的想法。

  5. 另一个半解决方案:将fractalgui.exe打印到基准标准输出,并管道到实用程序


  6. 我尚未选择,但我倾向于#3。



    感谢Matteo和smerlin的想法!

    解决方案

    在运行时选择她的子系统(有一些真正丑陋的解决方法,但那些充满了怪癖)。



    然后这个问题的一般解决方案是有一个控制台应用程序,



    对于您的基准案例,它只会打印您的基准统计信息。



    示例设置:

    - fractalgui.exe(subsystem:windows)

    - fractal.exe(subsystem:console)



    *用户桌面上的快捷方式链接到 fractalgui.exe

    *(如果用户启动 fractal.exe 从控制台, fractal exe 开始 fractalgui.exe

    *启动 fractal.exe --benchmark ,它会执行基准测试本身(如果可能将此基准逻辑添加到另一个可执行文件),并将信息直接打印到控制台,或 - 如果这不可能 - 它将需要启动 fractalgui.exe --nogui --benchmark 。这里的棘手案例是将你的输出从fractalgui.exe到fractal.exe,所以你可以打印它在适当的控制台。有几种方法可以做到这一点。命名管道(有办法以一种方式启动fractalgui.exe,你可以只使用stdout / cout那里,数据将被管道到fractal.exe的stdout,但我不记得如何excactly这个工作了:也许作品))。最简单的方法是启动 fractalgui.exe --nogui --benchmark> mylogfile 然后在fractalgui.exe完成后打印mylogfile(因为如果输出被重定向到文件,fractalgui.exe的stdout / cout将工作),但是你不会得到live输出,因为所有当fractalgui.exe已经完成时,输出将打印在控制台上。


    I've got a Windows application (written in C, compiled with MSVC Express edition, 32-bit mode), which has two main modes of operation:

    • Windowed mode -- create a window, and draw stuff in it (namely, a fractal).
    • Benchmark mode -- when run with --benchmark as an argument, don't make a window but just print some benchmark statistics to stdout.

    During development I've compiled as a Console app, and used SDL to create the window and perform other GUI functions. So benchmark mode runs fine (no window is created), and graphical mode just has a lingering console window.

    However for my release compilation I've enabled the Windows subsystem instead of Console. (As explained in this question). This works great except I've suddenly discovered I can't run benchmarks any more. :o

    I'm just wondering, is there a way for an application to choose at run time (e.g. based on the command line it's given) which kind of subsystem behaviour to use?

    I've done some experimentation with EXE files in Windows (explorer, notepad, winword) and none of them seem to print anything to the console when run with an argument like "/?" (which most Windows console apps support). So it doesn't look like it, but I thought it's worth asking here in case there's a special trick.

    Update. It looks like, no, you can't. Thanks for the answers guys.

    Additional academic question. Does this mean that the subsystem choice is marked in the EXE header, and it's the operating system that examines this and sets up the Window or connects it to the console it's run from? I don't know much about EXE loading, but I would be curious to learn a few details here.

    Conclusion. I think there are four good solutions (plus two semi-solutions, making five total :p) to choose from:

    1. Use the console subsystem, but use FreeConsole when running in GUI mode.
    2. Use the windows system, and use AllocConsole when running in benchmark mode. Not perfect if fractal.exe is run from an existing console, so I'll count this as half a solution ;-).
    3. Just have one executable for each subsystem, fractal.exe and fractalgui.exe.
    4. Have two (or more) executables, one of which does the work and passes it to the other to be displayed on the console or in a Window as appropriate. Needs some thought on how to divide the programs and how to communicate between them.
    5. Another half-solution: have fractalgui.exe print the benchmark to standard out, and pipe that to a utility that will simply print it.

    I haven't yet chosen, but I'm leaning towards #3.

    Thanks to Matteo and smerlin for the ideas!

    解决方案

    There is no way a application can choose her subsystem at runtime (well there are some really ugly workarounds, but those are full of quirks).

    Then general solution for this problem is to have a console application, which starts your gui application if necessary

    For your benchmark case, it would just print your benchmark statistics.

    example setup:
    - fractalgui.exe (subsystem: windows)
    - fractal.exe (subsystem: console)

    * the shortcut on the user desktop links to your fractalgui.exe
    * if the user starts fractal.exe from the console, fractal exe starts fractalgui.exe
    * if the user starts fractal.exe --benchmark, it either does the benchmark itself (if its possible to add this benchmark logic to another executable) and prints the information directly to console, or - if thats not possible - it will need to start fractalgui.exe --nogui --benchmark. The tricky case here is to get your output from fractalgui.exe to fractal.exe, so you can print it on the appropriate console. There are several ways to do this, e.g. named pipes (there are ways to start fractalgui.exe in a way, that you can just use stdout / cout there, and the data will be piped to the stdout of fractal.exe, but i dont recall how excactly this works anymore (edit: maybe this works)). The easiest way would be to start fractalgui.exe --nogui --benchmark > mylogfile and then print mylogfile after fractalgui.exe finished (since stdout/cout of fractalgui.exe will work if the output is redirected to a file), however you wont get "live" output, since all the output will be printed on the console when fractalgui.exe is already finished.

    这篇关于可选地保持附加到控制台的Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆