海湾合作委员会/ C如何隐藏控制台窗口? [英] GCC / C how to hide console window?

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

问题描述

**ç新手警报** 如何编译C应用程序,以便它在运行时显示在Windows控制台窗口?我使用的是Windows XP和GCC 3.4.5(MinGW的,Vista的特殊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无济于事。在code样品启动记事本,但仍闪烁了命令提示符。

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作为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);
}

请注意:你需要为 execl的完整路径()调用,而不是 execlp()之一。

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

编辑:为什么这个工作的简要说明 - ()使用系统启动一个shell(如CMD.EXE)给exec产生一个控制台窗口中的命令。使用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.

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

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