GCC/C如何隐藏控制台窗口? [英] GCC / C how to hide console window?

查看:27
本文介绍了GCC/C如何隐藏控制台窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

****C 新手警报**** 如何编译 C 应用程序,使其在 Windows 上运行而不显示控制台窗口?我正在使用 Windows XP 和 GCC 3.4.5(mingw-vista special r3).我已经用谷歌搜索了这个,我想出了以下内容,根据我读过的内容,听起来它应该可以解决问题,但在我的系统上没有:

****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:

#include <windows.h>
#include <stdlib.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    system("start notepad.exe");
}

我也尝试将-mwindows"参数传递给 GCC,但无济于事.代码示例会启动记事本,但仍会显示命令提示符.

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.

FWIW 我也尝试过将 ShellExecute 作为 system() 的替代品,尽管此时我什至很乐意让一个带有空 main() 或 WinMain() 的应用程序正常工作.

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.

推荐答案

保留 -mwindows 标志并使用:

Retain the -mwindows flag and use this:

#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);
}

注意:您需要 execl() 调用的完整路径,而不是 execlp() 调用的完整路径.

Note: you need the full path for the execl() call but not the execlp() one.

简要说明其工作原理 - 使用 system() 启动一个 shell(如 cmd.exe)来执行生成控制台窗口的命令.使用 execl 不会.

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.

这篇关于GCC/C如何隐藏控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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