windows 启动隐藏的 C++ 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8331930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting a hidden C++ program
提问by Felipe
I am creating a C++ program with Visual Studio 2010 that is supposed to run on the background of my machine.
我正在使用 Visual Studio 2010 创建一个 C++ 程序,该程序应该在我的机器的后台运行。
Therefore when I start it, I shouldn't see the CMD screen while it is running. How can I do this? Do I have to use the Win32 API or a normal C++ program will suffice?
因此,当我启动它时,我不应该在它运行时看到 CMD 屏幕。我怎样才能做到这一点?我必须使用 Win32 API 还是普通的 C++ 程序就足够了?
Please note that my program has no GUI at all.
请注意,我的程序根本没有 GUI。
回答by FailedDev
Use WinMain() :
使用 WinMain() :
#include <windows.h>
int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show)
{
// program starts here
return 0;
}
// int main() <-- remove main()
Then ake sure your project settings are set so that you build a "Win32" program and not a "Console" program.
然后确保您的项目设置已设置,以便您构建“Win32”程序而不是“控制台”程序。
Edit:As @Sehe points out, winMain may not be necessary, although I am not quite sure where this option lies.
编辑:正如@Sehe 指出的,winMain 可能不是必需的,尽管我不太确定这个选项在哪里。
回答by John Dibling
回答by dasblinkenlight
回答by kichik
Simply make it a GUI application instead of a command line application. Right click the project -> Properties -> Configuration Properties -> Linker -> System -> SubSystem -> Windows (/SUBSYSTEM:WINDOWS).
只需将其设为 GUI 应用程序而不是命令行应用程序即可。右键单击项目-> 属性-> 配置属性-> 链接器-> 系统-> 子系统-> Windows (/SUBSYSTEM:WINDOWS)。
回答by Ben Straub
You can run as a Windows process (which doesn't attach to a console), but never create a window. the main difference is the signature of WinMain
, and the flags to the compiler.
您可以作为 Windows 进程(不附加到控制台)运行,但永远不要创建窗口。主要区别在于 的签名WinMain
和编译器的标志。
Go to File -> New Project, select the standard Windows Application, then delete everything except WinMain
.
转到文件 -> 新建项目,选择标准的 Windows 应用程序,然后删除除WinMain
.