C++ 创建没有窗口的应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/224225/
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
Create an Application without a Window
提问by kiewic
How would you program a C/C++ application that could run without opening a window or console?
您将如何编写无需打开窗口或控制台即可运行的 C/C++ 应用程序?
回答by computinglife
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.
当您编写 WinMain 程序时,您会自动将 /SUBSYSTEM 选项设置为编译器中的窗口。(假设您使用 Visual Studio)。对于任何其他编译器,可能存在类似的选项,但标志名称可能不同。
This causes the compiler to create an entry in the executable file format (PE format) that marks the executable as a windows executable.
这会导致编译器以可执行文件格式(PE 格式)创建一个条目,将可执行文件标记为 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.
但是,如果 Windows 程序不需要,则不需要创建任何窗口,就像您在任务栏中看到的所有程序和服务一样,但看不到任何相应的窗口。如果您创建了一个窗口但选择不显示它,也会发生这种情况。
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 */
}
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.
您需要 WinMain 本身的原因是,一旦您将子系统标记为 Windows,链接器就会假定您的入口点函数(在程序加载和 C Run TIme 库初始化后调用)将是 WinMain 而不是 main。如果您没有在此类程序中提供 WinMain,您将在链接过程中收到未解决的符号错误。
回答by Brian R. Bondy
In windows:
在窗口中:
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// <-- Program logic here
return 0;
}
Be sure to use the /SUBSYSTEM linker switch as mentioned by Adam Mitz.
请务必使用 Adam Mitz 提到的 /SUBSYSTEM 链接器开关。
On other platforms:
在其他平台上:
int main(int argc, char**argv)
{
// <-- Program logic here
return 0;
}
回答by Ilya
If you have a need to contiguously run your program without having console or window you might find useful deamon on *NIXor services on Windows, this .NET example if you need plain win32 just google a little bit for sample.
Since your question tagged as win32 i assume that services are more relevant for you.
如果您需要在没有控制台或窗口的情况下连续运行您的程序,您可能会在 *NIX或Windows上的服务上找到有用的守护程序,如果您需要简单的 win32,只需在 google 上搜索一下示例。
由于您的问题被标记为 win32,我认为服务与您更相关。
回答by crisat
In Visual Studio Express 2010 after setting the subsystem to windows (as suggested by user17224), alternatively to changing the main to WinMain (as suggested by user17224 and Brian R. Bondy), one can set the entry function to main in properties, linker, advanced, entry point: just type main in the text box.
在 Visual Studio Express 2010 中,将子系统设置为 windows(如 user17224 建议),或者将 main 更改为 WinMain(如 user17224 和 Brian R. Bondy 建议),可以在属性、链接器、高级,入口点:只需在文本框中键入 main。
回答by WndProc
This also processes messages:
这也处理消息:
#include <windows.h>
#include <stdio.h>
int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
DWORD curThreadId;
curThreadId = GetCurrentThreadId();
// Send messages to self:
PostThreadMessage(curThreadId, WM_USER, 1, 2);
PostThreadMessage(curThreadId, WM_USER+1, 3, 4);
PostThreadMessage(curThreadId, WM_USER+2, 5, 6);
PostThreadMessage(curThreadId, WM_USER+3, 7, 8);
PostThreadMessage(curThreadId, WM_QUIT, 9, 10);
while (GetMessage(&msg, NULL, 0, 0)) {
printf("message: %d; wParam: %d; lParam: %d\n", msg.message, msg.wParam, msg.lParam);
}
return (int) msg.wParam;
}
回答by Vinay
Use Visual Studio wizard to create the Win32 Application. But don't create the window i.e., you remove the window creation function. Alternatively we can create Win Service application.
使用 Visual Studio 向导创建 Win32 应用程序。但是不要创建窗口,即,您删除了窗口创建功能。或者,我们可以创建 Win Service 应用程序。
回答by jussij
If you are using MSVCor Visual Studiojust use the new Project Wizardand select the Console Application.
如果您使用的是MSVC或Visual Studio,只需使用新的Project Wizard并选择Console Application。