Hello!
I use "advanced" app.previnstance through API. My code looks like this:
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?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
Does anyone have better suggestion?


Reply With Quote