?
Solved

Kill Task like Task Mgr

Posted on 2015-01-14
5
Medium Priority
?
213 Views
Last Modified: 2015-02-01
I want my Uninstall.exe (running with admin rights) to Terminate my Application that was installed and is running with Std User Rights.
Whereas this works in OS above win XP it fails on Win XP.

function Tform_Main.KillTask(ExeFileName: string): integer;
const
  PROCESS_TERMINATE=$0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle,ProcessHandl: 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
    begin
      ProcessHandl:=OpenProcess(PROCESS_TERMINATE,BOOL(0),FProcessEntry32.th32ProcessID) ;
      Result := Integer(TerminateProcess(ProcessHandl, 0));
      CloseHandle(ProcessHandl);
    end;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

Open in new window

0
Comment
Question by:Allan_Fernandes
[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
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40549914
- first you must find process id using EnumProcesses and compare started process module name with your exe file:

function FindExeProcess(sExe: String): Cardinal;
var
  PIDArray: array [0..1023] of DWORD;
  cb: DWORD;
  i, ProcCount: Integer;
  hMod: HMODULE;
  hProcess: THandle;
  ModuleName: array [0..300] of Char;
begin
  Result := 0;
  sExe := UpperCase(sExe);
  EnumProcesses(@PIDArray, SizeOf(PIDArray), cb); //get all process list
  ProcCount := cb div SizeOf(DWORD);
  
  for i := 0 to ProcCount - 1 do
  begin
    hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PIDArray[i]);
    if (hProcess <> 0) then
    begin
      EnumProcessModules(hProcess, @hMod, SizeOf(hMod), cb);
      GetModuleFilenameEx(hProcess, hMod, ModuleName, SizeOf(ModuleName));
      if UpperCase(ModuleName) = sExe then
      begin
        Result := PIDArray[i];
      end;
      CloseHandle(hProcess);
      if Result > 0 then Break; //found it
    end;
  end;
end;

Open in new window


... and kill process by process id:
procedure KillProcess(hProcessID: Cardinal);
Var
  hProcess : THandle;
  ovExitCode : LongWord;
begin
  try
    if hProcessID<>0 then
    begin
      hProcess:=OpenProcess(PROCESS_TERMINATE, True, hProcessID);
      if hProcess<>0 then
      begin
        GetExitCodeProcess(hProcess, ovExitCode);
        TerminateProcess(hProcess, ovExitCode);
        CloseHandle(hProcess);
      end;
    end;
  except
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
  hPid: Cardinal;
begin
  hPid := FindExeProcess('C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe');
  if hPid > 0 then
    KillProcess(hPid);
end;

Open in new window

0
 

Author Comment

by:Allan_Fernandes
ID: 40553031
The code you have given works only within the User
0
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40553405
check if function FindExeProcess get valid Pid (>0) for different user. Is running exe 64 bit? or 32bit?
try change:
const
  PROCESS_QUERY_LIMITED_INFORMATION = $1000;
...
hProcess := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, PIDArray[i]);
...

Open in new window

0
 

Author Comment

by:Allan_Fernandes
ID: 40554996
Sorry that does not help either.
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 40556625
Ok. You can try WMI.
Ref: WMI and Win32_Process class

function FindExeProcess2(sExe: String): Cardinal;
const
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator, FWMIService, FWbemObjectSet, FWbemObject: OLEVariant;
  oEnum : IEnumvariant;
  iValue : LongWord;
  PathStr, ModuleName: String;
begin;
  Result := 0;
  sExe := UpperCase(sExe);
  
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_Process','WQL',wbemFlagForwardOnly);
  oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    PathStr := '';
    if not VarIsNull(FWbemObject.ExecutablePath) then
      PathStr := FWbemObject.ExecutablePath;
    ModuleName := IncludeTrailingPathDelimiter(PathStr) + FWbemObject.Name;

    if UpperCase(ModuleName) = sExe then
    begin
      Result := FWbemObject.ProcessId; //keep pid
    end;
    FWbemObject:=Unassigned;
    if Result > 0 then Break;
  end;
end;

procedure KillProcess2(hProcessID: Cardinal);
const
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator, FWMIService, FWbemObjectSet, FWbemObject: OLEVariant;
  oEnum : IEnumvariant;
  iValue : LongWord;
  PathStr, ModuleName: String;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_Process','WQL',wbemFlagForwardOnly);
  oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    if FWbemObject.ProcessId = hProcessID then
    begin
      FWbemObject.Terminate();
      FWbemObject:=Unassigned;
      Break;
    end;
  end;
end;

Open in new window


this works for me - as admin user
more useful samples here:
https://theroadtodelphi.wordpress.com/page/12
0

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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