windows服务

windows服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
unit untserverinfo;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Forms, WinSvc, SvcMgr,
  System.Win.Registry;
 
function ServiceGetStatus(sMachine, sService: string): DWord; // 取服务状态
function ServiceUninstalled(sMachine, sService: string): boolean; // 反安装服务
function ServiceRunning(sMachine, sService: string): boolean; // 判断服务是否运行
function ServiceStopped(sMachine, sService: string): boolean; // 停止服务
function StartService(ServiceName: string; NumberOfArgument: DWord;
  ServiceArgVectors: PChar): boolean; overload; // 服务启动  StartService('',0,nil)
function StartServicea(AServName: string): boolean; // 开始服务
function StopService(ServiceName: string): boolean; // 暂停服务
function ShutdownService(ServiceName: string): boolean; // 御载服务
function IsServiceInstalled(ServiceName: string): boolean; // 服务是否安装
function InstallService(ServiceName, DisplayName, Filename: string;
  ServiceDescription: string = ''): boolean; // 安装服务
procedure ServiceUpdateDescription(const ServiceName, Description: string);
// 添加服务描述
function DoControlService(ServiceName: string; ControlFalg: Cardinal): boolean;
// 反安装服务
function UnInstallService(ServiceName: string): boolean;
 
implementation
 
function UnInstallService(ServiceName: string): boolean;
var
  SCManager, ServiceHandle: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = '') then
    Exit;
  SCManager := OpenSCManager(nil, nil, GENERIC_WRITE);
  if SCManager = 0 then
    Exit;
  try
    ServiceHandle := OpenService(SCManager, PChar(ServiceName), _DELETE);
    Result := DeleteService(ServiceHandle);
    CloseServiceHandle(ServiceHandle);
  finally
    CloseServiceHandle(SCManager);
  end;
end;
 
function DoControlService(ServiceName: string; ControlFalg: Cardinal): boolean;
var
  ServiceStatus: TServiceStatus;
  SCManager, hService: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = '') then
    Exit;
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCManager <> 0 then
  begin
    hService := OpenService(SCManager, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if hService <> 0 then
    begin
      Result := ControlService(hService, ControlFalg, ServiceStatus);
      CloseServiceHandle(hService);
    end;
    CloseServiceHandle(SCManager);
  end;
end;
 
procedure ServiceUpdateDescription(const ServiceName, Description: string);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    with reg do
    begin
      RootKey := HKEY_LOCAL_MACHINE;
      if OpenKey('SYSTEM\CurrentControlSet\Services\' + ServiceName, False) then
      begin
        WriteString('Description', Description);
      end;
      CloseKey;
    end;
  finally
    reg.Free;
  end;
end;
 
function InstallService(ServiceName, DisplayName, Filename: string;
  ServiceDescription: string = ''): boolean;
var
  SCManager, ServiceHandle: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = '') and not FileExists(Filename) then
    Exit;
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCManager = 0 then
    Exit;
  try
    ServiceHandle := CreateService(SCManager, PChar(ServiceName),
      PChar(DisplayName), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS or
      SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
      PChar(Filename), nil, nil, nil, nil, nil);
    if IsServiceInstalled(ServiceName) and (ServiceDescription <> '') then
      ServiceUpdateDescription(ServiceName, ServiceDescription);
    CloseServiceHandle(ServiceHandle);
    Result := ServiceHandle <> 0;
  finally
    CloseServiceHandle(SCManager);
  end;
end;
 
function IsServiceInstalled(ServiceName: string): boolean;
var
  Mgr, Svc: Integer;
begin
  Result := False;
  if (Trim(ServiceName) = '') then
    Exit;
  Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if Mgr <> 0 then
  begin
    Svc := OpenService(Mgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    Result := Svc <> 0;
    if Result then
      CloseServiceHandle(Svc);
    CloseServiceHandle(Mgr);
  end;
end;
 
function ShutdownService(ServiceName: string): boolean;
begin
  Result := DoControlService(ServiceName, SERVICE_CONTROL_SHUTDOWN);
end;
 
function StopService(ServiceName: string): boolean;
begin
  Result := DoControlService(ServiceName, SERVICE_CONTROL_STOP);
end;
 
function ServiceGetStatus(sMachine, sService: string): DWord;
var
  schm, schs: THandle;
  ss: TServiceStatus;
  dwStat: DWord;
begin
  dwStat := 0;
  schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);
  if (schm > 0) then
  begin
    schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS);
    if (schs > 0) then
    begin
      if (QueryServiceStatus(schs, ss)) then
        dwStat := ss.dwCurrentState;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result := dwStat;
end;
 
{ 判断某服务是否安装,未安装返回true,已安装返回false }
 
function ServiceUninstalled(sMachine, sService: string): boolean;
begin
  Result := 0 = ServiceGetStatus(sMachine, sService);
end;
 
{ 判断某服务是否启动,启动返回true,未启动返回false }
 
function ServiceRunning(sMachine, sService: string): boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService);
end;
 
{ 判断某服务是否停止,停止返回true,未停止返回false }
 
function ServiceStopped(sMachine, sService: string): boolean;
begin
  Result := SERVICE_STOPPED = ServiceGetStatus(sMachine, sService);
end;
 
function StartService(ServiceName: string; NumberOfArgument: DWord;
  ServiceArgVectors: PChar): boolean; overload; // More complex start
var
  SCManager, hService: SC_HANDLE;
begin
  Result := False;
  if (Trim(ServiceName) = '') then
    Exit;
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  Result := SCManager <> 0;
  if Result then
    try
      hService := OpenService(SCManager, PChar(ServiceName),
        SERVICE_ALL_ACCESS);
      Result := hService <> 0;
      if (hService <> 0) then
        try
          Result := WinSvc.StartService(hService, NumberOfArgument,
            PChar(ServiceArgVectors));
          if not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then
            Result := True;
        finally
          CloseServiceHandle(hService);
        end;
    finally
      CloseServiceHandle(SCManager);
    end;
end;
 
function StartServicea(AServName: string): boolean; // use WinSvc
var
  SCManager, hService: SC_HANDLE;
  lpServiceArgVectors: PChar;
begin
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  Result := SCManager <> 0;
  if Result then
    try
      hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);
      Result := hService <> 0;
      if (hService = 0) and (GetLastError = ERROR_SERVICE_DOES_NOT_EXIST) then
        Exception.Create('指定的服务不存在!');
      if hService <> 0 then
        try
          lpServiceArgVectors := nil;
          Result := WinSvc.StartService(hService, 0,
            PChar(lpServiceArgVectors));
          if not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then
            Result := True;
        finally
          CloseServiceHandle(hService);
        end;
    finally
      CloseServiceHandle(SCManager);
    end;
end;
 
end.

  

posted @   delphi中间件  阅读(205)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-02-19 DELPHI开发和使用REDIS
2019-02-19 咏南数据序列(还原)类
2012-02-19 插件之注册插件和注册插件中的模块
2012-02-19 插件之类注册
2012-02-19 插件之基类窗体
2012-02-19 插件之接口实现
2012-02-19 主控程序之主窗体
点击右上角即可分享
微信分享提示