Solved

TerminateProcess(), Task Manager...

Posted on 2002-06-14
7
228 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
[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
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
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!

 
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

Technology Partners: 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

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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

628 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