×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Terminate a running application

Terminate a running application

Terminate a running application

(OP)
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!

RE: Terminate a running application

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

RE: Terminate a running application

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

RE: Terminate a running application

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

RE: Terminate a running application

(OP)
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!

RE: Terminate a running application

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close