Solved

How can I tell what services are running

Posted on 2000-02-25
4
137 Views
Last Modified: 2010-04-04
How can I tell what NT services are running? I use D5 Ent.

Morten
0
Comment
Question by:mdrasted
  • 2
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
Lischke earned 200 total points
ID: 2558067
Hi Morten,

try this code to enumerate services:

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Winsvc,
  StdCtrls, CheckLst;

type
  TForm1 = class(TForm)
    ServiceBox: TCheckListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ServiceBoxClickCheck(Sender: TObject);
  private
    FManager: SC_HANDLE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);

var
  EnumStatus,
  Run: PEnumServiceStatus;
  S: PChar;
  BytesNeeded,
  Returned,
  Resume: DWORD;
  I: Integer;
  Service: SC_HANDLE;

begin
  FManager := OpenSCManager(nil, nil, SC_MANAGER_CONNECT or SC_MANAGER_ENUMERATE_SERVICE);
  if FManager = 0 then RaiseLastWin32Error
                  else
  begin
    Resume := 0;
    BytesNeeded := 0;
    EnumStatus := nil;
    // determine buffer size
    EnumServicesStatus(FManager, SERVICE_WIN32, SERVICE_ACTIVE or SERVICE_INACTIVE, EnumStatus^, 0,
                       BytesNeeded, Returned, Resume);
    if GetLastError = ERROR_MORE_DATA then
    begin
      GetMem(EnumStatus, BytesNeeded);
      EnumServicesStatus(FManager, SERVICE_WIN32, SERVICE_ACTIVE or SERVICE_INACTIVE, EnumStatus^, BytesNeeded,
                         BytesNeeded, Returned, Resume);
      Run := EnumStatus;
      for I := 0 to Returned - 1 do
      begin
        // add service to listbox
        with Run^ do
        begin
          S := AllocMem(StrLen(lpServiceName) + 1);
          Move(lpServiceName^, S^, StrLen(lpServiceName) + 1);
          ServiceBox.Items.AddObject(lpDisplayName, Pointer(S));

          // query running state
          Service := OpenService(FManager, lpServiceName, SERVICE_QUERY_STATUS);
          QueryServiceStatus(Service, ServiceStatus);
          if (ServiceStatus.dwCurrentState and SERVICE_RUNNING) <> 0 then ServiceBox.Checked[I] := True;
          CloseServiceHandle(Service);
        end;
        Inc(Run);
      end;
      FreeMem(EnumStatus);
    end;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);

var
  I: Integer;

begin
  for I := 0 to ServiceBox.Items.Count - 1 do
    FreeMem(Pointer(ServiceBox.Items.Objects[I]));
  if FManager <> 0 then CloseServiceHandle(FManager);
end;

procedure TForm1.ServiceBoxClickCheck(Sender: TObject);

var
  ServiceStatus: TServiceStatus;
  Service: SC_HANDLE;
  Dummy: PChar;
  Result: Boolean;

begin
  with ServiceBox do
  begin
    // state is already switched
    if not Checked[ItemIndex] then
    begin
      if MessageDlg('Do you want to stop this service?', mtConfirmation, [mbOK, mbCancel], 0) = mrOK then
      begin
        Service := OpenService(FManager, PChar(Items.Objects[ItemIndex]), SERVICE_STOP);
        if Service <> 0 then
        begin
          Result := ControlService(Service, SERVICE_CONTROL_STOP, ServiceStatus);
          CloseServiceHandle(Service);
          if not Result then
          begin
            Checked[ItemIndex] := True;
            RaiseLastWin32Error;
          end;
        end
        else
        begin
          Checked[ItemIndex] := True;
          RaiseLastWin32Error;
        end;
      end
      else
        Checked[ItemIndex] := True;
    end
    else
    begin
      if MessageDlg('Do you want to start this service?', mtConfirmation, [mbOK, mbCancel], 0) = mrOK then
      begin
        Service := OpenService(FManager, PChar(Items.Objects[ItemIndex]), SERVICE_START);
        if Service <> 0 then
        begin
          Dummy := nil;
          Result := StartService(Service, 0, Dummy);
          // check here periodically whether the service could be started
          CloseServiceHandle(Service);
          if not Result then
          begin
            Checked[ItemIndex] := False;
            RaiseLastWin32Error;
          end;
        end
        else
        begin
          Checked[ItemIndex] := False;
          RaiseLastWin32Error;
        end;
      end
      else
        Checked[ItemIndex] := False;
    end;
  end;
end;

end.


Open a new project and place a check list box on the main form. Name it "ServiceBox" and try it out.

Notes:
- I have not included the validation loops after starting and stopping a service, so wait a few seconds before you try to stop a service after you have started it with this code
- this code works only under WinNT
- if you get exceptions then it is likely that access has been denied (e.g. I can start my RPC locator service with this code, but not stop it. This happens also with the control panel "Services", so this is not a failure in the code).
- for a more complete implementation you need additional code to pass start parameters (if any, on my system no service needs parameters) and dependency checks before shutting down or starting a service

Ciao, Mike
0
 
LVL 1

Author Comment

by:mdrasted
ID: 2558264
Hi Mike,

I'll get back to you monday.

Morten
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2558331
Okey, dokey, have a nice weekend.

Ciao, Mike
0
 
LVL 1

Author Comment

by:mdrasted
ID: 2567874
Hi again,

Thanks for helping me. Nice code.

Regards,
Morten
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question