I am developing a application using VB 6.0. My requirement is if the user log off the application using File->Exit the below action should perform

Action1 : The login status should get updated with 0 in the back end table called "Create_Login" so that next time if the user login the appl, it will allow the user else it will throw an error saying "The user already logged in"

Action2 : The process with the name "Daily Status" should get killed.

Issue description:
-> If the user Open 2 application window using 2 different user id, there will be 2 process running with same name called "Daily Status"
-> If he close anyone of the appl, then my code killing both process as both are same process name "Daily Status" and both windows are getting closed.
-> The only different is, one appl getting closed after change the status in the back end table as its closed properly but the another one is forced to close due to kill its respective process id so the back end table not getting updated. Hence the login status of second user id still remains '1' and unable to login to the application

How can i kill the appropriate process in which the user try to close the appl?

my code is below

Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim MyConn As ADODB.Connection
Dim MyRecSet1 As New ADODB.Recordset
        
    Set MyConn = New ADODB.Connection
    MyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=DIVAKAR-PC\LOCALHOST;Initial Catalog=Metrics;User Id=metrics;Password=Thinksoft"
    MyConn.Open
        
        C = MDIForm1.Caption
        SP = InStr(1, C, "(", 1) + 1
        EP = ((InStr(1, C, ")", 1)) - (InStr(1, C, "(", 1))) - 1
        EID = Mid$(C, SP, EP)
    Set MyRecSet1 = MyConn.Execute("Update Create_Login Set Login_Status = 0 Where Emp_Id = '" & EID & "'")
    
    MDIForm1.Hide
    
Dim oWMI
Dim ret
Dim sService
Dim oWMIServices
Dim oWMIService
Dim oServices
Dim oService
Dim servicename
        Set oWMI = GetObject("winmgmts:")
        Set oServices = oWMI.InstancesOf("win32_process")
        processName = "Daily_Status.exe"
        For Each oService In oServices
            servicename = LCase(Trim(CStr(oService.Name) & ""))
            If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
            ret = oService.Terminate
            End If
        Next

          Set oServices = Nothing
          Set oWMI = Nothing
End Sub

Edited 6 Years Ago by divakar.it: n/a

You are specifically calling the process to end, hence the closing of ALL within the loop.

Change your loop to exit the sub after the first process has been terminated. The only problem now is to ensure that the correct process/user gets terminated and not the other way around.

a Second solution will be to start up the process as say Daily_Status_1.exe etc. Then only kill that process.

Thanks for the response however your first option is not a answer for my question since my question is how to identify the correct process.

So i will go for a second option but can you please advise how to initiate a process name like Daily_Status_1.exe when a user open a application and how the same name will get terminated when the respective window is closed..

Can you pls explain more? It would be great if you can provide sample code for this

You can use the WMI to get some extra information about the processes. For instance, you can set a command line to each instance of your app you start, and you can use the win32_process class to see what the command line is for each instance, and then terminate the instance based on the handle you receive in that class.

You can find all teh help you need HERE with code samples.

Also have a look at THIS link for more information.:)

This question has already been answered. Start a new discussion instead.