Solved

TerminateProcess(), Task Manager...

Posted on 2002-06-14
7
214 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 33

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 144
Create Database on Android via Delphi dbExpress 3 94
Delphi 2 61
How to load 2 images in same column in Delphi 2 47
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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