Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do You Start And Stop An External Program?

Status
Not open for further replies.

TBaz

Technical User
Jun 6, 2000
65
ES
I have a Delphi program where you select a program - ie Calculator, Notepad etc.

A 'Start' button launches the program you selected (let's say it's Calculator).

When you click on the 'Stop' button which closes the Calculator.

This needs to work regardless of the program chosen by the user.

At first, I thought this would be fairly straight forward.

Using ShellExecute the external program launches fine. The problem is stopping it from running.

There's also PostMessage for closing an application, but when you launch a program with ShellExecute there doesn't seem to be any way of finding out what handle the app is using for the PostMessage function.

Anyone give me some pointers on what I'm doing wrong please or suggest an alternative method?

TBaz
 
Close application by window name
Use FindWindow function, which returns handle of window, which you want to close. And after that, send WM_CLOSE message to this window.

procedure TForm1.Button1Click(Sender: TObject);
var
MyHandle: THandle;
begin
MyHandle:=FindWindow(nil, 'Calculator');
SendMessage(MyHandle, WM_CLOSE, 0, 0);
end;

Aaron Taylor
John Mutch Electronics
 
Quote:

"There's also PostMessage for closing an application, but when you launch a program with ShellExecute there doesn't seem to be any way of finding out what handle the app is using for the PostMessage function."

This applies equally to the SendMessage function.

TenBaz
 
Quote:

"is that another question ???"

No, it's the question I asked in the OP. I think you misunderstood what I asked, or didn't read it properly.

"personally i dont see the point in switching back to your app to close the app you just left."

Lol. So you answer a question I don't ask, then say you can't see the point in doing what I'm trying to do?

I don't think it's unreasonable to be able to launch an exe with a button in a Delphi program and then use a button in the same program to close the launched app when you are finished with it.

You may for example have written a series of external controller programs which need to be launched and closed at will from the main program.

But, I'm grateful that you tried to help though.

whosrdaddy:

Thanks for the link - not exactly what I was after, but it answered a lot of questions and pointed me in the right direction! :)

TBaz
 
whosrdaddy:

Thanks for the link - not exactly what I was after, but it answered a lot of questions and pointed me in the right direction! :)

The link shows code how to terminate the process that was created from the delphi app. I think that is the answer to your question...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
The link shows code how to terminate the process that was created from the delphi app. I think that is the answer to your question..."

No, unless I'm being incredibly stupid - which I know is quite possible - that link shows you how to launch a program then wait until the launched program is terminated. So, it would run Notepad for example then wait until you clicked on Exit in Notepad.

I wanted to launch Notepad with a button from my app then close Notepad with a button in my app - not even touching Notepad itself. (Notepad is just the example exe I used as everyone has it. In my finished app, it will launch various exe's which do tasks with no user intervention and need to be closed via my main program).

There's a slight difference between the two. But, the link helped a lot as it used CreateProcess which is what I needed to use rather than ShellExecute, so thanks for pointing me in the right direction.

TBaz
 
straight fom the link:

Code:
procedure TForm1.btnCancelClick(Sender: TObject);
begin
  Timer1.Enabled := False;
  if Application.MessageBox('You should try to finish'
  + ' the application normally.'#13#13'¿Terminate it '
  + 'anyway?', 'Warning', MB_YESNO + MB_DEFBUTTON2 +
  MB_ICONQUESTION + MB_TASKMODAL) = ID_YES then begin
    TerminateProcess(proc_info.hProcess, 0);
    CloseHandle(proc_info.hProcess);
    btnCancel.Enabled := False;
    btnExecute.Enabled := True;
  end else begin
    Timer1.Enabled := True;
  end;
end;

I think that answers your question [morning]

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
OK - I concede an element of stupidity! Lol

I made the error of reading what it said the program did at the top of the page - "run an external application and wait until it terminates".

I have to admit that I didn't read the code at the bottom of the page carefully as by the time I'd read the first section of code I'd had an idea and went off and experimented.

Either way, thanks for the link - it did help - though I could have saved myself a bit of time if I'd read it all a little more carefully... :)

TBaz
 
np man! Glad I could help...

[2thumbsup]



-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
how did i misunderstand your question ????????

you asked how to get the handle and close the app

the sample i posted does that
read it

Close application by window name
Use FindWindow function, which returns handle of window, which you want to close. And after that, send WM_CLOSE message to this window.

procedure TForm1.Button1Click(Sender: TObject);
var
MyHandle: THandle;
begin
MyHandle:=FindWindow(nil, 'Calculator');
SendMessage(MyHandle, WM_CLOSE, 0, 0);
end;

whats the problem ????????

Aaron Taylor
John Mutch Electronics
 
aaronjme

"whats the problem ????????"

There's no problem other than the fact that I asked a question, you answered a different question with code that doesn't work *for what I said I was trying to do* and you are now saying that I have the problem!

"you asked how to get the handle and close the app"

No I didn't. I asked how to get the handle and close ANY app!

I don't really have time for this, but if you read my original post properly, I clearly said:

"I have a Delphi program where you select a program - ie Calculator, Notepad etc."

In your code you have:

MyHandle:=FindWindow(nil, 'Calculator');

So what if my app launches Outlook Express or ANY program other than calculator? How does it know what is on the title bar?

Using FindWindow to look for 'Calculator' certainly isn't going to help is it?

I also said in the same post:

"when you launch a program with ShellExecute there doesn't seem to be any way of finding out what handle the app is using for the PostMessage function"

This is still true.

Your code works fine in a program where you can launch any program you like... as long as it's the Calculator!

whosrdaddy understood my post easily enough and kindly pointed me to the correct way to do it. It was only my own stupidity which prevented me from seeing the code he sent me to look at - I never said I was perfect... :)

