windows GCC/C 如何隐藏控制台窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/597818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GCC / C how to hide console window?
提问by Wayne Koorts
****C newbie alert**** How do I compile a C app so that it runs without showing a console window on Windows? I'm using Windows XP and GCC 3.4.5 (mingw-vista special r3). I've googled this exhaustively and I've come up with the following which, according to what I've read, sounds like it's supposed to do the trick, but doesn't on my system:
****C 新手警报**** 如何编译 C 应用程序,使其在 Windows 上不显示控制台窗口的情况下运行?我使用的是 Windows XP 和 GCC 3.4.5(mingw-vista special r3)。我已经用谷歌搜索了详尽的内容,并提出了以下内容,根据我所阅读的内容,听起来应该可以解决问题,但在我的系统上却没有:
#include <windows.h>
#include <stdlib.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
system("start notepad.exe");
}
I've also tried passing the "-mwindows" argument to GCC to no avail. The code sample launches Notepad but still flashes up a command prompt.
我也试过将“-mwindows”参数传递给 GCC 无济于事。代码示例启动记事本,但仍会出现命令提示符。
EDIT: FWIW I have also tried ShellExecute as an alernative to system(), although I would be happy to even get an app with an empty main() or WinMain() working at this point.
编辑:FWIW 我也尝试过将 ShellExecute 作为 system() 的替代方案,尽管我很乐意在此时获得一个带有空 main() 或 WinMain() 的应用程序。
回答by
Retain the -mwindows flag and use this:
保留 -mwindows 标志并使用它:
#include <windows.h>
#include <process.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
execl("c:\winnt\system32\notepad.exe", 0);
// or: execlp("notepad.exe", 0);
}
Note: you need the full path for the execl()
call but not the execlp()
one.
注意:您需要execl()
调用的完整路径,而不是完整路径execlp()
。
Edit:a brief explanation of why this works - using system() starts a shell (like cmd.exe) to exec the command which produces a console window. Using execl doesn't.
编辑:简要解释为什么会这样 - 使用 system() 启动一个 shell(如 cmd.exe)来执行生成控制台窗口的命令。使用 execl 不会。