windows 在没有控制台窗口的情况下启动程序(在后台)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6342935/
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
start a program without a console window (in background)
提问by aajtak
I want to start a simple program when windows start but I don't want to show the console output window associated with that program. Just to test, the program can be as simple as:
我想在 Windows 启动时启动一个简单的程序,但我不想显示与该程序关联的控制台输出窗口。只是为了测试,程序可以很简单:
int main (int argc, char** argv)
{
while (1)
{
printf ("hello world...\n");
Sleep (10000);
}
return 0;
}
compiling it as: cl foo.c
将其编译为: cl foo.c
I know how to start the program when windows is starting (putting in startup folder, creating registry etc), but I don't know how to hide the console output window that comes when the program is started.
我知道如何在 Windows 启动时启动程序(放入启动文件夹、创建注册表等),但我不知道如何隐藏程序启动时出现的控制台输出窗口。
While searching for this, I found that I can use start /B foo.exe
. Is there any other method for doing this? some thing like "&"
we use on unix.
在搜索这个时,我发现我可以使用start /B foo.exe
. 有没有其他方法可以做到这一点?一些像"&"
我们在 unix 上使用的东西。
My actual program is big and is written in C
, so I can not use some other alternatives i found for c#
(like WinMain) and java
(like explained here).
我实际的程序是大,是用C
,所以我不能用一些其他办法,我发现了c#
(像WinMain函数)和java
(如解释在这里)。
回答by Juho
This should work.
这应该有效。
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdParam, int iCmdShow)
{
for (;;) {
//action here
}
return 0;
}
回答by Marcelo Cantos
WinMain is not a C# entry point. C# uses a method of a static class; it is called Main by default, but can be configured to any static-class method.
WinMain 不是 C# 入口点。C#使用静态类的方法;它默认称为 Main,但可以配置为任何静态类方法。
In Windows, non-console C programs should define a WinMain. You can then link them using the linker's /subsystem:windows
command-line option, invokable either directly or from CL.
在 Windows 中,非控制台 C 程序应该定义一个 WinMain。然后您可以使用链接器的/subsystem:windows
命令行选项链接它们,可直接调用或从 CL 调用。
回答by stijn
One method is calling FreeConsole()as first thing in main. It will hide the console window, if any.
一种方法是调用FreeConsole()作为 main 中的第一件事。它将隐藏控制台窗口(如果有)。
回答by Robin Wernick
Unfortunately simply setting FreeConsole() as the first function in main allows the window to appear momentarily anyway. And FreeConsole removes the window so that if you wish to use it later( as in killing the process ) you have to make it appear on screen in a mode out of your control.
不幸的是,简单地将 FreeConsole() 设置为 main 中的第一个函数,无论如何都会让窗口暂时出现。并且 FreeConsole 删除了该窗口,因此如果您希望稍后使用它(如终止进程),您必须使其以无法控制的模式出现在屏幕上。
Windows allows Win32 programs to have only one of four contexts under Visual Studio: Window program with initial GUI window, Console program with initial Console window, DLL or Lib. Changing the subsystem to a non-Console choice from the project->Properties->System view only results in linking issues that block the build.
Windows 允许 Win32 程序在 Visual Studio 下只有四种上下文之一:带有初始 GUI 窗口的窗口程序、带有初始控制台窗口的控制台程序、DLL 或 Lib。从项目-> 属性-> 系统视图将子系统更改为非控制台选项只会导致链接问题阻止构建。
Here is what worked for me with only a little effort. Use Mike's approach above and choose Win32 Project with Window Application. Then delete everything in WinMain after the "Place code here" direction and delete all the called functions. Return true or 1, as you wish from WinMain. No window of any type will appear on launch.
这是只需要一点点努力就对我有用的方法。使用上面 Mike 的方法并选择 Win32 Project with Window Application。然后在“Place code here”方向后删除WinMain中的所有内容并删除所有调用的函数。从 WinMain 返回 true 或 1,如您所愿。启动时不会出现任何类型的窗口。
And when you are ready to deal with a Console Window call AllocConsole() in your code and deal with its positioning and size as you see fit. The Console can be positioned off screen and slid into view if you wish; it only takes a few minutes to get the handle on the configuring functions. Start with Microsoft's 'Console Functions' in MSDN documents. Unfortunately, there is no book on how to use all the functions properly as there is for NCurses in Linux.
当您准备好处理控制台窗口时,请在您的代码中调用 AllocConsole() 并根据您的需要处理其位置和大小。如果您愿意,控制台可以位于屏幕外并滑入视图;只需几分钟即可掌握配置功能。从 Microsoft 在 MSDN 文档中的“控制台功能”开始。不幸的是,没有关于如何像 Linux 中的 NCurses 那样正确使用所有功能的书。
回答by Dennis Bernaerts
If the program would be eg procexp.exe you can do this out of the box :
如果程序是例如 procexp.exe,您可以开箱即用:
cmd /c "start C:\Users\denni\OneDrive\_bin\_tools\ProcessExplorer\procexp.exe"
回答by Mike Kwan
When you're creating your project create one with WinMain instead ( Win32 Project ). If you still want the console later use AllocConsole() and FreeConsole().
当你创建你的项目时,用 WinMain 来创建一个( Win32 Project )。如果您稍后仍需要控制台,请使用 AllocConsole() 和 FreeConsole()。
回答by Michael Kristofik
WinMain
isn't unique to C#. It's possible to write GUI applications in C too. The WinMain function is the entry point, but nothing says you have to actually create a window. You could have WinMain do nothing more than call the first function of your program to get it started. Then you'd have your program running with no GUI window and no console window.
WinMain
不是 C# 独有的。也可以用 C 编写 GUI 应用程序。WinMain 函数是入口点,但没有说明您必须实际创建一个窗口。您可以让 WinMain 只调用程序的第一个函数来启动它。然后,您的程序将在没有 GUI 窗口和控制台窗口的情况下运行。
Of course, this also means no easy way to stop it, short of killing it from Task Manager.
当然,这也意味着没有简单的方法可以阻止它,除了从任务管理器中杀死它。