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
BIAPROAsked:
Who is Participating?
 
Antagony1960Connect With a Mentor Commented:
I take it you want to close all your managed programs when the startup application gets closed--they are separate programs (executables) aren't they? If so, you call the function like this:

EndProcess "myProgram1.exe"

Alternatively, as it is a boolean function, you could use its return value to log whether it has succeeded or failed, like this:

If EndProcess("myProgram1.exe") Then
    'Unloaded successfully'
Else
    'Failed to unload, may already have been closed'
End If
0
 
Antagony1960Commented:
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

0
 
Antagony1960Commented:
Oh, and I should point out that that EndProcess function will not work on programs running from the IDE, only on the executables.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
BIAPROAuthor Commented:
ok, what would be the syntax to use this function at the end off my program   ( private sub form_unload part)
0
 
BIAPROAuthor Commented:
Thanks
0
 
BIAPROAuthor Commented:
1 qst, is it possible to find at running time the programname, myProgram1.exe  instead off fixed text?
0
 
Antagony1960Commented:
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

0
 
BIAPROAuthor Commented:
True , allways in same drive/folder (network), so thanks again
Regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.