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
Solved

TerminateProcess(), Task Manager...

Posted on 2002-06-14
7
217 Views
Last Modified: 2010-04-04
I wrote a client/server software. I login client from server and logged pc runs Counter Strike game. When i logout i am terminating all running visible windows. It works fine but if i try to run Counter Strike again it generates error so it doesn't run. It also happens with Fifa sometimes. But this problem started after the new version of CS game. If i terminate Counter Strike using Ctrl + Alt + Del so it works fine again. This is happening in win98 i didn't try other os. I also tried to kill DDhelp (DirectX) as written MSDN but no chance. I like to know what does ctrl + alt + del uses and how does it force terminate applications?

Termination code i wrote is:

function TEasyUtils.KillTask(pid: Int64): boolean;
var
  hw: THandle;
  ename: string;
begin
  Result := False;
  if (pid > 0) and (pid <> GetCurrentProcessId) then
  begin
    hw := OpenProcess(PROCESS_ALL_ACCESS, False, pid);
    if hw > 0 then
    try
      Result := TerminateProcess(hw, 0);
    finally
      CloseHandle(hw);
    end;
  end;
end;

I call this like:

var
  pid: Cardinal;
begin
  GetWindowThreadProcessId(winhwn, @pid);
  KillTask(pid);
end;

Thanks...
0
Comment
Question by:Dumani
7 Comments
 
LVL 34

Expert Comment

by:Slick812
ID: 7080031
you might try to ask the app to Close with
PostMessage(hAppHandle,WM_CLOSE,0,0); so it can maybe shut down and save finalization info to file, before you jerk the process and wack the app out before it can close it's self down. If it still don't close, then kill the process.
0
 
LVL 1

Author Comment

by:Dumani
ID: 7080143
Thanks but i have tried that, that doesn't really work. I want to simulate Task Manager Terminate Process...
0
 
LVL 17

Expert Comment

by:geobul
ID: 7083239
Hi,
Change either
function KillTask(pid: DWORD): boolean;
or
hw := OpenProcess(PROCESS_TERMINATE, False, DWORD(pid));
and try again.

Regards, Geo
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Author Comment

by:Dumani
ID: 7083293
Thanks GEO but that's not the solution
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 7083408
I'm not sure what the task manager does exactly. But he's kindly asking the process to end. Only if the process doesn't do after 5 seconds, he forces it with TerminateProcess.

Some ideas:

(1) Post WM_CLOSE to the *main window*. That's important. It's wrong to post it to the application handle, that can not even work. And it's also wrong to post it to any window, just post it to the MAIN window.
(2) Try sending WM_QUERYENDSESSION to the main window. If the process accepts that (see the documentation), send a WM_ENDSESSION afterwards and then call TerminateProcess.
(3) Try posting a WM_QUIT to the main thread (PostThreadMessage). That's definately NOT what the task manager does, but it might be worth a try, nevertheless. It works fine for some processes, but not for all.

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9010895
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from Madshi

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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