Solved

Closing Windows Applications programatically

Posted on 1998-01-18
9
130 Views
Last Modified: 2010-04-04
I am using D1 and WIN 3.11
I need a way of closing other open applications from within my ap.  The other programs which may be open, I have written myself in Delphi, but the Title Bars do not contain a caption, so I can't use the obvious API call.
Can someone please provide a code snippet for me.

Thanx in advance
0
Comment
Question by:jdthedj
  • 4
  • 3
  • 2
9 Comments
 
LVL 5

Expert Comment

by:ronit051397
Comment Utility
Closing a notepad document:
SendMessage(FindWindow('Notepad', nil), wm_Close, 0, 0);
0
 
LVL 3

Author Comment

by:jdthedj
Comment Utility
Sorry, Ronit, but that won't work because as I mentioned, the ap does not have a caption in the Title Bar.
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
Ronit's answer doesn't need a caption in the title bar!  Replace 'notepad' with your form class name:

E.g.  SendMessage(FindWindow(PChar('TMyForm'), nil), WM_CLOSE, 0, 0);

JB
0
 
LVL 3

Author Comment

by:jdthedj
Comment Utility
Hi JimBob - thanks for replying.  I've just tried your suggestion and I still can't get the Ap to close - any other ideas??  JD

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
Mmm...  I've used this method in Delphi 2 & 3, so I assume it should work in Delphi 1.  (The Win api help is the same for Win 3.1 and Win 95.)

You can check it by creating a simple Delphi app, with form class TMyForm.  Create a second app which calls SendMessage(...);  If your first app has no code preventing it from closing, then it should close!  Try also with notepad and Ronit's SendMessage code.

It's possible that Delphi 1 works a little differently from Delphi 2 & 3.  If this is the case try:
In your app to close:
  Application.Title := 'Test App';

In your calling app:
  SendMessage(FindWindow('TApplication', 'Test App'), WM_CLOSE, 0, 0);

I don't think this will work, but try it anyway...

JB
0
 
LVL 3

Author Comment

by:jdthedj
Comment Utility
Thanks JimBob

I ran a test as you suggested and it worked. I cut and pasted the code to my own calling ap and it didn't work.  I finally realised that both forms were called TMainForm!!
There's 50 points up for grabs if you turn this into an answer  (or should Ronit come back and claim it).

Thanks again.
0
 
LVL 5

Expert Comment

by:ronit051397
Comment Utility
Jimi deserves the points, mainly for his patience.
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
Comment Utility
Oh, well, if you insist...   ;-)

0
 
LVL 3

Author Comment

by:jdthedj
Comment Utility
Thanks to both you guys - and sorry, Ronit - My ignorance of WINAPI calls and things I had read led me to believe that you needed a caption to work with.
JD
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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

11 Experts available now in Live!

Get 1:1 Help Now