Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2001-08-15
11
Medium Priority
?
160 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
  • 4
  • 4
  • 3
11 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 900 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
Industry Leaders: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

824 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