Solved

TerminateProcess(), Task Manager...

Posted on 2002-06-14
7
205 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now