如何在运行时或OnAfterInstall上启动Windows服务 [英] How to start my Windows Service at RunTime or at OnAfterInstall

查看:267
本文介绍了如何在运行时或OnAfterInstall上启动Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

历史

我正在构建 Windows服务作为我平台应用程序来处理更新和服务器功能,因此它可以 安装在与 Client Application 已安装。它使用UDP发送和接收广播消息,并使用TCP处理更敏感和最重要的消息。

I am building a Windows Service as part of my Platform Application to handle updates and server functions, so it can be installed in machines different than where the Client Application is installed. It uses UDP to send and receive broadcast messages, and TCP to to handle more sensitive and most important messages.

客观

我希望最终用户可以轻松地安装我的应用程序,只需在计算机中复制可执行文件并在执行时即可。主应用程序应验证用户是否为管理员,创建配置文件,然后安装Windows Service并运行它,因此,非管理用户登录时,他们不会从我的应用程序收到任何有关管理权限的错误。我们的目标是在不需要现有技术人员的情况下进行大多数配置,因为数据库将是远程的。

I want that my Application could be easily installed by the end user, just by copying the executable files in the computers and when executing them. The main application should verify if the user is an administrator, create the configuration files, and then install the Windows Service and run it, so when non administrative users log in, they won't receive any error from my Application regarding administrative rights. The goal is to make most configurations without the need a present technician, since database will be remote.

问题

我的服务通常使用 MyService.exe / install 命令安装,但不会自动启动。启动它的唯一方法是进入控制面板>。管理工具>服务,然后手动执行。我试图通过我的应用程序调用 net start MyService ,但是在外壳程序中收到无效的服务名称。我尝试了可执行名称显示名称对象名称服务,但没有一个起作用。这是我的TService的对象:

My Service is being installed normally with the command MyService.exe /install but it is not starting automatically. The only way to start it is to go on Control Panel > Admin Tools > Services and do it manually. I tryed to call net start MyService through my application but I receive invalid service name at the shell. I tryied the executable name, the display name and the object name of the service, but none of them worked. This is the object of my TService:

object ServiceMainController: TServiceMainController
  OldCreateOrder = False
  OnCreate = ServiceCreate
  DisplayName = 'PlatformUpdateService'
  Interactive = True
  AfterInstall = ServiceAfterInstall
  AfterUninstall = ServiceAfterUninstall
  OnShutdown = ServiceShutdown
  OnStart = ServiceStart
  OnStop = ServiceStop
  Height = 210
  Width = 320 

问题

该如何通过代码而不是用户的手来启动服务?如果可以在客户端应用程序中执行此操作,或者在 OnServiceAfterInstall 调用之后单独执行操作,那将是最好的选择。

What should I do to start my service by the code and not by the user hands? It would be the best if I could do it inside my client application, or when by itself after the OnServiceAfterInstall call.

推荐答案

AfterInstall 事件中,有一个示例 StartService 调用:

Here's a sample StartService call, in AfterInstall event:

procedure TServiceMainController.ServiceAfterInstall(Sender: TService);
var
  Manager, Service: SC_HANDLE;
begin
  Manager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  Win32Check(Manager <> 0);
  try
    Service := OpenService(Manager, PChar(Name), SERVICE_ALL_ACCESS);
    Win32Check(Service <> 0);
    try
      Win32Check(StartService(Service, 0, PChar(nil^)));
    finally
      CloseServiceHandle(Service);
    end;
  finally
    CloseServiceHandle(Manager);
  end;
end;

但是我不确定它是否对您有用,因为通常情况下,您应该成功使用 net start 也是如此。

However I'm not sure it will work for you, as normally you should have success with net start too.

这篇关于如何在运行时或OnAfterInstall上启动Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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