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

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

问题描述

**** C newbie alert ****我如何编译一个C应用程序,使其运行时不显示在Windows上的控制台窗口?我使用Windows XP和GCC 3.4.5(mingw-vista special r3)。我已经详细地google了,我已经提出了以下,根据我已经阅读,听起来像是应该做的伎俩,但不是在我的系统:

****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()的一个alernative,虽然我会很高兴甚至得到一个空的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);
}

注意:您需要完整路径 ()调用而不是 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天全站免登陆