No announcement yet.

End if Previous Instance

  • Filter
  • Time
  • Show
Clear All
new posts

  • End if Previous Instance

    VB6 has a very convenient way to end a new instance of an application if the application has an instance running.

    The VB code would be : If App.PrevInstance Then End

    I know this could be done with API calls but I am not sure what is the best way to accomplish it in the console and windows powerbasic programs. I think one method would be to detect the window of the running programs or an other way apparantly uses "mutex" API's - which I haven't really investigated enough to understand.

    Right now I need to "end" a couple of console powerbasic programs if they are launched when the programs are already running. If more than one instance of either of these programs runs it will completely corrupt a data base.

    Can anyone point me to the code I need? Or to an explanation of what I need to do to solve the multiple instances problem.


    Rudy Steury
    Rudy Steury

  • #2
    Local hWnd As Dword
    hWnd= FindWindow(App_ClassName, ByVal %Null)
    If hWnd Then 
          If IsWindowVisible(hWnd)= 0 Then ShowWindow hWnd, %SW_SHOW
          SetForegroundWindow hWnd 
          'End Current Duplicate Instance
          'Continue with this instance
    End If
    Mobile Solutions
    Sys Analyst and Development


    • #3
      There have got to be literally dozens of "previnstance" code snippets here.

      I'd use one which uses a mutex, not one of those which use "FindWindow" but that's probably just a 'panache points' thing....

      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]


      • #4

        Here's a Mutex example:
        Function PBMain
             Mutex_Name$ = "Progam_To_Check_if_Running"
               ' GHL Added Mutex code from PB Forum by ???
                    'Check to see if program already running
                       Local hmutex&, smutex$ 'just used for testing program existence
                    smutex$ = Mutex_Name$   'Program name 
                    hmutex& = CreateMutex(ByVal %Null, 0, ByVal StrPtr(smutex$))'check if running
                    If hmutex& <> %Null Then 'Program probably already running
                       If GetLastError() = %error_already_exists Then 'Constant defined in WinAPI
                          MsgBox $CrLf & " is already running",, _
                         Exit Function
                      End If 
                    End If
        'Rest of code ...
        End Function
        === http://Www.SwedesDock.Com ===
        "Education is a progressive discovery
        of our own ignorance."
        Will Durant
        === http://Www.SwedesDock.Com ===
        It's a pretty day. I hope you enjoy it.


        JWAM: (Quit Smoking):
        LDN - A Miracle Drug:


        • #5
          Here's another version based on some code once posted by Semen which will bring the already running program to the fore..
          'Function PBMain
          'Mutex check
          Dim WinName As Asciiz * %MAX_PATH
          Dim hMutex As Dword, hForWnd As Dword, hWin As Dword
          WinName = $AppTitle
          hMutex = CreateMutex(ByVal %NULL, 0, WinName)
            If hMutex = %NULL Then Exit Function
            If GetLastError = %ERROR_ALREADY_EXISTS Then    'already running!
              hWin = FindWindow (ByVal %Null, WinName)      'find handle of target window
              hForWnd = GetForegroundWindow                 'handle of current foreground window
                'Set own window to the top in Z-order with %HWND_TOP
                SetWindowpos hWin, %HWND_TOP, 0, 0, 0, 0, %SWP_NOMOVE Or %SWP_NOSIZE Or %SWP_SHOWWINDOW
                'Disable current foreground window.
                EnableWindow hForWnd, 0 
                'Redirect focus to next window in Z-order / move to foreground and activate
                SetForegroundWindow hWin                    
                'Re-enable previous foreground window
                EnableWindow hForWnd, 1
              Exit Function
            End If
          Rgds, Dave