|
-
Aug 1st, 2001, 05:54 AM
#1
Thread Starter
Member
How do I kill a program from VB if I only know its name
How do I kill a program from VB if I only know its name e.g. iexplorer.exe not the name in the task bar or the whole path.
-
Aug 1st, 2001, 05:57 AM
#2
PowerPoster
what do yo mean by kill? do you want to delete it from disk or just stop it from running?
-
Aug 1st, 2001, 06:03 AM
#3
Thread Starter
Member
Originally posted by Muddy
what do yo mean by kill? do you want to delete it from disk or just stop it from running?
just stop it running
-
Aug 1st, 2001, 06:18 AM
#4
PowerPoster
hmmm ... can't help you there. If it is even possible, my bet is that it is some kind of API. Try crossposting this to the API forum. At least youll have your question up in two places.
Sorry I couldnt help.
-
Aug 1st, 2001, 08:42 PM
#5
Try this:
VB Code:
Private Declare Function ProcessFirst Lib "kernel32" _
Alias "Process32First" (ByVal hSnapshot As Long, uProcess As _
PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" _
Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
lProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject 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 TerminateProcess Lib "kernel32" (ByVal _
hProcess As Long, ByVal uExitCode As Long) As Long
Private Const MAX_PATH = 260
Private 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
Private 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
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Function
'[b][u]Usage[/u][/b]
Call KillApp("C:\Program Files\Internet Explorer\Iexplore.exe")
-
Aug 1st, 2001, 10:59 PM
#6
Just thought I'd mention real quick that this doesn't work in WinNT (but it does work in Win2K).
-
Aug 2nd, 2001, 12:33 AM
#7
Originally posted by Tygur
Just thought I'd mention real quick that this doesn't work in WinNT (but it does work in Win2K).
Forgot to include those minor details .
-
Aug 2nd, 2001, 03:05 AM
#8
Fanatic Member
Now, in theory, this should work:
VB Code:
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, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Const WM_DESTROY = &H2
Private Const WM_QUIT = &H12
Private Sub CmdKill_Click()
Dim blnAlreadyOpen As Boolean
Dim lngWindowHandle As Long
blnAlreadyOpen = (FindWindow("Internet Explorer_Frame ", vbNullString) <> 0)
If blnAlreadyOpen Then
lngWindowHandle = FindWindow("Internet Explorer_Frame", vbNullString)
PostMessage lngWindowHandle, WM_DESTROY, 0, 0
PostMessage lngWindowHandle, WM_QUIT, 0, 0
End If
End Sub
Unfortunately, it doesn't - on 95 at least. If anyone knows the correct class name for IE I'd be interested to know it. This one came from MSDN article reference Q104710.
It does work for most other applications, though...
-
Aug 2nd, 2001, 04:01 AM
#9
Addicted Member
Originally posted by InvisibleDuncan
Unfortunately, it doesn't - on 95 at least.  If anyone knows the correct class name for IE I'd be interested to know it. This one came from MSDN article reference Q104710.
It does work for most other applications, though...
I think we had this discussion on another thread - IE does not have a single class - if it is showing a flash movie the classname is different to that when showing normal HTML, and if the browser is behaving like Windows Explorer listing a dir the classname is again different...
-
Aug 2nd, 2001, 10:25 AM
#10
Thread Starter
Member
Originally posted by Matthew Gates
Try this:
Call KillApp("C:\Program Files\Internet Explorer\Iexplore.exe")
[/Highlight]
Thanks Matthew and InvisibleDuncan - I already looked at http://forums.vb-world.net/showthrea...t=kill+process
but I only have the short process name of the process and not the whole path ( see http://forums.vb-world.net/showthrea...threadid=91744 ) or the name in the task bar
-
Aug 2nd, 2001, 10:29 AM
#11
Fanatic Member
Is it actually going to be IE you need to kill, or was that just an example?
-
Aug 2nd, 2001, 11:39 AM
#12
Thread Starter
Member
Originally posted by InvisibleDuncan
Is it actually going to be IE you need to kill, or was that just an example?
Thats just an example - it needs to look at a list of currently running tasks and kill anything that it doesn't recognise.
-
Aug 2nd, 2001, 01:26 PM
#13
What method are you using to retrieve the filename? In most cases, you should also be able to get the path of the file with it.
-
Aug 3rd, 2001, 06:04 AM
#14
Thread Starter
Member
Originally posted by Megatron
What method are you using to retrieve the filename? In most cases, you should also be able to get the path of the file with it.
I got it from this thread http://forums.vb-world.net/showthrea...ning+processes
this just gives the short name notepad.exe etc
Looking at it again though I find that I can't get KillApp to work even with the full path now - maybe this is because I'm running under windows 2K? Is there another way of killing Apps under windows 2k if I just have the short name?
Last edited by Jenny W; Aug 3rd, 2001 at 06:15 AM.
-
Aug 3rd, 2001, 06:51 AM
#15
Registered User
Also you can try this method out which allows you to close an application either by caption or by class name:
http://www.vbforums.com/showthread.p...ight=terminate
-
Aug 3rd, 2001, 06:59 AM
#16
Fanatic Member
Looking at Matthew's code, you should be able to get away with only passing the application name because of this line:
VB Code:
If Right$(szExename, Len(myName)) = LCase$(myName) Then
Since it takes the right-most characters, it shouldn't make any difference whether you look for "C:\Program Files\Internet Explorer\Iexplore.exe" or just "Iexplore.exe".
Originally posted by Jenny W
Looking at it again though I find that I can't get KillApp to work even with the full path now - maybe this is because I'm running under windows 2K?
Are you sure? According to the comments above, it only works on Win2K...
-
Aug 3rd, 2001, 08:21 AM
#17
Hi.
Heres a suggestion. With your method, your not only
getting the exe name, your also getting the processID and the Parent process ID. {check the structure of uProcess and you'll see it as th32ProcessID and th32ParentProcessID.}
Now, the following code returns the handle of processID's.
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
just pass the processID to the function and it should return the hWnd. {I just found this code on the API-Guide Network, never tried it, so I don't know for sure if it works, but it should.}
So, if you retrieve the hWnd of , oh, I believe the parentProcessID,
then you could propably use invisibleduncans method
PostMessage lngWindowHandle, WM_DESTROY, 0, 0
PostMessage lngWindowHandle, WM_QUIT, 0, 0
to kill it.
Now my question, what is the reason behind this mass slaughter of innocent running apps?
-Lou
-
Aug 7th, 2001, 02:30 PM
#18
Thread Starter
Member
-
Aug 7th, 2001, 02:49 PM
#19
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|