Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to avoid another instance of the exe
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00721779
Message ID:
00721791
Views:
8
I use this code to do so
FUNCTION AppMutex
    DECLARE INTEGER CreateMutex IN WIN32API INTEGER, INTEGER, STRING @
    DECLARE INTEGER CloseHandle IN WIN32API INTEGER
    DECLARE INTEGER GetLastError IN WIN32API
    DECLARE INTEGER SetProp IN WIN32API INTEGER, STRING @, INTEGER
    DECLARE INTEGER GetProp IN WIN32API INTEGER, STRING @
    DECLARE INTEGER RemoveProp IN WIN32API INTEGER, STRING @
    DECLARE INTEGER IsIconic IN WIN32API INTEGER
    DECLARE INTEGER SetForegroundWindow IN WIN32API INTEGER
    DECLARE INTEGER GetWindow IN WIN32API INTEGER, INTEGER
    DECLARE INTEGER ShowWindow IN WIN32API INTEGER, INTEGER
    DECLARE INTEGER GetDesktopWindow IN WIN32API
    DECLARE LONG FindWindow IN WIN32API LONG, STRING
    #DEFINE SW_RESTORE 9
    #DEFINE ERROR_ALREADY_EXISTS 183
    #DEFINE GW_HWNDNEXT 2
    #DEFINE GW_CHILD 5
    LOCAL llRetVal, lcExeFlag, lnExeHwnd, lnHwnd
    lcExeFlag = STRTRAN(_screen.caption, " ", "") + CHR(0)
    lnExeHwnd = CreateMutex(0, 1, @lcExeFlag)
    IF GetLastError() = ERROR_ALREADY_EXISTS
        lnHwnd = GetWindow(GetDesktopWindow(), GW_CHILD)
        DO WHILE lnHwnd > 0
            IF GetProp(lnHwnd, @lcExeFlag) = 1
                IF IsIconic(lnHwnd) > 0
                    ShowWindow(lnHwnd, SW_RESTORE)
                ENDIF
                SetForegroundWindow(lnHwnd)
                EXIT
            ENDIF
            lnHwnd = GetWindow(lnHwnd, GW_HWNDNEXT)
        ENDDO
        CloseHandle(lnExeHwnd)
        llRetVal = .F.
    ELSE
        SetProp(FindWindow(0, _screen.caption), @lcExeFlag, 1)
        llRetVal = .T.
    ENDIF
    RETURN llRetVal
ENDFUNC
and in the main.prg you have to add this
 _SCREEN.Caption = "Main Menu"
IF NOT AppMutex()
    QUIT
ENDIF
note that you have to set the _Screee.caption before running the code
.......
DO WHILE .T.
      ME.Work()
ENDDO
Previous
Reply
Map
View

Click here to load this message in the networking platform