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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close another exe. How?

Status
Not open for further replies.

tilotama

Programmer
Aug 24, 2001
25
MY
How can i thru a VB executable program, close another opened window which is a FLASH exe? Is that possible?

thanx


[sadeyes]
 
Use Send/PostMessage API call to send a WM_QUIT message- though it's a long time since I played with messaging...

 
I got this code form someone here and changed it a little. it works well for what I am doing. it used the from name to find the program and close it.


Option Explicit

Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

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, _
ByVal lParam As Long) As Long

Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

'Constants that are used by the API
Const WM_CLOSE = &H10
Const INFINITE = &HFFFFFFFF
Const SYNCHRONIZE = &H100000


Public Function ClosePos()

Dim hWindow As Long
Dim hThread As Long
Dim hProcess As Long
Dim lProcessId As Long
Dim lngResult As Long
Dim lngReturnValue As Long
Dim ApptoKill As String
Dim fk As String

Open "c:\AppKill.txt" For Input As #10

Line Input #10, fk
ApptoKill = fk
Close #10

hWindow = FindWindow(vbNullString, ApptoKill)
hThread = GetWindowThreadProcessId(hWindow, lProcessId)
hProcess = OpenProcess(SYNCHRONIZE, 0&, lProcessId)
lngReturnValue = PostMessage(hWindow, WM_CLOSE, 0&, 0&)
lngResult = WaitForSingleObject(hProcess, INFINITE)

'Does the handle still exist?
DoEvents
hWindow = FindWindow(vbNullString, ApptoKill)
If IsWindow(hWindow) = 1 Then
'The handle still exists. Use the TerminateProcess function
'to close all related processes to this handle. See the
'article for more information.
ClosePos

Else
'Handle does not exist.

End If
StartPos
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top