Results 1 to 19 of 19

Thread: How do I kill a program from VB if I only know its name

  1. #1

    Thread Starter
    Member Jenny W's Avatar
    Join Date
    Jul 2001
    Posts
    33

    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.

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    what do yo mean by kill? do you want to delete it from disk or just stop it from running?

  3. #3

    Thread Starter
    Member Jenny W's Avatar
    Join Date
    Jul 2001
    Posts
    33
    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

  4. #4
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    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.

  5. #5
    Matthew Gates
    Guest
    Try this:


    VB Code:
    1. Private Declare Function ProcessFirst Lib "kernel32" _
    2. Alias "Process32First" (ByVal hSnapshot As Long, uProcess As _
    3. PROCESSENTRY32) As Long
    4.  
    5. Private Declare Function ProcessNext Lib "kernel32" _
    6. Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
    7. PROCESSENTRY32) As Long
    8.  
    9. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    10. Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    11. lProcessID As Long) As Long
    12.  
    13. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    14. hObject As Long) As Long
    15.  
    16. Private Declare Function OpenProcess Lib "kernel32" (ByVal _
    17. dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    18. ByVal dwProcessId As Long) As Long
    19.  
    20. Private Declare Function TerminateProcess Lib "kernel32" (ByVal _
    21. hProcess As Long, ByVal uExitCode As Long) As Long
    22.  
    23. Private Const MAX_PATH = 260
    24.  
    25. Private Type PROCESSENTRY32
    26.     dwSize As Long
    27.     cntUsage As Long
    28.     th32ProcessID As Long
    29.     th32DefaultHeapID As Long
    30.     th32ModuleID As Long
    31.     cntThreads As Long
    32.     th32ParentProcessID As Long
    33.     pcPriClassBase As Long
    34.     dwFlags As Long
    35.     szexeFile As String * MAX_PATH
    36. End Type
    37.  
    38. Private Function KillApp(myName As String) As Boolean
    39.     Const PROCESS_ALL_ACCESS = 0
    40.     Dim uProcess As PROCESSENTRY32
    41.     Dim rProcessFound As Long
    42.     Dim hSnapshot As Long
    43.     Dim szExename As String
    44.     Dim exitCode As Long
    45.     Dim myProcess As Long
    46.     Dim AppKill As Boolean
    47.     Dim appCount As Integer
    48.     Dim i As Integer
    49.     On Local Error GoTo Finish
    50.     appCount = 0
    51.    
    52.     Const TH32CS_SNAPPROCESS As Long = 2&
    53.    
    54.     uProcess.dwSize = Len(uProcess)
    55.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    56.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    57.    
    58.     Do While rProcessFound
    59.         i = InStr(1, uProcess.szexeFile, Chr(0))
    60.         szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    61.         If Right$(szExename, Len(myName)) = LCase$(myName) Then
    62.             KillApp = True
    63.             appCount = appCount + 1
    64.             myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    65.             AppKill = TerminateProcess(myProcess, exitCode)
    66.             Call CloseHandle(myProcess)
    67.         End If
    68.         rProcessFound = ProcessNext(hSnapshot, uProcess)
    69.     Loop
    70.  
    71.     Call CloseHandle(hSnapshot)
    72. Finish:
    73. End Function
    74.  
    75.  
    76. '[b][u]Usage[/u][/b]
    77.  
    78. Call KillApp("C:\Program Files\Internet Explorer\Iexplore.exe")

  6. #6
    Tygur
    Guest
    Just thought I'd mention real quick that this doesn't work in WinNT (but it does work in Win2K).

  7. #7
    Matthew Gates
    Guest
    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 .

  8. #8
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    Now, in theory, this should work:
    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    2.     (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    4.     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    5. Private Const WM_CLOSE = &H10
    6. Private Const WM_DESTROY = &H2
    7. Private Const WM_QUIT = &H12
    8.  
    9. Private Sub CmdKill_Click()
    10.     Dim blnAlreadyOpen As Boolean
    11.     Dim lngWindowHandle As Long
    12.    
    13.     blnAlreadyOpen = (FindWindow("Internet Explorer_Frame   ", vbNullString) <> 0)
    14.     If blnAlreadyOpen Then
    15.         lngWindowHandle = FindWindow("Internet Explorer_Frame", vbNullString)
    16.        
    17.         PostMessage lngWindowHandle, WM_DESTROY, 0, 0
    18.         PostMessage lngWindowHandle, WM_QUIT, 0, 0
    19.        
    20.     End If
    21.  
    22. 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...
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  9. #9
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202
    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...
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

  10. #10

    Thread Starter
    Member Jenny W's Avatar
    Join Date
    Jul 2001
    Posts
    33
    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

  11. #11
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    Is it actually going to be IE you need to kill, or was that just an example?
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  12. #12

    Thread Starter
    Member Jenny W's Avatar
    Join Date
    Jul 2001
    Posts
    33
    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.

  13. #13
    Megatron
    Guest
    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.

  14. #14

    Thread Starter
    Member Jenny W's Avatar
    Join Date
    Jul 2001
    Posts
    33
    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.

  15. #15
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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

  16. #16
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    Looking at Matthew's code, you should be able to get away with only passing the application name because of this line:
    VB Code:
    1. 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...
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  17. #17
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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

  18. #18

    Thread Starter
    Member Jenny W's Avatar
    Join Date
    Jul 2001
    Posts
    33
    Thanks

  19. #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
  •  



Click Here to Expand Forum to Full Width