一个可执行既可以是一个控制台和GUI应用程序? [英] Can one executable be both a console and GUI application?

查看:545
本文介绍了一个可执行既可以是一个控制台和GUI应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使可运行为依据CLI或GUI应用程序 C#计划什么标志传递给它。可以这样做?

I want to make a C# program that can be run as a CLI or GUI application depending on what flags are passed into it. Can this be done?

我发现这些相关的问题,但他们不完全覆盖我的情况:

I have found these related questions, but they don't exactly cover my situation:

  • How to write to the console in a GUI application
  • How do I get console output in C++ with a Windows program?

推荐答案

<一个href=\"http://stackoverflow.com/questions/493536/can-one-executable-be-both-a-console-and-gui-app/493568#493568\">Jdigital's回答点 Raymond Chen的博客,这也解释了为什么你可以'吨有一个应用程序,既是一个控制台程序和非控制台 * 程序:操作系统需要知道程序开始运行之前的 的哪个子系统使用。一旦程序开始运行时,为时已晚回去,并要求其他模式。

Jdigital's answer points to Raymond Chen's blog, which explains why you can't have an application that's both a console program and a non-console* program: The OS needs to know before the program starts running which subsystem to use. Once the program has started running, it's too late to go back and request the other mode.

<一个href=\"http://stackoverflow.com/questions/493536/can-one-executable-be-both-a-console-and-gui-app/493556#493556\">Cade's回答点有关运行与控制台一个.net WinForms应用程序的文章。它采用调用 AttachConsole 的技术程序后开始运行。这有利于程序写回启动该程序的命令提示符控制台窗口的作用。但是,该文章中的评论指出,我认为是一个致命的缺陷:的子进程并没有真正控制控制台的控制台继续接受代父进程的输入,父过程是不知道它应该等待孩子完成使用控制台其他东西之前运行。

Cade's answer points to an article about running a .Net WinForms application with a console. It uses the technique of calling AttachConsole after the program starts running. This has the effect of allowing the program to write back to the console window of the command prompt that started the program. But the comments in that article point out what I consider to be a fatal flaw: The child process doesn't really control the console. The console continues accepting input on behalf of the parent process, and the parent process is not aware that it should wait for the child to finish running before using the console for other things.

陈的文章指向由李俊峰张的文章,解释了一对夫妇的其他技术

首先是 devenv的的使用。它的工作原理通过实际有两个方案。一个是<青霉> devenv.exe的的,这是主要的GUI程序,而另一个是<青霉> devenv.com 的,它处理控制台模式的任务,但如果它处于非使用控制台般的方式,它的任务转发给的 devenv.exe的的并退出。该技术依赖于Win32的规则 COM 的文件会选择提前的 exe文件的时候,你没有文件扩展名键入命令文​​件。

The first is what devenv uses. It works by actually having two programs. One is devenv.exe, which is the main GUI program, and the other is devenv.com, which handles console-mode tasks, but if it's used in a non-console-like manner, it forwards its tasks to devenv.exe and exits. The technique relies on the Win32 rule that com files get chosen ahead of exe files when you type a command without the file extension.

有这么Windows脚本宿主做这个简单的变化。它提供了两个完全独立的二进制文件,的wscript.exe 的Cscript.exe 的。同样,Java提供的的java.exe 的控制台程序和的javaw.exe 的非控制台程序。

There's a simpler variation on this that the Windows Script Host does. It provides two completely separate binaries, wscript.exe and cscript.exe. Likewise, Java provides java.exe for console programs and javaw.exe for non-console programs.

俊峰的第二个技巧是什么的反汇编的使用。他引用了反汇编的的作者经历使得它在两种模式下运行时的过程。最终,这里就是在它的作用:

Junfeng's second technique is what ildasm uses. He quotes the process that ildasm's author went through when making it run in both modes. Ultimately, here's what the it does:


  1. 程序被标记为一个控制台模式二进制,因此它总是开始时与控制台。这使得输入和输出重定向正常工作。

  2. 如果程序没有控制台模式的命令行参数,它重新启动本身。

这是不够的,简单地调用 FreeConsole 来做出一审不再是一个控制台程序。这是因为启动该程序的过程中,的cmd.exe 的,知道它开始一个控制台模式程序,正在等待程序停止运行。调用 FreeConsole 将使的反汇编的停止使用控制台,但它不会让父进程的启动的使用控制台

It's not enough to simply call FreeConsole to make the first instance cease to be a console program. That's because the process that started the program, cmd.exe, "knows" that it started a console-mode program and is waiting for the program to stop running. Calling FreeConsole would make ildasm stop using the console, but it wouldn't make the parent process start using the console.

因此​​,一审重启本身(一个额外的命令行参数,我想)。当你调用的CreateProcess ,有两种不同的标志尝试,的 DETACHED_PROCESS CREATE_NEW_CONSOLE ,否则将确保二审将不被附加到父控制台。在这之后,第一个实例可以终止并允许命令提示恢复处理命令

So the first instance restarts itself (with an extra command-line parameter, I suppose). When you call CreateProcess, there are two different flags to try, DETACHED_PROCESS and CREATE_NEW_CONSOLE, either of which will ensure that the second instance will not be attached to the parent console. After that, the first instance can terminate and allow the command prompt to resume processing commands.

这种技术的副作用是,当您从GUI界面启动程序,仍然会有一个控制台。它会在屏幕上闪烁片刻,然后消失。

The side effect of this technique is that when you start the program from a GUI interface, there will still be a console. It will flash on the screen momentarily and then disappear.

有关使用俊峰的文章部分的 EDITBIN 的修改程序的控制台模式的标志是一个红色的鲱鱼,我想。你的编译器或开发环境应该提供一个设置或选项来控制它产生何种二进制。应该没有必要修改之后什么。

The part in Junfeng's article about using editbin to change the program's console-mode flag is a red herring, I think. Your compiler or development environment should provide a setting or option to control which kind of binary it creates. There should be no need to modify anything afterward.

的底线,那么,是您可以有两个二进制文件,或者你可以有一个控制台窗口的瞬间闪烁。一旦你决定这是两害取其轻,你有你的实现选择。

The bottom line, then, is that you can either have two binaries, or you can have a momentary flicker of a console window. Once you decide which is the lesser evil, you have your choice of implementations.

* 我说的非控制台的代替的 GUI 的,因为否则它是一个错误的二分法。仅仅因为一个节目没有一个控制台并不意味着它有一个GUI。服务应用程序是一个最好的例子。此外,程序可以有一个控制台的的窗口。

* I say non-console instead of GUI because otherwise it's a false dichotomy. Just because a program doesn't have a console doesn't mean it has a GUI. A service application is a prime example. Also, a program can have a console and windows.

这篇关于一个可执行既可以是一个控制台和GUI应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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