简体   繁体   English

使用sc create开始时Windows程序无法运行

[英]Windows program doesn't run when started using sc create

I have created a program which is supposed to run as Windows service, starting automatically on boot. 我创建了一个程序,该程序应该作为Windows服务运行,在启动时会自动启动。

Also i have created a starter, which should create a service and then stop. 我也创建了一个启动器,应该创建一个服务,然后停止。

Starter is ran as administrator. Starter以管理员身份运行。

I call this system command from a starter: 我从启动器调用此系统命令:

system("sc create MyApp binpath= /*full path*/ type= own start= auto DisplayName= MyService");

system("sc start MyApp");

Then Windows says that service creation successful. 然后Windows表示服务创建成功。

However, the program which should run as service just doesn't start! 但是,应该作为服务运行的程序无法启动!

After the "sc start" command the console shows nothing for a while, Task Manager shows "Starting", and later console says 在执行“ sc start”命令后,控制台暂时不显示任何内容,任务管理器显示“正在启动”,稍后控制台显示

"Error 1053: The service did not respond to the start or control request in a timely fashion".

I tried running a command like this, showed in another post here on StackOverflow: 我尝试运行这样的命令,在StackOverflow上的另一篇文章中显示:

sc create SERVICENAME binPath= "**cmd /c** c:\programlocation\program.exe"

However, then it says: 但是,它说:

StartService Failed 2: The system cannot find the file specified.

So the **cmd /c** thingy doesnt work. 所以**cmd /c**正常工作。

How to make it work, and please only Windows native tools, no external programs. 如何使其工作,请仅使用Windows本机工具,而不需要外部程序。

You could do something like this: 可以执行以下操作:

Native C++ Windows Service 本机C ++ Windows服务

But I'd switch to C++/CLI and port the following code examples from C#: 但是我将切换到C ++ / CLI并从C#移植以下代码示例:

System.ServiceProcess.ServiceProcessInstaller System.ServiceProcess.ServiceProcessInstaller

Or, even better yet, just do the Windows Service part of your code in C# ... it's much easier than rolling your own in C++. 或者,甚至更好的是,只需在C#中执行代码的Windows服务部分...比在C ++中滚动自己的代码要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM