Solved

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

Posted on 2001-08-15
11
135 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:
mnasman 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:mnasman
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
 
LVL 22

Expert Comment

by:mnasman
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 22

Expert Comment

by:mnasman
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

21 Experts available now in Live!

Get 1:1 Help Now