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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
BIAPROAuthor Commented:
ok, what would be the syntax to use this function at the end off my program   ( private sub form_unload part)
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Antagony1960Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.