Results 1 to 2 of 2

Thread: Advanced AppPrevInstance

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Hello!

    I use "advanced" app.previnstance through API. My code looks like this:
    Code:
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Public Const ERROR_ALREADY_EXISTS = 183&
    
    Sub Main()
        dim res as VbMsgBoxResult
        Debug.Print CreateMutex(sa, 1, App.Title)
        Debug.Print Err.LastDllError
        If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
            res= MsgBox ("The program is already running. Shall I close this and leave the running one intaced?", vbYesNo + vbExclamation,app.title)
            if res = vbYes Then End
            if res = vbNo Then CloseProgram
        End If
        frmMain.Show
    End Sub
    Sub ZapriProgram()
        Dim vLines As Variant
        Dim h As Long, a As String, j As Long, k As Long, bFound As Boolean
        h = GetWindow(GetDesktopWindow(), GW_CHILD)
        Do While h
            a = Space$(128)
            j = SendMessage(h, WM_GETTEXT, 128, ByVal a)
            If j Then
                bFound = False
                a = Left$(a, j)
                If a = "Raèuni 2.0" Then bFound = True
            End If
            h = GetWindow(h, GW_HWNDNEXT)
            If bFound = True Then Exit Do
        Loop
        SendMessage h, WM_CLOSE, 0, 0
        SendMessage h, WM_DESTROY, 0, 0
    End Sub
    It works partialy. It closes window, but then when I rerun my prog, CreateMutex() function says that the window (program) is stil opened. How can it be, 'cause I've just closed it?
    Does anyone have better suggestion?

    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Damn, I wrote the wrong code up there. Here's the right one:

    Code:
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Public Const ERROR_ALREADY_EXISTS = 183&
    
    Sub Main()
        dim res as VbMsgBoxResult
        Debug.Print CreateMutex(sa, 1, App.Title)
        Debug.Print Err.LastDllError
        If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
            res= MsgBox ("The program is already running. Shall I close this and leave the running one intaced?", vbYesNo + vbExclamation,app.title)
            if res = vbYes Then End
            if res = vbNo Then CloseProgram
        End If
        frmMain.Show
    End Sub
    
    Sub CloseProgram()
        Dim vLines As Variant
        Dim h As Long, a As String, j As Long, k As Long, bFound As Boolean
        h = GetWindow(GetDesktopWindow(), GW_CHILD)
        Do While h
            a = Space$(128)
            j = SendMessage(h, WM_GETTEXT, 128, ByVal a)
            If j Then
                bFound = False
                a = Left$(a, j)
                If a = "Raèuni 2.0" Then bFound = True
            End If
            h = GetWindow(h, GW_HWNDNEXT)
            If bFound = True Then Exit Do
        Loop
        SendMessage h, WM_CLOSE, 0, 0
        SendMessage h, WM_DESTROY, 0, 0
    End Sub
    The questions are the same... (look up).
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

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