Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

App.PreviousInstance

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Aleksandr Dobrev
    replied
    Originally posted by Christian Blackburn:
    For whatever reason I couldn't
    get the error already exists to be returned from GetLastError.
    To be more accurate use prefix "Global\" or "Local\"
    in the name of mutex to create the syn-object in the global or
    session name space.

    Leave a comment:


  • Christian Blackburn
    replied
    Hi Gang,

    That mutex example didn't work for me.
    I imagine the FindByClassName method does however, but that's not
    as safe as creating a mutex. For whatever reason I couldn't
    get the error already exists to be returned from GetLastError.
    For those of you who have the same problem here's my code. Which
    did work for <U>me</U>:
    Code:
        lngMutex = OpenMutex(%MUTEX_ALL_ACCESS, 0, "TimeSynch")
     
        IF lngMutex = 0 THEN
     
            lngMutex = CreateMutex(BYVAL %null, 1, "TimeSynch")
     
        ELSE
            MSGBOX "Another copy of TimeSynch was already running!", %MB_IConInformation, "Time Synch"
     
            ReleaseMutex(lngMutex)
            CloseHandle(lngMutex)
     
            EXIT FUNCTION
        END IF

    Leave a comment:


  • Scott Turchin
    replied
    That code looks familiar
    Code:
     
    Function App_PrevInstance(lpclassName As Asciiz) Export As Long
    Local hMutex As Long
    hMutex = CreateMutex(ByVal %Null, 0, lpclassName)
    If hMutex = 0 Then Exit Function ' Error in Mutex
    If GetLastError = %ERROR_ALREADY_EXISTS Or FindWindow(lpClassName, "") > 0 Then Function = %TRUE
    CloseHandle hMutex
    End Function
    See date of original post.

    Leave a comment:


  • Gregery D Engle
    replied
    Code:
    #Compile Exe
    #Include "win32api.inc"
    
    Global hMutex As Long
    
    Function App_PrevInstance(lpclassName As Asciiz) Export As Long
         Local hMutex             As Long
         
         hMutex = CreateMutex(ByVal %Null, 0, lpclassName)
         If hMutex = 0 Then Exit Function ' Error in Mutex
         If GetLastError = %ERROR_ALREADY_EXISTS Or FindWindow(lpClassName, "") > 0 Then Function = %TRUE
         CloseHandle hMutex
    End Function
    
    
    Function PbMain() As Long
         Local lpClassName        As Asciiz * 255
         lpClassName = "MyProgram1234"
         
         If App_PrevInstance(lpClassName) Then
            stdout "Only one instance of this can run!"
            waitkey$
            Exit Function
         End If
         
         'Continue on if a mutex does not exist
         hMutex = CreateMutex(ByVal %Null, 0, lpclassName)   
         
         stdout "Program Running, press key to end"
         waitkey$
         CloseHandle hMutex
    End Function
    ------------------
    -Greg

    [This message has been edited by Gregery D Engle (edited May 30, 2002).]

    Leave a comment:


  • Wayne Diamond
    replied
    Code:
    #COMPILE EXE
    #INCLUDE "win32api.inc"
     
    GLOBAL hMutex AS LONG
     
    FUNCTION App_PrevInstance(lpclassName AS ASCIIZ) EXPORT AS LONG
    LOCAL hMutex AS LONG
    hMutex = CreateMutex(BYVAL %Null, 0, lpclassName)
    IF hMutex = 0 THEN EXIT FUNCTION ' Error in Mutex
    IF GetLastError = %ERROR_ALREADY_EXISTS OR FindWindow(lpClassName, "") > 0 THEN FUNCTION = %TRUE
    CloseHandle hMutex
    END FUNCTION
     
    FUNCTION PBMAIN() AS LONG
    LOCAL lpClassName AS ASCIIZ * 255
    lpclassName = "MyMutexName"
    IF App_PrevInstance(lpClassName) THEN
    STDOUT "Only one instance may be run"
    EXIT FUNCTION
    END IF
    'Continue on if a mutex does not exist
    hMutex = CreateMutex(BYVAL %Null, 0, lpclassName)
    'Your program goes here
    STDOUT "Program running and mutex is set. Press any key to end ..."
    WAITKEY$
    'Before closing application:
    CloseHandle hMutex
    END FUNCTION
    ------------------
    The PowerBASIC Crypto Archives

    Leave a comment:


  • Steve Bouffe
    replied
    I've tried to convert the code for PBCC without luck.

    Can anyone help?

    ------------------

    Leave a comment:


  • Scott Turchin
    started a topic App.PreviousInstance

    App.PreviousInstance

    I just saw someone post the Mutex code, now I can't find that post again, but here it is in function format for your app to detect if another instance is running..And I have tested this with apps that run minimized in the system tray, works great!!


    Global hMutex As Long

    in Winmain
    Local lpClassName As Asciiz * 255

    If App_PrevInstance(lpClassName) Then
    MsgBox "Only one instance of " + Mine + " may run", %MB_ICONSTOP,"Warning!"
    Exit Function
    End If
    'Continue on if a mutex does not exist
    hMutex = CreateMutex(ByVal %Null, 0, lpclassName)


    Your program goes here


    'Before closing application:
    CloseHandle hMutex


    And the function itself:
    '------------------------------------------------------------------------------------------
    Function App_PrevInstance(lpclassName As Asciiz) Export As Long
    Local hMutex As Long
    hMutex = CreateMutex(ByVal %Null, 0, lpclassName)
    If hMutex = 0 Then Exit Function ' Error in Mutex
    If GetLastError = %ERROR_ALREADY_EXISTS Or FindWindow(lpClassName, "") > 0 Then Function = %TRUE
    CloseHandle hMutex
    End Function
    '------------------------------------------------------------------------------------------
Working...
X