-
May 8th, 2008, 03:11 PM
#1
Thread Starter
Member
[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.
-
May 8th, 2008, 10:27 PM
#2
Lively Member
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..
.
-
May 9th, 2008, 07:09 AM
#3
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
-
May 9th, 2008, 04:32 PM
#4
Thread Starter
Member
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.
-
May 9th, 2008, 04:57 PM
#5
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")
-
May 9th, 2008, 05:14 PM
#6
Thread Starter
Member
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??
-
May 9th, 2008, 05:31 PM
#7
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.
-
May 9th, 2008, 06:18 PM
#8
Thread Starter
Member
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
-
May 9th, 2008, 06:21 PM
#9
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.
-
Jun 30th, 2014, 11:29 AM
#10
New Member
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.?
-
Jul 1st, 2014, 01:44 AM
#11
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.
-
Jul 1st, 2014, 02:41 AM
#12
Re: [RESOLVED] How to programmatically kill user process from VB6?
Originally Posted by stum
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
-
Jul 1st, 2014, 03:15 AM
#13
Re: [RESOLVED] How to programmatically kill user process from VB6?
Originally Posted by Nightwalker83
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.
-
Jul 1st, 2014, 05:20 AM
#14
Re: [RESOLVED] How to programmatically kill user process from VB6?
Originally Posted by stum
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
-
Jan 24th, 2020, 04:47 AM
#15
Frenzied Member
Re: [RESOLVED] How to programmatically kill user process from VB6?
Originally Posted by stum
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 !
-
Jan 24th, 2020, 04:48 AM
#16
Frenzied Member
Re: [RESOLVED] How to programmatically kill user process from VB6?
Originally Posted by stum
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 !
-
Jan 25th, 2020, 04:20 AM
#17
Addicted Member
Re: [RESOLVED] How to programmatically kill user process from VB6?
Originally Posted by el84
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|