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!

Terminate a running application 1

Status
Not open for further replies.

pmcdaniel

Programmer
Feb 9, 2007
127
US
This is driving me crazy. I've searched the internet and found APIs that supposedly terminate an application but none work....or I can't get them to work such as TerminateProcess and Shell with a parameter to Terminate but I can't get either to work. I found one where I was expected to pass the cursor position!

Anyway, what I want to do is have an application that will terminate another running program in VB 6. Please help!

 
Try this with Adobe Reader

lcNameExe = ['AcroRd32.exe']
loCIMV2 = GetObject("winmgmts://localhost/root/cimv2")
loProcesses = loCIMV2.ExecQuery("SELECT * FROM Win32_Process WHERE Name = " + ['AcroRd32.exe'])
IF loProcesses.Count > 0
For Each objProcess in loProcesses
objProcess.Terminate(0)
NEXT
ENDIF
 
Using traditional APIs and the Window caption as a starting point; something like;

in a module;

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long
Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public 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 Sub CloseProg(WinTitle$)

Dim lret As Long
lret = FindWindow(0&, WinTitle$)
If lret Then EndTask lret


End Sub

Private Function EndTask(ByVal TargetHwnd&) As Boolean

If IsWindow(TargetHwnd) Then
If Not (GetWindowLong(TargetHwnd, GWL_STYLE) And WS_DISABLED) Then
PostMessage TargetHwnd, WM_CANCELMODE, 0, 0&
PostMessage TargetHwnd, WM_CLOSE, 0, 0&
EndTask = True
End If
End If

End Function
 
Oh! and here are the constants;

Public Const GWL_STYLE = (-16)
Public Const WS_DISABLED = &H8000000
Public Const WM_CLOSE = &H10
Public Const WM_CANCELMODE = &H1F
 
Wow!!!! Thanks much HughLerwill!

That really does kill it....But it made me think of something else.....is there a way for it to wait/check and see if the application I want to terminate is currently processing a job before ending it?

Thanks again for the absolutley great code! You didn't give me anything vaue what-so-ever and I really appreciate it.

I'll give Zografski's suggestion a try too and see what that does!
 
To expand on my recent question, I just realized the process I want to kill runs WORD which it leaves a open in Task Manager so I'd have to kill any objects of WORD it may have open.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top