How to Kill an Application with System Menu Using Visual Basic

ID Number: Q80186

1.00

WINDOWS

Summary:

Visual Basic can use the Windows 3.0 API SendMessage function to close

any active window that has a system menu (referred to as control box

within Visual Basic) with the Close option.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

You can use the Windows 3.0 API SendMessage function to post a message

to any window in the environment as long as the handle to the window

is known. You can use the API FindWindow function to determine the

handle associated with the window the user wants to close.

To find more information about the FindWindow function, query on the

following words:

FindWindow and Visual Basic

To create a program to close an occurrence of the Windows 3.0

Calculator program, do the following:

1. Create a form called Form1.

2. Create two command buttons called Command1 and Command2.

3. Within the Command1 Click event, add the following code:

Sub Command1_Click()

X% = Shell("Calc.exe")

End Sub

4. Within the Command2 Click event, add the following code:

Sub Command2_Click()

Const NULL = 0&

Const WM_SYSCOMMAND = &H112

Const SC_CLOSE = &HF060

lpClassName$ = "SciCalc"

lpCaption$ = "Calculator"

'* determine the handle to the Calculator window.

Handle = FindWindow(lpClassName$, lpCaption$)

'* post a message to Calc to end it's existence.

X& = SendMessage(Handle, WM_SYSCOMMAND, SC_CLOSE, NULL)

End Sub

5. In the Declarations section, declare the following two API

functions:

'* NOTE: Each declaration is on one line.

Declare Function FindWindow% Lib "user" (

ByVal lpClassName As Any,

ByVal lpCaption As Any)

Declare Function SendMessage& Lib "user" (

ByVal hwnd%,

ByVal wMsg%,

ByVal wParam%,

ByVal lParam As Long)

6. Run the program. A click on Command1 will bring up an instance of

the Calculator program. A click on Command2 will close the window.

Reference(s):

"Programming Windows: the Microsoft Guide to Writing Applications for

Windows 3," Charles Petzold, Microsoft Press, 1990

"Peter Norton's Windows 3.0 Power Programming Techniques," Peter

Norton and Paul Yao, Bantam Computer Books, 1990

"Microsoft Windows Software Development Kit: Reference Volume 1,"

version 3.0

WINSDK.HLP file shipped with Microsoft Windows Software Development

Kit version 3.0

Additional reference words: 1.00 3.00