Terminate Application by Knowing its .exe name

Using VB 6.0, how can i terminate an application knowing its exe name? I do have code that searches the running applications and returns true if the application is running. As the caption text in the running application changes frequently, I would like to stay away from determining its handle based on its caption (ie. FindWindow API) Function. Is there an API call available that I can use to just pass the exe name to it such as terminate "WINWORD.EXE"?? IF so which one and how? I am very new to using API calls, so any and all help is very much appreciated.

Private Sub Form_Load()
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "WinWord")
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "There was an error posting message."
End If
Else
MsgBox "The WinWord is not open."
End If

Private Sub Form_Load()
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "WinWord")
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "There was an error posting message."
End If
Else
MsgBox "The WinWord is not open."
End If

Unless I am missing something, findwindow only works if the criteria matches the caption title exactly. I need to terminate by just knowing its exe name or at the very most its class name. In the Winword example, you would have to have have typed "Microsoft Word - Document1" to have made this work (If you havent named the document after first opening word). I need a better way!!!!

Hi
>I do have code that searches the running applications and returns true if the application is running<
What do you searching for? Captions or process ID? If you use first way (like Azra and Ruchi shown), you already have window handle (this handle don't change while App running) and you need not loop through windows again, just send message WM_CLOSE to this window. BTW, you need not only close this windows, but all its parents.
If you use second way, you know processID. Then:
'---bas module code---

Option Explicit
Public glPid As Long
Public glHandle As Long
Public colHandle As New Collection

Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
Dim lParent As Long
Dim lThreadId As Long
Dim lProcessId As Long
fEnumWindowsCallBack = 1
lThreadId = GetWindowThreadProcessId(hwnd, lProcessId)

If glPid = lProcessId Then
lParent = GetParent(hwnd)
If lParent = 0 Then
colHandle.Add hwnd
End If
End If
End Function

Public Function fEnumWindows() As Boolean
Dim hwnd As Long
Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
End Function

Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Integer = 260

Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type

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

Sub close_by_exe(your_frm As Form, exe_name As String)
Dim CurrWnd As Long
Dim Length As Long
Dim TaskName As String
Dim Parent As Long
Dim exename As String
Dim SlashPos As Integer
Dim RetVal As Long
CurrWnd = GetWindow(your_frm.hwnd, GW_HWNDFIRST)

Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * MAX_PATH
End Type
'---------------------------------------

Public Function KillApp(myName As String) As Boolean

Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0

Your code worked excellent!!! It even appears that I don't need to specify the entire path to the exe to kill the application (which is a great feature). I dont have NT installed on this system, How will this work for me in the NT environment? If this doesnt work for NT, can you advise the alternative code to handle that? If you need me to open another question with points, I could do that.

Thanks to everyone else for your help, Lexien's code is what I was looking for!! The find window function is a good one for the windows you can predict the caption of, but if someone saves a word document and then opens it directly, you can't know exactly what their caption would say. Thanks again...

this code will work in Win95, Win98 and Windows 2000. I don't think it will work on NT4.

If you want to make it work on NT4, which there isn't actually much point, because Windows 2000 is now the norm, you will need to use EnumProcesses API. I don't actually have a sample for this, but someone else might.

I disagree with the fact that Windows 2000 is "the norm". Besides, you never want to make assumptions like "no one will ever use this who will be using NT". Thats just being lazy to try and work out an idea that will be functional across all platforms.

I agree with you that win200 isn't the norm.. I do need the alternative to provide customers that do have NT. There is a vast market that have that currently and will have that for years to come. As I explained before, you can never be certain that the name of the app will be in the caption. We as VB programmers know that we can make the caption read whatever we care to. I will at times have to shut down programs that I cannot predict what their captions will be. Thats why shutting them down by their exe file is a better choice for me. If you have any ideas similar to LeXien's solution but for NT, I would be perfectly happy to open a new question posting points for you for your help.

lexien mentioned EnumProcesses...here is code that will show you every process running (including those you had no idea were running) and their associated handle. i also provided buttons to kill each process so that you can see how it works. perhaps it can be applied to your app. and it is compatible with all platforms (32-bit)

'KILL ANY PROCESS
'ADD FOUR COMMAND BUTTONS TO THE FORM AND A LIST BOX
'************************************************************************
'
'MODULE CODE
'
'************************************************************************

Public Const WM_CLOSE = &H10

Public TitleCnt As Long
Public TopFndCnt As Long
Public ChildFndCnt As Long
Public TopDispCnt As Long
Public ChildDispCnt As Long

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.
There is also a companion Debug Toolbar that looks like the followin…

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.

As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…

Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…