创建无窗口应用程序 [英] Create an Application without a Window

查看:106
本文介绍了创建无窗口应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会如何编程,可以在不打开一个窗口或控制台运行C / C ++应用程序?

How would you program a C/C++ application that could run without opening a window or console?

推荐答案

当你写的WinMain程序,您将自动获得/ SUBSYSTEM选项是在编译器窗口。 (假设你使用Visual Studio)。对于任何其他编译器的类似选项可能被present但标志名称可能不同。

When you write a WinMain program, you automatically get the /SUBSYSTEM option to be windows in the compiler. (Assuming you use Visual Studio). For any other compiler a similar option might be present but the flag name might be different.

这导致编译器创建可执行文件格式( PE格式)的条目标记可执行文件为Windows可执行文件。

This causes the compiler to create an entry in the executable file format (PE format) that marks the executable as a windows executable.

一旦这个信息是present在可执行文件,启动程序将把您的二进制文件作为一个Windows可执行文件,而不是一个控制台程序,因此它不会导致控制台窗口在运行时自动打开系统加载程序。

Once this information is present in the executable, the system loader that starts the program will treat your binary as a windows executable and not a console program and therefore it does not cause console windows to automatically open when it runs.

但是,一个Windows程序不需要创建任何窗口,如果它不需要想,就像所有的程序和你看到在任务栏上运行,但看不到任何相应的Windows为他们服务。如果你创建一个窗口,但选择不显示它也会发生这种情况。

But a windows program need not create any windows if it need not want to, much like all those programs and services that you see running in the taskbar, but do not see any corresponding windows for them. This can also happen if you create a window but opt not to show it.

所有你需要做的,要实现这一切,

All you need to do, to achieve all this is,

#include <Windows.h>

int WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance, 
            LPTSTR    lpCmdLine, 
            int       cmdShow)
    {
    /* do your stuff here. If you return from this function the program ends */
    }

您需要的WinMain本身的原因是,一旦你标记子系统的Windows,链接器假定您的入口点函数(这是程序加载和C运行时库初始化之后调用)将WinMain函数,而不是主。如果你没有在这样一个程序提供的WinMain你会在链接过程中得到一个未解决的符号错误。

The reason you require a WinMain itself is that once you mark the subsystem as Windows, the linker assumes that your entry point function (which is called after the program loads and the C Run TIme library initializes) will be WinMain and not main. If you do not provide a WinMain in such a program you will get an un-resolved symbol error during the linking process.

这篇关于创建无窗口应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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