We've demonstrated how to check the status of Windows services and how to control them. One element missing from those functions was a way to convert between key and display names of Windows services.
uses WinSvc;
//-------------------------------------
// Get the service key name that is
// associated with a specified
// service's display name
// ie: 'Browser' is the key name for
// 'Computer Browser'
//
// sMachine:
// machine name, ie: \SERVER
// empty = local machine
//
// sService
// service display name,
// ie: 'Computer Browser'
//
function ServiceGetKeyName(
sMachine,
sServiceDispName : string ) : string;
var
//
// service control
// manager handle
schm : SC_Handle;
//
// max key name len
nMaxNameLen : integer;
//
// temp. string
psServiceName : PChar;
begin
Result := '';
// expect a service key
// name shorter than 255
// characters
nMaxNameLen := 255;
// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
psServiceName :=
StrAlloc(nMaxNameLen+1);
if(nil <> psServiceName)then
begin
if( GetServiceKeyName(
schm,
PChar(sServiceDispName),
psServiceName,
nMaxNameLen ) )then
begin
psServiceName
[nMaxNameLen] := #0;
Result :=
StrPas( psServiceName );
end;
StrDispose(psServiceName);
end;
// close service control
// manager handle
CloseServiceHandle(schm);
end;
end;
//-------------------------------------
// Get the service key name that is
// associated with a specified
// service's display name
// ie: 'Browser' is the key name for
// 'Computer Browser'
//
// sMachine:
// machine name, ie: \SERVER
// empty = local machine
//
// sService
// service display name,
// ie: 'Computer Browser'
//
function ServiceGetKeyName(
sMachine,
sServiceDispName : string ) : string;
var
//
// service control
// manager handle
schm : SC_Handle;
//
// max key name len
nMaxNameLen : integer;
//
// temp. string
psServiceName : PChar;
begin
Result := '';
// expect a service key
// name shorter than 255
// characters
nMaxNameLen := 255;
// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
psServiceName :=
StrAlloc(nMaxNameLen+1);
if(nil <> psServiceName)then
begin
if( GetServiceKeyName(
schm,
PChar(sServiceDispName),
psServiceName,
nMaxNameLen ) )then
begin
psServiceName
[nMaxNameLen] := #0;
Result :=
StrPas( psServiceName );
end;
StrDispose(psServiceName);
end;
// close service control
// manager handle
CloseServiceHandle(schm);
end;
end;
Listing #1 : Delphi code. Download disp2key (0.72 KB).
uses WinSvc;
//-------------------------------------
// Get the service display name that is
// associated with a specified
// service's display name
// ie: 'Computer Browser' is the
// display name for 'Browser'
//
// sMachine:
// machine name, ie: \SERVER
// empty = local machine
//
// sService
// service key name,
// ie: 'Browser'
//
function ServiceGetDisplayName(
sMachine,
sServiceKeyName : string ) : string;
var
//
// service control
// manager handle
schm : SC_Handle;
//
// max display name len
nMaxNameLen : integer;
//
// temp. string
psServiceName : PChar;
begin
Result := '';
// expect a service display
// name shorter than 255
// characters
nMaxNameLen := 255;
// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
psServiceName :=
StrAlloc(nMaxNameLen+1);
if(nil <> psServiceName)then
begin
if( GetServiceDisplayName(
schm,
PChar(sServiceKeyName),
psServiceName,
nMaxNameLen ) )then
begin
psServiceName
[nMaxNameLen] := #0;
Result :=
StrPas( psServiceName );
end;
StrDispose(psServiceName);
end;
// close service control
// manager handle
CloseServiceHandle(schm);
end;
end;
//-------------------------------------
// Get the service display name that is
// associated with a specified
// service's display name
// ie: 'Computer Browser' is the
// display name for 'Browser'
//
// sMachine:
// machine name, ie: \SERVER
// empty = local machine
//
// sService
// service key name,
// ie: 'Browser'
//
function ServiceGetDisplayName(
sMachine,
sServiceKeyName : string ) : string;
var
//
// service control
// manager handle
schm : SC_Handle;
//
// max display name len
nMaxNameLen : integer;
//
// temp. string
psServiceName : PChar;
begin
Result := '';
// expect a service display
// name shorter than 255
// characters
nMaxNameLen := 255;
// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
psServiceName :=
StrAlloc(nMaxNameLen+1);
if(nil <> psServiceName)then
begin
if( GetServiceDisplayName(
schm,
PChar(sServiceKeyName),
psServiceName,
nMaxNameLen ) )then
begin
psServiceName
[nMaxNameLen] := #0;
Result :=
StrPas( psServiceName );
end;
StrDispose(psServiceName);
end;
// close service control
// manager handle
CloseServiceHandle(schm);
end;
end;
Listing #2 : Delphi code. Download key2disp (0.72 KB).