Solved

Termination of a program in memory

Posted on 1998-02-15
7
197 Views
Last Modified: 2010-04-04
I need a way to terminate a running application (a different
one from the one I'm running) which will check if a certain
programming is running, and if so will terminate it.

In other words, I need to find a way to send the WM_CLOSE
message to a specified application in memory.
0
Comment
Question by:almiller
7 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1359259
To Close a notepad document:
SendMessage(FindWindow('notepad', nil), wm_Destroy, 0, 0);
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1359260
You'll first have to know the ClassName of the app you are trying to close. Then use FindWindow to get e handle of the main window. Then you can use SendMessage to send a WM_CLOSE message.

For example with Word 97 (the classname is OpusApp):

var h:Integer;
begin
 h:=FindWindow('OpusApp',nil);
 if h>0 then SendMessage(h,WM_DESTROY,0,0);
end;

Some know class-names are:

Calculator                        "SciCalc"
Notepad                           "Notepad"
Paintbrush (Win3x            "pbParent"
Paint (Win95)                    "MSPaintApp"
Write (Win3x)                    "MSWRITE_MENU"
WordPad (Win95)             "WordPadClass"
Word 2/6/7                        "OpusApp"
Excel 4/5/7                       "XLMAIN"
PowerPoint 4                      "PPApplicationClass"
PowerPoint 7                      "PP7FrameClass"
ToolBook (authoring            "ToolBook"
ToolBook (runtime)                "TBKMain"
Windows Help                     "MS_WINDOC"
WinHelp32 Topic                 "MS_WINDOC_SECONDARY"
File Manager                      "WFS_Frame"
Control Panel                     "CtlPanelClass"
Print Manager                     "PrintManager"
Explore (Win95)                   "ExploreWClass"
Program Manager                   "Progman"
Microsoft Exchange 4.0            "Microsoft Exchange 4.0 Viewer"
Microsoft Internet Explorer 3.0   "IEFrame"
Lotus Organizer 2.1               "TzBook"
Lotus123 5.0                      "123WParent"
Lotus Approach 96                 "ApproachWClass"
Lotus Freelance Graphics 96       "FLWApp"
Lotus ScreenCam 2.1               "ScreenCamMainWnd"
Lotus WordPro 96                  "WordProAppWndNT"

Greetings MvZ

0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1359261
You can find a class name of any active application by using the Spy program, coming with Delphi.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:almiller
ID: 1359262
Thanks for you answer. This _is_ what I was looking for, but it lacks the ClassName part which mvz commented on.

The two lines of code you wrote work on NotePad but not on anything else. I can only assume it has to do with the ClassName, so I need to know how to find the ClassName without any external programs such as Spy.
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1359263
What application you need to close?
0
 
LVL 4

Expert Comment

by:tomerlei
ID: 1359264
how can i close netscape window?
0
 
LVL 2

Accepted Solution

by:
mvz121697 earned 200 total points
ID: 1359265
When you don't know the classname you can use
the Window-name.
If you want to close netscape you can:

procedure TForm1.Button1Click(Sender: TObject);
var cWindow:string;
    h:integer;
begin
 cWindow:='Netscape';
 h:=FindWindow(nil,Pchar(cWindow));
 if h>0 then SendMessage(h,WM_CLOSE,0,0);
end;

Be sure to use the exact windowname. If a document is opened, and the document name is also in the caption, you must provide this whole new caption-name.

You can also find a classname if you know the WindowName, by using this routine:

var cCLassName: PChar;
    cName:string;
    cWindow:string;
    n,h:integer;
begin
 cWindow:='Test.txt - Notepad';           // exact caption
 h:=FindWindow(nil,Pchar(cWindow));
 if h>0 then begin
   GetMem(cCLassName,256);
   try
    n:=GetClassname(h,cCLassName,256);
    if n=0 then ShowMessage('Error')
    else begin
      ShowMessage('Classname is '+cCLassName);
    end;
   finally
    Freemem(cCLassName,256);
   end;
 end else MessageBeep(0);
end;


BTW, you can use EnumWindows function to get a list of
all openend windows.


Greetings,
MvZ
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

18 Experts available now in Live!

Get 1:1 Help Now