Microsoft KB Archive/153463

= How To Close Applications from Visual Basic =

Article ID: 153463

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Enterprise Edition

-



This article was previously published under Q153463



SUMMARY
Periodically it may be necessary for a programmer to close down another application from Visual Basic code. For example, your application might conflict with some other known utility, so you need to close the conflicting utility so your application can work correctly. The step-by- step example in this article shows how to do it using the Windows API.



Step-by-Step Example
 Start a new Visual Basic project. Form1 is created by default. Add a command button (Command1) to Form1.  Add the following code to the General Declarations section of Form1: Option Explicit

Private Declare Function FindWindow Lib "User" (ByVal lpClassName _     As Any, ByVal lpWindowName As Any) As Integer Private Declare Function PostMessage Lib "User" (ByVal hWnd _     As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _      lParam As Any) As Integer

Private Const WM_QUIT = &H12

Private Sub Command1_Click Dim sTitle As String Dim iHwnd As Integer Dim ihTask As Integer Dim iReturn As Integer sTitle = "Notepad - (Untitled)" iHwnd = FindWindow(0&, sTitle) iReturn = PostMessage(iHwnd, WM_QUIT, 0, 0&) MsgBox "Notepad has been Closed Down" End Sub  Start Notepad by double-clicking the Notepad Icon. Activate Visual Basic, and press F5 to start the project. Click the command button and the instance of Notepad you started should automatically close down. The message box will confirm this.

NOTE: For Visual Basic for Windows, 32-bit only, version 4.0 and Visual Basic for Windows version 5.0, you need to use the following code:

 Start a new Visual Basic project. Form1 is created by default.</li> Add a command button (Command1) to Form1.</li>  Add the following code to the General Declarations section of Form1: Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal    lpClassName As Any, ByVal lpWindowName As Any) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam    As Long) As Long Private Const WM_QUIT = &H12 Private Sub Command1_Click Dim sTitle As String Dim iHwnd As Long Dim ihTask As Long Dim iReturn As Long sTitle = "Untitled - Notepad" iHwnd = FindWindow(0&, sTitle) iReturn = PostMessage(iHwnd, WM_QUIT, 0&, 0&) MsgBox "Notepad has been Closed Down" End Sub </li> Start Notepad by double-clicking the Notepad Icon.</li> Activate Visual Basic, and press the F5 key to start the project. the command button and the instance of Notepad you started should automatically close down. The message box will confirm this.</li></ol>

Keywords: kbhowto KB153463

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.