Create an Application without a Window(创建没有窗口的应用程序)
问题描述
您将如何编写无需打开窗口或控制台即可运行的 C/C++ 应用程序?
How would you program a C/C++ application that could run without opening a window or console?
推荐答案
当您编写 WinMain 程序时,您会自动将/SUBSYSTEM 选项设置为编译器中的窗口.(假设您使用 Visual Studio).对于任何其他编译器,可能存在类似的选项,但标志名称可能不同.
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.
一旦此信息出现在可执行文件中,启动程序的系统加载程序会将您的二进制文件视为 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 程序不需要创建任何窗口,就像您在任务栏中看到的所有那些程序和服务一样,但没有看到任何对应的窗口.如果您创建了一个窗口但选择不显示它,也会发生这种情况.
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 Run TIme 库初始化后调用)将是 WinMain 而不是 main.如果您没有在此类程序中提供 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.
这篇关于创建没有窗口的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:创建没有窗口的应用程序


基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01