用C / C ++创建一个“隐形”的windows程序

时间:2012-02-29 01:44:08

标签: c winapi background silent

好的,这是这个问题的延续:How to make a simple Hello World "invisible" in Windows (C/C++)

人们给了我一些指导,在这里我有一个新的问题:

在做了一些研究之后我又被卡住了。互联网上的人声称,通过创建一个win32应用程序,将没有图形指示。

这是执行此操作的代码(我很确定您已经知道这一点但是没有)

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {

//code
return 0;
}

因此不会显示在main中键入的代码。我真的不知道他们的意思是什么样的代码,例如:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {

    while (1) {
    }
}

这个程序弹出一个Cmd窗口就好了。

我还发现通过初始化STARTUPINFO结构中的值

STARTUPINFO StartupInfo;
memset(&StartupInfo, 0, sizeof(StartupInfo));
// set the size of the structure
StartupInfo.cb = sizeof(STARTUPINFO);
// tell the application that we are setting the window display 
// information within this structure
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
// set the window display to HIDE
StartupInfo.wShowWindow = SW_HIDE;

会隐藏控制台窗口。这对我来说不起作用。我有这种感觉,我在这里错过了一个主要的概念,所以我需要你的知识。我想创建一个简单的.exe,类似于while循环或一个不显示东西的简单打印。我错过了什么?

1 个答案:

答案 0 :(得分:5)

您必须使用特定的编译器选项才能执行此操作;它不是代码本身的属性。我假设您正在使用Visual Studio进行编译。

转到Project Properties > Configuration Properties > Linker > System > SubSystem并将其设为Windows。如果您执行此操作并运行程序,例如,将程序置于无限循环中,则必须从任务管理器中删除它。

我不知道如何在GCC上这样做。 Gerald在评论中告诉我,使用--subsystem,windows-mwindows会为GCC做这件事。请注意,-mwindows也链接了GDI库。

相关问题