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

Killing a process by window name

Status
Not open for further replies.

tjtindal

Programmer
Mar 22, 2004
66
US
I have the window's name already in a variable ready to be "killed" or rather, the equivelant of "End Task." How do I go about sending a command from VB to kill that task?

No examples I've used on the net seem to work. I can't believe this isn't just something simple, like one line to shoot to the computer that will end that running program by name.

What should I do?
 
That link did not help at all. Copying and pasting that information in Visual Basic causes it to appear in red as if it's not understanding it.
 
Then there are syntax errors. I suppose Hugh assumed you would be able to correct those on your own. If not, just post the code that you copied and pasted, and we'll see what we can do.
 
>did not help at all?

The code from my posts has just been copy/pasted from above and RE-tested and is running fine here.
 
My theory is that tjtindal copied Zografski's code (which does indeed have one or two errors)
 
It's okay, I found a much simpler code on the site that uses a shell command to kill a process. Much easier. Thanks anyway.
 
Glad you found a solution you like. Are you going to share it with us, or keep it to yourself?
 
I'll share.

This is the line of code I use in a sub when I want to end a process:

Code:
KillProc ("loftindb.exe")

This is the sub it calls:

Code:
Sub KillProc(ImageName As String, Optional Force As Boolean)
    If Force Then
        Shell "taskkill /f /im " & ImageName, vbHide
    Else
        Shell "taskkill /im " & ImageName, vbHide
    End If
End Sub
 
thread222-1370593 for reference.

It is useful to note that taskkill.exe can also kill processes by window name (as mentioned in the original question) instead of image name. See the variant below.
___
[tt]
Sub EndTask(WindowName As String, Optional Force As Boolean)
If Force Then
Shell "taskkill /f /fi ""WINDOWTITLE EQ " & WindowName & """", vbHide
Else
Shell "taskkill /fi ""WINDOWTITLE EQ " & WindowName & """", vbHide
End If
End Sub[/tt]
 
<My theory is that tjtindal copied Zografski's code
Seems likely...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top