Link to home
Start Free TrialLog in
Avatar of jack niekerk
jack niekerkFlag for Netherlands

asked on

IN VB6 TO FORCE CLOSING A "HANGING" PROGRAM

It happens that a "VB6 program is not properly closed by user, then it will be when I popup taskmanager still in "running tasks" I have to manualy force by clicking few times in taskmanager to get rid off it.
So I wonder would there be a way to do this from VB6 from my startup VB6 program from where I shell to all the other VB6 programs
Avatar of Antagony1960
Antagony1960

If your program is not shutting down fully it is because there is a process running which isn't being unloaded by the main form's unload event. You should really put code in there to make sure any hidden forms and processes are unloaded in an orderly fashion. Of course a simple End statement will accomplish pretty much the same thing.

Failing that, this code should accomplish what you want:

Public Function EndProcess(sProcessName As String) As Boolean
Dim winProcess
    For Each winProcess In GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Process")
        'Debug.Print winProcess.Name
        If LCase(winProcess.Name) = LCase(sProcessName) Then
            winProcess.Terminate
            EndProcess = True
            Exit For
        End If
    Next
End Function

Open in new window

Oh, and I should point out that that EndProcess function will not work on programs running from the IDE, only on the executables.
Avatar of jack niekerk

ASKER

ok, what would be the syntax to use this function at the end off my program   ( private sub form_unload part)
ASKER CERTIFIED SOLUTION
Avatar of Antagony1960
Antagony1960

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks
1 qst, is it possible to find at running time the programname, myProgram1.exe  instead off fixed text?
Hmm, the function would need to know how to distinguish between your programs and any others in the process list.

Are the programs you want to unload located in the same folder, where no other programs (which you wouldn't want to close) reside? If so you could use the ExecutablePath property of the winProcess object to check whether found processes should be closed. Try the sub below. (You will need to add a program reference to the "Microsoft Scripting Runtime")

Public Sub EndMyApps()
Const sAppFolder As String = "C:\MyAppFolder"
Dim winProcess, FSO As New FileSystemObject, sExeFolder As String
    For Each winProcess In GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Process")
        sExeFolder = FSO.GetParentFolderName("" & winProcess.ExecutablePath)
        'Terminate any programs (except this one) running from the app folder'
        If sExeFolder = sAppFolder Then
            If winProcess.Name <> App.EXEName & ".exe" Then winProcess.Terminate
        End If
    Next
    Set FSO = Nothing
End Sub

Open in new window

True , allways in same drive/folder (network), so thanks again
Regards