• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

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

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
Blood_Line
Asked:
Blood_Line
  • 4
  • 4
  • 3
1 Solution
 
Mohammed NasmanSoftware DeveloperCommented:
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
 
Mohammed NasmanSoftware DeveloperCommented:
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
 
MadshiCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mohammed NasmanSoftware DeveloperCommented:
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
 
MadshiCommented:
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
 
Mohammed NasmanSoftware DeveloperCommented:
thanks alot for your comment Madshi
it's really helpful :)

Mohammed

0
 
Blood_LineAuthor Commented:
so in the first instance I should be able to stop the resource problem by closing the handle?
0
 
MadshiCommented:
Exactly. E.g.:

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

Regards, Madshi.
0
 
Blood_LineAuthor Commented:
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
 
Blood_LineAuthor Commented:
hmmm I guess I never awarded points for this last year woops well 300 for you mnasman  :)
0
 
Blood_LineAuthor Commented:
thank you both sorry I took so long to accept an answer
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now