DEFINE CLASS OnlyOne AS CUSTOM > hMutex = 0 > nMutexError = 0 > cFormCaption = "" > > PROCEDURE Init > > LPARAMETER pcformcaption, pcsdiname, pcmutexname > > DECLARE INTEGER CreateMutex IN Win32API; > STRING @lpMutexAttributes,; > INTEGER bInitialOwner,; > STRING @lpName > DECLARE INTEGER GetLastError IN Win32API > DECLARE INTEGER FindWindow IN Win32API; > STRING @lpClassName, STRING @lpWindowName > DECLARE INTEGER ShowWindow IN Win32API; > INTEGER hwnd, INTEGER nCmdShow > DECLARE SHORT IsIconic IN Win32API; > INTEGER hwnd > DECLARE SHORT SetForegroundWindow IN Win32API; > INTEGER hWnd > LOCAL lnInitOwner, lcname, lnargs, lcformcaption > lnargs = PARAMETERS() > DO CASE > CASE lnargs = 1 > * So that the mutex name is different > * from the caption > lcname = CHRTRAN(pcformcaption, SPACE(1), "") > lcformcaption = pcformcaption > CASE lnargs = 2 > * This is an SDI Form > * The form's caption must be different > * from the _SCREEN's for restoring the > * screen if necessary > lcname = CHRTRAN(pcsdiname, SPACE(1), "") > lcformcaption = pcsdiname > CASE lnargs = 3 > * If a mutex name is to be passed > * pcsdiname should be passed as empty > * If not and SDI form > IF NOT EMPTY(pcsdiname) > lcformcaption = pcformcaption > ELSE > lcformcaption = pcsdiname > ENDIF > lcname = pcmutexname > ENDCASE > * Simulate a BOOL = TRUE > lnInitOwner = 1 > This.cFormCaption = lcformcaption > This.hMutex = CreateMutex(0, lnInitOwner, @lcname) > This.nMutexError = GetLastError() > RETURN > ENDPROC > > PROCEDURE DisplayOtherInst > * This procedure is used to display > * the initial instance of the application > > LOCAL lcformcaption, llvisible, lnhWnd,; > llicon > lcformcaption = This.cFormCaption > lnhWnd = FindWindow(0, @lcformcaption) > IF NOT EMPTY(lnhWnd) > * Just Restore it if necessary > llicon = (IsIconic(lnhWnd) # 0) > = SetForegroundWindow(lnhWnd) > IF llicon > * It's an icon, so restore it > = ShowWindow(lnhWnd, 9) > ENDIF > ELSE > = MESSAGEBOX("Only one instance of this application allow! Please check your taskbar.", 64, "Single Instance Application") > ENDIF > RETURN > ENDFUNC > > PROCEDURE Destroy > DECLARE INTEGER WaitForSingleObject IN Win32API; > INTEGER hObject, INTEGER dwTimeOut > DECLARE SHORT ReleaseMutex IN Win32API; > INTEGER hMutex > DECLARE SHORT CloseHandle IN Win32API; > INTEGER hObject > LOCAL lnresult > IF This.hMutex # 0 > lnresult = WaitForSingleObject(This.hMutex, 50) > * 258 = STATUS_TIMEOUT > IF lnresult # 258 > lnresult = ReleaseMutex(This.hMutex) > * 1 equals Boolean TRUE > IF lnresult = 1 > lnresult = CloseHandle(This.hMutex) > ENDIF > ENDIF > ENDIF > CLEAR DLLS > ENDPROC >ENDDEFINE