Solved

Closing Windows Applications programatically

Posted on 1998-01-18
9
134 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
ID: 1356985
Closing a notepad document:
SendMessage(FindWindow('Notepad', nil), wm_Close, 0, 0);
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1356986
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
ID: 1356987
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 3

Author Comment

by:jdthedj
ID: 1356988
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1356989
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
ID: 1356990
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
ID: 1356991
Jimi deserves the points, mainly for his patience.
0
 
LVL 5

Accepted Solution

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

0
 
LVL 3

Author Comment

by:jdthedj
ID: 1356993
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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