GCC / C如何隐藏控制台窗口?

时间:2021-02-23 00:21:53

****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 newbie alert ****如何编译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()的alernative,虽然我很乐意甚至得到一个空的main()或WinMain()工作的应用程序。

1 个解决方案

#1


15  

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没有。

#1


15  

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没有。