Solved

Termination of a program in memory

Posted on 1998-02-15
7
199 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

786 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