2009-02-28 145 views
9

**** C newbie alert ****如何编译C应用程序,以便它在Windows上不显示控制台窗口的情况下运行?我使用Windows XP和GCC 3.4.5(mingw-vista特别r3)。我详尽地用Google搜索这一点,我想出来的,而根据我读过的东西,听起来像它应该做的伎俩以下,但不会在我的系统:GCC/C如何隐藏控制台窗口?

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

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

我我也尝试向GCC传递“-mwindows”参数无济于事。代码示例启动记事本,但仍闪烁了命令提示符。编辑:FWIW我也曾尝试ShellExecute作为系统()的缩影,尽管我很乐意甚至用一个空的main()或WinMain()来获取应用程序。

回答

14

保留-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); 
} 

注意:您需要为execl()呼叫,但完整的路径不是execlp()之一。

编辑:简要说明为什么这会起作用 - 使用system()启动一个shell(如cmd.exe)来执行生成控制台窗口的命令。使用execl不会。

+0

完美!谢谢。 – 2009-02-28 10:35:11