Results 1 to 6 of 6

Thread: Termination a previous instance of my application...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    Gainesville, FL
    Posts
    131

    Talking

    Anyone know a clean way of terminating a previous instance of my program?

    I don't want to just detect the previous instance...(easy to do with app.previnstance). I want to kill it.

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Are you talking about your application or you want to kill other application(s)?

  3. #3
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    Me I know :
    I'm not sure at 100% the code is working cause I've modify it a little bit for U but Try to repair it ..

    like that:

    Code:
    if app.previnstance=true then
    'This command find the CAPTION of each openned program
           Call FindWindowLike(hWnds(), 0, me.caption, "ThunderForm")
    'ThunderForm is the CLASS code of all the vb applications
    'me.caption is the caption to find .. the same as the recent version
    end if
    
    
    
    
    Private Sub FindWindowLike(hWndArray() As Long, ByVal hWndStart As Long, WindowText As String, Classname As String)
    Static level As Long
    Static found As Long
    Dim hWnd As String
    
    
    '   TitleToFind = (txtTitle)
    '   ClassToFind = (txtclass)
        If level = 0 Then found = 0
        ReDim hWndArray(0 To 0)
        If hWndStart = 0 Then hWndStart = GetDesktopWindow()
        level = level + 1
        hWnd = GetWindow(hWndStart, GW_CHILD)
        Do Until hWnd = 0
            Call FindWindowLike(hWndArray(), hWnd, WindowText, Classname)
                sWindowText = Space(255)
                sClassname = Space(255)
                    r1 = GetWindowText(hWnd, sWindowText, 255)
                    r2 = GetClassName(hWnd, sClassname, 255)
                        sWindowText = LCase(Left(sWindowText, r1))
                        sClassname = LCase(Left(sClassname, r2))
                        WindowText = LCase(WindowText)
                        Classname = LCase(Classname)
                        
            
            If (sWindowText Like WindowText) Or (sWindowText = WindowText) Then
                If (sClassname Like Classname) Or (sClassname = Classname) Then ' Si trouvé
                    GoTo CloseIt ' Close the Older APP
                End If
            End If
        hWnd = GetWindow(hWnd, GW_HWNDNEXT)
        Loop
        level = level - 1
    Exit Sub
    
    CloseIt:
    	If not hWnd = me.hWnd Then
                msgbox "Previous APP version found, the older version will be closed !"
                ReDim Preserve hWndArray(0 To found)
                hWndArray(found) = hWnd
                RetVal = PostMessage(hWnd, WM_CLOSE, 0&, 0& )
    	End If
    End Sub

  4. #4
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    Ooops !
    I've forgot a little declaration..
    wait a min ..

  5. #5
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    Put all this and all will be perfect..
    and with al that, U can modify some setting later..

    Code:
    'CLOSE APP
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
       (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
       
    Public Declare Function GetDesktopWindow Lib "user32" () As Long
    
    Public Declare Function GetWindow Lib "user32" _
       (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    
    Public Declare Function GetWindowPlacement Lib "user32" _
       (ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
    
    Public Declare Function GetWindowRect Lib "user32" _
       (ByVal hWnd As Long, lpRect As RECT) As Long
    
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
       (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
       (ByVal hWnd As Long, ByVal lpClassName As String, _
        ByVal nMaxCount As Long) As Long
    
    Public Declare Function MoveWindow Lib "user32" _
       (ByVal hWnd As Long, _
        ByVal x As Long, ByVal y As Long, _
        ByVal nWidth As Long, ByVal nHeight As Long, _
        ByVal bRepaint As Long) As Long
    
    Public Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" _
       (ByVal hWnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long
    
    Public Declare Function SetForegroundWindow Lib "user32" _
       (ByVal hWnd As Long) As Long
    
    Public Declare Function SetWindowPlacement Lib "user32" _
       (ByVal hWnd As Long, _
        lpwndpl As WINDOWPLACEMENT) As Long
    
    Public Declare Function ShowWindow Lib "user32" _
       (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    
    
    'close
    
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Const WM_CLOSE = &H10

  6. #6
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    If U have any problem, ask me there:
    [email protected]
    and I'll send U a stample ..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width