Solved

How do I kill a service

Posted on 2003-11-28
6
208 Views
Last Modified: 2010-04-05
Hi, my question is simple, I want to kill a service programatically. I want to optain a functionality like psKill from sysInternals.com.

I have tried the TerminteProcess function but I get an error (access denied).

Anyone know how to??
0
Comment
Question by:kbb2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 64 total points
ID: 9837224
Hi,

Here it is (ServiceStop function):
unit Unit1;

interface

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

type
 TForm1 = class(TForm)
   Button1: TButton;
   Button2: TButton;
   procedure Button1Click(Sender: TObject);
   procedure Button2Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

uses WinSvc;

function ServiceStart(sMachine, sService: String) : Boolean;
var
  schm,   schs: SC_Handle;
  ss: TServiceStatus;
  psTemp: PChar;
  dwChkP: DWord;
begin
  ss.dwCurrentState := 0;
 schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
  if (schm>0) then
  begin
   schs := OpenService(schm, PChar(sService), SERVICE_START or SERVICE_QUERY_STATUS);
    if (schs>0) then
   begin
     psTemp := nil;
      if (StartService(schs, 0, psTemp)) then
        if (QueryServiceStatus(schs, ss)) then
         while (SERVICE_RUNNING<>ss.dwCurrentState) do
          begin
           dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
           if (not QueryServiceStatus(schs, ss)) then    
            Break;
            if ss.dwCheckPoint <> 0 then  
              Break;
          end;
      CloseServiceHandle(schs);
    end;
   CloseServiceHandle(schm);
  end;
 Result := SERVICE_RUNNING=ss.dwCurrentState;
end;

function ServiceStop(sMachine, sService: String) : Boolean;
var   schm,   schs: SC_Handle;
  ss: TServiceStatus;
 dwChkP: DWord;
begin  
schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
 if (schm>0) then   begin
    schs := OpenService(schm, PChar(sService), SERVICE_STOP or SERVICE_QUERY_STATUS);
    if (schs>0) then
    begin
     if (ControlService(schs, SERVICE_CONTROL_STOP, ss)) then
        if (QueryServiceStatus(schs, ss)) then
         while (SERVICE_STOPPED<>ss.dwCurrentState) do
         begin  
          dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if (not QueryServiceStatus(schs, ss)) then
              Break;
            if ss.dwCheckPoint <> 0 then
              Break;
          end;
      CloseServiceHandle(schs);
    end;
   CloseServiceHandle(schm);
  end;
  Result := SERVICE_STOPPED=ss.dwCurrentState;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ServiceStart('','Alerter');
//ServiceStart('\\computername','Alerter');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ServiceStop('','Alerter');
end;

end.

Regards, Geo
0
 
LVL 6

Assisted Solution

by:CyberGhost
CyberGhost earned 62 total points
ID: 9837280
There are several restrictions depending on OS you are using.
Windows 98 may allow you to kill all processes except of Windows XP will not.
Also the original "Task Manager" on Windows XP is sometimes unable to kill a process (see http://netsecurity.about.com/gi/dynamic/offsite.htm?site=http://www.diamondcs.com.au/index.php%3Fpage=taskman for more info).

Also system processes cannot be killed at all (don´t actually know if in Win9x too).

So maybe I am not really right but there´s no way to kill any process you want.

CyberGhost
0
 
LVL 2

Assisted Solution

by:TheLeader
TheLeader earned 62 total points
ID: 9837286
will I wrote a complete code in this link
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20800952.html

hope it helped ...
0
 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 62 total points
ID: 9837331
for me works always with success this one....

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Tlhelp32, StdCtrls;

type
TForm1 = class(TForm)
    Button1: TButton;
   

procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
function KillTask(ExeFileName: string): integer;
implementation

{$R *.dfm}
function KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin

if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then

Result := Integer(TerminateProcess(OpenProcess(
PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0));
ContinueLoop := Process32Next(FSnapshotHandle,
FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
killtask('WinVNC.exe');
end;

end.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Spellcheck in Webbrowser 1 50
Delphi Firemonkey Need Sample for Online Shopping Example. 2 193
Delphi Yen format 3 62
SUM 2 INTEGER ARRAYS INTO 1 10 111
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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