Solved

Closing a program with D-5 with the file name such as 'SOL.EXE'

Posted on 2001-08-15
11
147 Views
Last Modified: 2010-04-06
I want to close programs with D-5 using the file name such as 'SOL.EXE' I need it to work with WIN NT and 9X but NT is the more important one. CODE is a requirement for points! I want to close programs with at least the proficiancy of Win taskman. please all I am realy looking for is a function/procedure and the usage I would love at least a general explination of the code I dislike just using code without a clue on what it is doing I don't get anywere like that.
0
Comment
Question by:Blood_Line
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 300 total points
ID: 6391456
Hello

this code will work with terminate the notepad, you can use it with any application, Test with delphi 5 in win2000(so will work fine with win nt and others)

procedure TForm1.Button1Click(Sender: TObject);
var
  wnd : hwnd;
  ProcessID,cpid : Cardinal;
begin
  wnd:=FindWindow(pchar('notepad'),nil);
  ProcessID := GetWindowThreadProcessID(wnd,@cpid);
  TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,cpid),1);
end;

Best regards
Mohammed Nasman
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6391491
Hello again

  The last code will terminate the application, it will close it even if you don't save without inform you, this code will send message to close the application, similar to use close from the application

procedure TForm1.Button1Click(Sender: TObject);
var
  wnd : hwnd;
  ProcessID,cpid : Cardinal;
begin
  wnd:=FindWindow(pchar('notepad'),nil);
  PostMessage(Wnd, WM_CLOSE, 0, 0);

end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6391545
Mohammed's TerminateProcess code is not good (sorry), because there's a resource leak in it. If you call OpenProcess, you get a handle, which you then MUST close thereafter, otherwise there is a resource leak.

But apart from that, Mohammed's both examples are correct. You should better not search for the window, though. If you e.g. know the to-be-killed exe is named "sol.exe", then you normally don't know the main window's caption, so FindWindow is no solution in that case.

But doesn't matter, Bloodline, you have enough code to enumerate processes from your other question. All that code gives you a processID for "sol.exe". Having this processID, you can call OpenProcess and TerminateProcess for a hard stop (unclean). If you want to simulate a user click on the [X], you have to call EnumWindows to find the main window of the application, then you can use PostMessage with WM_CLOSE for a [X] stop.

Regards, Madshi.
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 22

Expert Comment

by:Mohammed Nasman
ID: 6391979
Thanks alot Madshi for you comment
tell me, is this code will be better?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ProcInfo : TProcessInformation;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  SUInfo : TStartupInfo;
begin
  if ProcInfo.hProcess <> 0 then
    Exit;

  FillChar(SUInfo   , SizeOf(SUInfo)   , #0);
  FillChar(ProcInfo , SizeOf(ProcInfo) , #0);

  SUInfo.cb          := SizeOf(SUInfo);
  SUInfo.dwFlags     := STARTF_USESHOWWINDOW;
  SUInfo.wShowWindow := SW_SHOW;
  if not CreateProcess(nil,
                       pChar('c:\windows\'+
                             'notepad.exe'),
                       nil,
                       nil,
                       FALSE,
                       CREATE_NEW_CONSOLE or
                       NORMAL_PRIORITY_CLASS,
                       nil,
                       pChar(ExtractFilePath('c:\windows\'+
                                             'notepad.exe')),
                       SUInfo,
                       ProcInfo) then
    ShowMessage('CreateProcess FAILED!!!');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  tmpCard : Cardinal;
begin
  if GetExitCodeProcess(ProcInfo.hProcess,tmpCard) then
  begin
    if TerminateProcess(ProcInfo.hProcess,tmpCard) then
      FillChar(ProcInfo, SizeOf(ProcInfo),#0);
  end;
end;

end.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6391993
Hmmm... No, I'm sorry, it's even worse...  :-(

(1) You should use SW_SHOWNORMAL instead of SW_SHOW, when starting a new application.
(2) The handles of ProcInfo are not closed anywhere -> resource leak.
(3) Calling GetExitCodeProcess makes only sense if the application is already terminated. The way you call it does not make sense. "tmpCard" will be filled with STILL_RUNNING, which means that the application is still running. Then when terminating the process, you give in STILL_RUNNING as the exit code. That's kind of paradox...   :-)

The button2 code should look like this:

procedure TForm1.Button2Click(Sender: TObject);
begin
  if TerminateProcess(ProcInfo.hProcess, 0) then begin
    // please *NEVER* forget to close the handles:
    CloseHandle(ProcInfo.hProcess);
    CloseHandle(ProcInfo.hThread );
    FillChar(ProcInfo, SizeOf(ProcInfo), #0);
  end;
end;

Regards, Madshi.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6392226
thanks alot for your comment Madshi
it's really helpful :)

Mohammed

0
 

Author Comment

by:Blood_Line
ID: 6393762
so in the first instance I should be able to stop the resource problem by closing the handle?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6393847
Exactly. E.g.:

var ph : dword;
begin
  ph := OpenProcess(PROCESS_TERMINATE, false, processID);
  TerminateProcess(ph, 0);
  CloseHandle(ph);
end;

Regards, Madshi.
0
 

Author Comment

by:Blood_Line
ID: 6405056
hmmm I am shure I more than likely have what I need to shute the programs down it is just piecing it together for what I need it to do the problem I am running into is trying to associat wich prosses go to which program sorry I am going to have to hold off on points for the moment to sort things out have some patience with me guys thanks
0
 

Author Comment

by:Blood_Line
ID: 7306765
hmmm I guess I never awarded points for this last year woops well 300 for you mnasman  :)
0
 

Author Comment

by:Blood_Line
ID: 7306770
thank you both sorry I took so long to accept an answer
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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