|
-
Jun 19th, 2000, 10:37 PM
#1
Thread Starter
Addicted Member
I want to check from one program to see if another program is running and if it is shut down that program. It's for an upgrade install. Any ideas?
-
Jun 19th, 2000, 10:52 PM
#2
Hyperactive Member
YES!
first of all, i didn't come up with this myself.
it's from kpdteam.tripod.com
this looks for a window headed Calculator, and closes it
you're gonna neeed to know what the form's caption is, though.....
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Private Sub Form_Load()
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
If winHwnd <> 0 Then
PostMessage winHwnd, WM_CLOSE, 0&, 0&
Else
MsgBox "The Calculator is not open."
End If
End Sub
-
Jun 19th, 2000, 10:58 PM
#3
_______
partial name close window
Part of title:
This code uses EnumWindows to retrieve the title of all
windows in the system, compares it with the partial name,
and stops when a match is made. Code modified from a sample
available at http://www.thescarms.com
Code taken from a Q&A On Experts Exchange Posted By: Erick37
'~~~~~~~MODULE CODE~~~~~~~~~~~~
Option Explicit
Public Const WM_CLOSE = &H10
Public Const MAX_PATH = 260
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, _
ByVal lParam As Long) As Long
Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private sAppTitle As String
Public glHwnd As Long
Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
Dim lResult As Long
Dim sWndName As String
fEnumWindowsCallBack = 1
sWndName = Space$(MAX_PATH)
lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
sWndName = Left$(sWndName, lResult)
'Search Title for our string
If (InStr(1, sWndName, sAppTitle, vbTextCompare) > 0) Then
Debug.Print sWndName
glHwnd = hwnd
fEnumWindowsCallBack = 0
End If
End Function
Public Function SearchWindows(sApp As String, hwnd As Long) As Long
sAppTitle = sApp
glHwnd = 0
Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
SearchWindows = glHwnd
End Function
'~~~~~~~FORM CODE~~~~~~~~~~~~~~
Option Explicit
Private Sub Command1_Click()
Dim sApp As String
'Find notepad with partial name
sApp = "notepa"
glHwnd = SearchWindows(sApp, Me.hwnd)
'End application if found
If glHwnd > 0 Then
PostMessage glHwnd, WM_CLOSE, 0&, 0&
End If
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jun 19th, 2000, 11:05 PM
#4
Thread Starter
Addicted Member
-
Jun 19th, 2000, 11:10 PM
#5
Lively Member
Maybe you could use this.
Detect if the application is already running.
Sub Form_Load ()
If App.PrevInstance Then
MsgBox App.EXEName & " already running!", 4096, "Warning"
End
End If
End Sub
An ass may bray a good long time before he shakes the stars down.
T.S. Elliot
-
Jun 19th, 2000, 11:35 PM
#6
If you know the caption name you could use
Code:
AppActivate "Calculator"
SendKeys "%{F4}"
If the calculator is open, this will close it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|