(page requirements)

Windows services - Converting between key and display names

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.
 
That's right, a Windows service has two names. The long name you see in the Control Panel is the display name of the service. The internal shorter name of the serivce is called the key name. Most functions require that you use the key name of a service, so here's a function to convert a display name to a key name.
 
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;
Listing #1 : Delphi code. Download disp2key (0.72 KB).
 
For example, "ServiceGetKeyName( '', 'Computer Browser' )" will return "Browser", "browser" being the key name that you can pass to Windows services related functions. Of course, you may also have a need to convert a key name to a display name:
 
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;
Listing #2 : Delphi code. Download key2disp (0.72 KB).
 
 

Serbo-Croatian translation by Jovana

 
 
Applicable Keywords : Delphi, Delphi 3.x, Functions, Win32
 
 
 
Copyright © 2009 Chami.com. All Rights Reserved. | Advertise | Created in HTML Kit editor