Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I close ?

Status
Not open for further replies.

brittoalexis

Programmer
Dec 16, 2001
32
0
0
IN
Hi,

I just want to close an application(exe file) from VB.
How is it possible?

Brits.
 
Start with adding the following API Declarations to the Declarations section of a module:

Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10

And here's the code. Just replace apptitle with the exact title of the application you want to close (i.e. "Calculator"):

Dim winHwnd As Long
Dim RetVal As Long

winHwnd = FindWindow(vbNullString, "apptitle")

If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox &quot;Error posting message.&quot;
End If
Else
MsgBox &quot;Application is not open.&quot;
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top