Sign in Join
Wiki copytemporary

ServiceManager: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: ServiceManager

OldNewLine
1+*(Windows only)*
2+
3+## ServiceManager
4+
5+For Windows, the fcl-extra package contains a unit called ServiceManager.
6+
7+The example below checks whether the SamSs (Security Accounts Manager; should always be running) service is running. (Command line equivalent `sc query samss`).
8+
9+### Service Name
10+
11+Please note that if you develop your own service using the Lazdaemon package, the service name you must use is important. While the commands NET START and NET STOP use the service display name, the command SC and the code below use the service internal name. The display name is the one given to the property "DisplayName" of the item created in the DaemonDefs items. The service name is the property "Name" of the same item. The display name is the one currently displayed in the **Name** column by the MS services manager applet (services.msc) Let's use an example to clarify this. Assume you have written a service application called **myService.exe** in which there is one item in the DaemonDefs. The item has a name of "**MyServ**" and the associated "DisplayName" property has the value of "**Internal build service**". Using the command line as administrator you can:
12+
13+### Service management using console
14+
15+You will need: Microsoft .NET Framework 4.5.2 (Web Installer) for Windows Vista SP2, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2008 SP2 Windows Server 2008 R2 SP1, Windows Server 2012 and Windows Server 2012 R2
16+
17+You have to get the support of .NET SDK Command prompt for install the service into your local machine. 1. Call to SDK Command prompt, follow Start\>All Programs\>Microsoft .NET Framework SDK v2.0\>SDK Command prompt
18+
19+Install the service:
20+
21+```dos
22+\your\path\to\application\myService.exe --install
23+rem .... or /install, -install works too
24+```
25+
26+Activate the service: In these commands, do not forget **double quotes** when multiple words are needed!
27+
28+```dos
29+net start "Internal build service"
30+rem .... or alternatively:
31+sc start "MyServ"
32+```
33+
34+To stop the service:
35+
36+```dos
37+net stop '''"Internal build service"'''
38+rem ... or alternatively
39+sc stop "'''MyServ'''"
40+```
41+
42+## Code example
43+
44+Given the above, the code snippet to call the function below would be in our case:
45+
46+``` pascal
47+if IsServiceRunning('MyServ') then
48+```
49+
50+Example program:
51+
52+``` pascal
53+program ServiceTest;
54+// Check if a certain process is running.
55+{$mode objfpc}{$H+}
56+uses
57+ Classes,
58+ SysUtils,
59+ ServiceManager,
60+ JwaWinSvc {for services declarations};
61+
62+function IsServiceRunning(ServiceName: string): boolean;
63+ {description Checks if a Windows service is running}
64+var
65+ Services: TServiceManager;
66+ ServiceStatus: TServiceStatus;
67+begin
68+ //Check for existing services
69+ //equivalent to sc query <servicename>
70+ Services := TServiceManager.Create(nil);
71+ try
72+ try
73+ Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
74+ //We don't need more access permissions than this; by default
75+ //the servicemanager is trying to get all access
76+ Services.Connect; //Now connect with requested access level
77+ Services.GetServiceStatus(ServiceName, ServiceStatus);
78+ Result := (ServiceStatus.dwCurrentState = SERVICE_RUNNING);
79+ Services.Disconnect;
80+ except
81+ on E: EServiceManager do
82+ begin
83+ // A missing service might throw a missing handle exception? No?
84+ {LogOutput('Error getting service information for ' + ServiceName +
85+ '. Technical details: ' + E.ClassName + '/' + E.Message); }
86+ Result := False;
87+ raise; //rethrow original exception
88+ end;
89+ on E: Exception do
90+ begin
91+ {LogOutput('Error getting service information for ' + ServiceName +
92+ '. Technical details: ' + E.ClassName + '/' + E.Message); }
93+ Result := False;
94+ raise; //rethrow original exception
95+ end;
96+ end;
97+ finally
98+ Services.Free;
99+ end;
100+end;
101+
102+const
103+ ServiceToTest = 'SamSs';
104+
105+//Security Accounts Manager, should be running, at least on Vista
106+begin
107+ WriteLn('Starting test for ' + ServiceToTest + ' service.');
108+ if IsServiceRunning(ServiceToTest) then
109+ WriteLn('The ' + ServiceToTest + ' service is running')
110+ else
111+ WriteLn('The ' + ServiceToTest + ' service is not running');
112+end.
113+```
114+
115+## See also
116+
117+- [[Daemons and Services]]
118+- [[TServiceManager]]
119+
120+---
121+*From the Free Pascal wiki, page "ServiceManager", as it stood on 2022-03-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history