You say I should read your post. I have and I suggest you now read mine.

TBaz
 
i gave you the simplest most direct answer to your question.

if you launched calculator then you know you need to close calculator.

if you launched notepad then you need know you need to close notepad.

if you have 10 windows and return 10 handles how are you to know wich 1 is yours to close, obviously you need to know something about the window to isolate the correct handle.

anyways see if these help.




Aaron Taylor
John Mutch Electronics
 
i gave you the simplest most direct answer to your question."

How could it be the 'simplest most direct answer' to my question' if it didn't actually answer the question. I'm sorry, but I don't care what question you *think* I asked.

Had I asked 'how do I launch and then close the Calculator program from within my Delphi application?' then I wouldn't have a leg to stand on. Yours really would have been the simplest most direct answer.

Anyone who reads my initial post has to agree that when I write the phrases "I have a Delphi program where you select a program - ie Calculator, Notepad etc." and "This needs to work regardless of the program chosen by the user", I am not asking for some code to simply launch and close just the calculator!

"if you launched calculator then you know you need to close calculator.

if you launched notepad then you need know you need to close notepad."

Yes I know that (sigh). But just think about what you are saying for a second...

You suggested using FindWindow right? To close Calculator you use:

MyHandle:=FindWindow(nil, 'Calculator');

OK, agreed, but why the 'Calculator' parameter? Because that's what appears on the Calculator window's title bar.

OK, so say you now choose to launch Disk Cleanup (cleanmgr.exe). What do you use as a parameter instead of 'Calculator' in the FindWindow function?

The answer is 'you don't know' because not all programs are as simplistic as Calculator. When you are able to launch ANY program, there's no way to know what is going to be on the title bar of the program you launch.

You therefore cannot say "you know you launched program X, so you just close down program x' - not without knowing the handle... which brings me back to what I actually asked in my original post.

Look, we can argue about this forever more - something I'm sure neither of us wants to do and I'm also sure that isn't what this forum is for.

As stated a good few posts back, the problem is now solved, but I would still like to thank you for your attempt at helping with the problem.

I quite like a good argument as long as everyone is still friends at the end of it, but I think this one has gone as far as it can now. :)

TBaz
 
TBaz et al,
Seeing as TBaz's problem has been solved, I think it would be wise to halt the discussion there. ;-)

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Perhaps not, what about CreateProcess and SendMessage to start and terminate programs? I've always used those to do that.

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
omg stop it already, problem is solved...

[hairpull]


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Not really, this way it doesn't account for multiple windows of the same program to be open like for example with internet explorer. You can't tell which handle it will return if there is two windows open for IE and and use FindWindow('Microsoft Internet Explorer');

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top