如何从窗口控制台程序创建Windows服务? [英] How do I make a windows service from window console program?

查看:195
本文介绍了如何从窗口控制台程序创建Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为a.exe的窗口控制台程序,由VC ++运行,每天24小时运行。

我试图将此程序更改为Windows服务程序。

但是当我编写一个关于它的代码时,看到我的手册,编译器无法知道变量类型的SERVICE_TABLE_ENTRY,或SERVICE_STATUS等。



1.什么是我的主要错误点,即使它是在手册中写的?

2.作为满足此目的的另一种方法,我将使用开始按钮进行对话,所以如果点击它, ShellExecute()函数打开包含sc / create a.bat的批处理文件。但是这导致了一条关于拒绝访问的错误消息....



所以这两个错误对我来说都是非常复杂的问题,对于一个新手来说制作Windows服务并不容易。



请让我展示一下这个简单的样品。



提前谢谢。



我尝试了什么:



这个问题浪费了3天多。

I have a window console program named a.exe by VC++ which is running 24 hours a day.
I tried to change this program to windows service program.
But when I have written a code about it as seen my manual, the compiler could not know the variable type of SERVICE_TABLE_ENTRY, or SERVICE_STATUS, etc.

1. What is my main error point even though it is written as in the manual?
2. As another ways to satisfy this purpose, I will make a dialogue with a "Start" button, so if clicking it, the "ShellExecute()" function open the batch file which is including "sc /create a.bat". But this caused an error message about "access denied"....

So both errors are very complicated problems to me, making windows service for a novice is not easy.

Please let me show the simple samples about this.

Thank you in advance.

What I have tried:

More 3 days wasted for this problem.

推荐答案

有关示例,请参阅 C ++中的基本Windows服务(CppWindowsService)C ++ for Visual Studio 2008中的示例 [ ^ ]和 C ++中的简单Windows服务 [ ^ ]。



SERVICE_TABLE_ENTRY SERVICE_STATUS Windows.h 中包含的 Winsvc.h 中定义。所以在编译时应该知道它们。



sc.exe 是服务控制管理器命令行应用程序。必须以足够的权限执行它才能创建,启动和停止服务。创建只能执行一次,通常由安装过程执行(或使用以管理员身份启动的命令shell手动执行测试/非公共服务)。当使用调用 ShellExecute 的普通应用程序来执行 sc 时,该应用程序也必须以足够的权限执行。



另请注意,在创建先前版本时,您必须在开发期间删除该服务。不这样做也会导致拒绝访问错误。
For examples see A basic Windows service in C++ (CppWindowsService) sample in C++ for Visual Studio 2008[^] and Simple Windows Service in C++[^].

SERVICE_TABLE_ENTRY and SERVICE_STATUS are defined in Winsvc.h which is included by Windows.h. So they should be known when compiling.

sc.exe is the service control manager command line application. It must be executed with sufficient privileges to create, start, and stop services. Creation must be done only once and is usually performed by an installation process (or manually for testing / non-public services using a command shell started as Administrator). When using a normal application that calls ShellExecute to execute sc, that application has to be executed too with sufficient privileges.

Note also that you have to delete the service during development when a previous version has been already created. Not doing so will also result in Access denied errors.


这篇关于如何从窗口控制台程序创建Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