Results 1 to 17 of 17

Thread: [RESOLVED] How to programmatically kill user process from VB6?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    34

    Resolved [RESOLVED] How to programmatically kill user process from VB6?

    I have a VB6 application that is executed by a compiled .exe that installs many files into windows and integrates a folder based application into Outlook 2003.. It also has a companion Uninstaller that is also VB6 code. When I attempt to run the Uninstaller, it uninstalls the folder based application and deltes all files that were installed by the VB6 application, but I discovered it was not deleting the .exe file from its stored Windows folder location. After a lot of head scratching, I discovered by using the windows task manager that the reason the .exe file would not delete is because it is still running as a process. What is the code I can programatically add to my Uninstaller to check whether the .exe is running and if so to shut it down so the code following that location will delete the .exe file? ??
    I can't require the user to use the alt-ctrl-del Task Manager to do this....I somehow need to check for this situation and have the process killed from within the Uninstaller code.

  2. #2
    Lively Member
    Join Date
    Mar 2008
    Posts
    115

    Re: How to programmatically kill user process from VB6?

    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
    
    Const WM_CLOSE = &H10
    
    Private Sub Kill_Program()
        Dim WinWnd As Long 
    
        WinWnd = FindWindow(vbNullString, "Yourprogram.exe")
    
        If WinWnd <> 0 Then 
            PostMessage WinWnd, WM_CLOSE, 0&, 0&
        Else
              '   do nothing or
              '  MsgBox "No window of that name exists." 
        End If
    End Sub
    That should work..

    .

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to programmatically kill user process from VB6?

    Generally WM_CLOSE will work for running programs, but I have seen instances where it doesn't always work with a process.

    If Jabber's code doesn't work, then use WM_QUIT instead
    Code:
    Private Const WM_QUIT = &H12

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    34

    Re: How to programmatically kill user process from VB6?

    I tried the code with both forms of Constant, WM_CLOSE and WM_QUIT. Neither worked. I am listing the code I was using below and would appreciate your critique if there is something wrong with how I implemented your suggestion. The file names I listed as passed arguments are the actual names I see being listed by the Windows Task Manager in the Applications and Processes listings as still running. Till I remove these, my Uninstaller won't delete the folder that contains these files. Not sure if this relates to your code or not, but neither of the listed processes appear as actual windows...just as listed processes by the Task Mgr. that are still running.
    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_QUIT = &H12
    
    Private Sub Form_Load()
        Kill_Program ("BidAssistant.exe")
        Kill_Program ("EvalBidAssistant.exe")
    End Sub
    
    Private Sub Kill_Program(ProgramName As String)
        Dim WinWnd As Long
    
        WinWnd = FindWindow(vbNullString, ProgramName)
    
        If WinWnd <> 0 Then
            PostMessage WinWnd, WM_QUIT, 0&, 0&
        Else
              '  do nothing or
              MsgBox "No window of name " & ProgramName & " exists."
        End If
    End Sub
    Last edited by jellis00; May 9th, 2008 at 04:42 PM.

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to programmatically kill user process from VB6?

    FindWindow uses the exact titlebar text of the program as the second parameter, i.e. Kill_Program ("Untitled - Notepad")

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    34

    Re: How to programmatically kill user process from VB6?

    Thanks for that input, Edgemeal! It confuses me a little....all I have is the name that lists in the Applications and Processes that are running. How would those names relate to their respective titlebar text??

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to programmatically kill user process from VB6?

    There are ways to terminate a process using exe file name, but surely not the best way to close a program!

    You can enumerate all running programs and get their titlebar text, off hand I don't know a link to an example but I'm sure it's been posted here before.

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    34

    Thumbs up Re: How to programmatically kill user process from VB6?

    Success!! The below listed code works. The secret was to use exactly the titlebar text of the process(es) to be removed and not the .exe name as was indicated in the above code.
    This has been invaluable to me and will probably be invaluable to a lot of future users of this forum. Thanks so much for all your help.

    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 Sub Form_Load()
        Kill_Program ("BidAssistant")
        Kill_Program ("EvalBidAssistant")
    End Sub
    
    Private Sub Kill_Program(ProgramName As String)
        Dim WinWnd As Long
    Stop
        WinWnd = FindWindow(vbNullString, ProgramName)
    
        If WinWnd <> 0 Then
            PostMessage WinWnd, WM_CLOSE, 0&, 0&
        Else
              '  do nothing or
              MsgBox "No window of name " & ProgramName & " exists."
        End If
    End Sub

  9. #9
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to programmatically kill user process from VB6?

    Great!
    Do us a favor and mark this thread as Resolved, see the "Thread Tools" pulldown menu.

  10. #10
    New Member
    Join Date
    Jun 2014
    Posts
    2

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    This code looks like something that could work for me too, but where would I put the code in the program, in a class, module, etc.?

  11. #11
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    Sometimes developers seem to overthink stuff, think simple.

    Code:
    Shell ("taskkill /IM notepad.exe")
    replace notepad.exe with any filename.exe process you wish to shut.

    can also be done by PID. google taskkill.

  12. #12
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    Quote Originally Posted by stum View Post
    Sometimes developers seem to overthink stuff, think simple.
    That is because that code is relying on a command that is part of Windows (third-party) program rather than using code with the host application. Also, if you are interested in Software Development I would suggest using a method such as that posted by jellis00 because it shows the user what is happening in-order to kill a particular process.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  13. #13
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    Quote Originally Posted by Nightwalker83 View Post
    That is because that code is relying on a command that is part of Windows (third-party) program rather than using code with the host application.
    The way i see it windows is a fundemental dependency for almost any VB6 program.
    and Sendmessage API also depends on User32.dll libraries, which also requires windows.

    and .. jellis00's code (good code by the way), won't work on process that hold no window.
    his FindWindow()'s function just won't return any handle of a windowless process and WinWnd will remain zero.
    Last edited by stum; Jul 1st, 2014 at 03:21 AM.

  14. #14
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    Quote Originally Posted by stum View Post
    and .. jellis00's code (good code by the way), won't work on process that hold no window.
    his FindWindow()'s function just won't return any handle of a windowless process and WinWnd will remain zero.
    What I was suggesting is that the programmer can see what the code is doing.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  15. #15
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,165

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    Quote Originally Posted by stum View Post
    Sometimes developers seem to overthink stuff, think simple.

    Code:
    Shell ("taskkill /IM notepad.exe")
    replace notepad.exe with any filename.exe process you wish to shut.

    can also be done by PID. google taskkill.
    I have been using this with success, except that if I follow it by a Robocopy command I get an error process still running...
    Thanks all !

  16. #16
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,165

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    Quote Originally Posted by stum View Post
    Sometimes developers seem to overthink stuff, think simple.

    Code:
    Shell ("taskkill /IM notepad.exe")
    replace notepad.exe with any filename.exe process you wish to shut.

    can also be done by PID. google taskkill.
    I have been using this with success, except that if I follow it by a Robocopy command I get an error process still running... This is with Outlook, so a pretty long pst file in case that is relevant
    Thanks all !

  17. #17
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    201

    Re: [RESOLVED] How to programmatically kill user process from VB6?

    Quote Originally Posted by el84 View Post
    I have been using this with success, except that if I follow it by a Robocopy command I get an error process still running... This is with Outlook, so a pretty long pst file in case that is relevant
    try to modify this sample:
    KillProcess.zip
    The Refresh button fill the listbox with the processes machin with mask in the textbox, and the button Kill Selected Process kill the process selected in the list box

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