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.
Printable View
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.
what do yo mean by kill? do you want to delete it from disk or just stop it from running?
just stop it runningQuote:
Originally posted by Muddy
what do yo mean by kill? do you want to delete it from disk or just stop it from running?
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.
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")
Just thought I'd mention real quick that this doesn't work in WinNT (but it does work in Win2K).
Quote:
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 :rolleyes:.
Now, in theory, this should work:
Unfortunately, it doesn't - on 95 at least. :rolleyes: If anyone knows the correct class name for IE I'd be interested to know it. This one came from MSDN article reference Q104710.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
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...Quote:
Originally posted by InvisibleDuncan
Unfortunately, it doesn't - on 95 at least. :rolleyes: 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...
Thanks Matthew and InvisibleDuncan - I already looked at http://forums.vb-world.net/showthrea...t=kill+processQuote:
Originally posted by Matthew Gates
Try this:
Call KillApp("C:\Program Files\Internet Explorer\Iexplore.exe")
[/Highlight]
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
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.Quote:
Originally posted by InvisibleDuncan
Is it actually going to be IE you need to kill, or was that just an example?
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+processesQuote:
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.
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?
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
Looking at Matthew's code, you should be able to get away with only passing the application name because of this line:
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".VB Code:
If Right$(szExename, Len(myName)) = LCase$(myName) Then
Are you sure? According to the comments above, it only works on Win2K... :confused:Quote:
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?
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?
:D :D :D
-Lou
Thanks
Something worked?
-Lou