创建一个没有窗口的应用程序

时间:2023-01-19 23:30:53

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

如何编写不打开窗口或控制台就可以运行的C/ c++应用程序?

6 个解决方案

#1


35  

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程序时,您将自动获得/子系统选项作为编译器中的windows。(假设你使用的是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运行时库初始化之后调用)将是WinMain而不是main。如果在这样的程序中不提供WinMain,那么在链接过程中会出现未解决的符号错误。

#2


11  

In windows:

在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提到的/子系统链接器开关。

On other platforms:

在其他平台上:

int main(int argc, char**argv)
{
  // <-- Program logic here
  return 0;
}

#3


3  

If you have a need to contiguously run your program without having console or window you might find useful deamon on *NIX or 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上找到有用的deamon或Windows上的服务,如果您需要普通的win32,那么这个. net示例只是谷歌。由于您的问题被标记为win32,我认为服务对您更相关。

#4


2  

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)的建议,或者改变主要. WinMain(如Bondy user17224和布莱恩·r·)的建议,可以设置入口函数一个主要属性,链接器,先进、入口点:类型主要在文本框中。

#5


0  

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服务应用程序。

#6


-7  

If you are using MSVC or Visual Studio just use the new Project Wizard and select the Console Application.

如果您正在使用MSVC或Visual Studio,请使用new Project向导并选择控制台应用程序。

#1


35  

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程序时,您将自动获得/子系统选项作为编译器中的windows。(假设你使用的是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运行时库初始化之后调用)将是WinMain而不是main。如果在这样的程序中不提供WinMain,那么在链接过程中会出现未解决的符号错误。

#2


11  

In windows:

在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提到的/子系统链接器开关。

On other platforms:

在其他平台上:

int main(int argc, char**argv)
{
  // <-- Program logic here
  return 0;
}

#3


3  

If you have a need to contiguously run your program without having console or window you might find useful deamon on *NIX or 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上找到有用的deamon或Windows上的服务,如果您需要普通的win32,那么这个. net示例只是谷歌。由于您的问题被标记为win32,我认为服务对您更相关。

#4


2  

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)的建议,或者改变主要. WinMain(如Bondy user17224和布莱恩·r·)的建议,可以设置入口函数一个主要属性,链接器,先进、入口点:类型主要在文本框中。

#5


0  

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服务应用程序。

#6


-7  

If you are using MSVC or Visual Studio just use the new Project Wizard and select the Console Application.

如果您正在使用MSVC或Visual Studio,请使用new Project向导并选择控制台应用程序。