How to terminate the previous instance of my application
Hi all,
I have the following code to detect if my application is already running; if so, how can i terminate the previous instance of my application.
VB Code:
If App.PrevInstance Then
'App is already running
'Here I want to unload the previous instance
End If
Thanks in advance.
Re: How to terminate the previous instance of my application
you have to use Kill Process for this, to find out the process you can use either FindWindow or something like that...just search for Kill process, you will hit a lot of examples and codes...
Re: How to terminate the previous instance of my application
ganeshmoorthy, The kill process actually deletes the file off the hard disk.
Try this, Not sure how can you use it:
VB Code:
Private Sub Command1_Click()
ShutWindow "Untitled - Notepad"
End Sub
In A Module:
VB Code:
Option Explicit
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) 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
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As Any, _
ByVal lpWindowName As String) As Long
Public Const GWL_STYLE = -16
Public Const WS_DISABLED = &H8000000
Public Const WM_CLOSE = &H10
Public Function ShutWindow(Window_Title As String)
Dim X As Long
Dim WindowHwnd As Long
WindowHwnd = FindWindow(0&, Window_Title)
If WindowHwnd = 0 Then Exit Function
If IsWindow(WindowHwnd) = False Then
Else
If Not (GetWindowLong(WindowHwnd, GWL_STYLE) And WS_DISABLED) Then
X = PostMessage(WindowHwnd, WM_CLOSE, 0, 0&)
End If
End If
End Function