Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Preventing a second instance.
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Versions des environnements
Visual FoxPro:
VFP 7 SP1
OS:
Windows 2000 SP4
Network:
Windows 2000 Server
Database:
Visual FoxPro
Divers
Thread ID:
01001365
Message ID:
01001372
Vues:
32
IF !CheckWin('My Application Window Title') 
	oSplashScreen.Hide()
	RELEASE oSplashScreen
	CLOSE ALL
	MESSAGEBOX('My application is already running.'+CHR(13),0+48+4096,'Application Running')
	RETURN .F.
ENDIF

FUNCTION CheckWin
LPARAMETERS tcAppName
*** This function uses the creation of a named MUTEX to determine whether
*** or not the application is already running. This function should be called
*** near the top of the application's main program to create a named object 
*** that can be checked every time the application is started.  If the named 
*** object exists, the function will try to activate the FoxPro running application.
LOCAL lcMsg, lnAttributes, lnOwner, lcName, lnMutexHandle, lnhWnd, lnRelationship, lnStyle, llRetVal
LOCAL lcAppName
LOCAL lcAsserts

lcMsg = ''
lcAsserts = SET('ASSERTS')

*SET ASSERTS ON
IF EMPTY( NVL( tcAppName, '' ) )
	RETURN .T.
  TEXT TO lcMsg NOSHOW 
    Error checking to see if app is already running.
  ENDTEXT
  ASSERT .F. MESSAGE lcMsg
  SET ASSERTS &lcAsserts
  RETURN 
ENDIF

lcAppName = UPPER( ALLTRIM( tcAppName ) ) + CHR( 0 )

*** Declare just API functions and constants
#DEFINE SW_MAXIMIZE              3
#DEFINE ERROR_ALREADY_EXISTS   183
#DEFINE GW_HWNDNEXT              2
#DEFINE GW_CHILD                 5

DECLARE INTEGER CreateMutex IN WIN32API INTEGER lnAttributes, INTEGER lnOwner, STRING @lcName
DECLARE INTEGER GetProp IN WIN32API INTEGER lnhWnd, STRING @lcName
DECLARE INTEGER SetProp IN WIN32API INTEGER lnhWnd, STRING @lcNam, INTEGER lnValue
DECLARE INTEGER CloseHandle IN WIN32API INTEGER lnMutexHandle
DECLARE INTEGER GetLastError IN WIN32API 
DECLARE INTEGER GetWindow IN USER32 INTEGER lnhWnd, INTEGER lnRelationship
DECLARE INTEGER GetDesktopWindow IN WIN32API 
DECLARE BringWindowToTop IN Win32APi INTEGER lnhWnd
DECLARE ShowWindow IN WIN32API INTEGER lnhWnd, INTEGER lnStyle

*** Try and create a new MUTEX with the name of the passed application
lnMutexHandle = CreateMutex( 0, 1, @lcAppName )

*** If the named MUTEX creation fails because it exists already, try to display
*** the existing application 
IF GetLastError() = ERROR_ALREADY_EXISTS

  *** Get the hWnd of the first top level window on the Windows Desktop.
  lnhWnd = GetWindow( GetDesktopWindow(), GW_CHILD )

  *** Loop through the windows. 
  DO WHILE lnhWnd > 0

     *** Is this the one that we are looking for?
     *** Look for the property we added the first time
     *** we launched the application
     IF GetProp( lnhWnd, @lcAppName ) = 1
       *** Activate the app and exit stage left
       BringWindowToTop( lnhWnd )
       ShowWindow( lnhWnd, SW_MAXIMIZE )
       EXIT
     ENDIF
     lnhWnd = GetWindow( lnhWnd, GW_HWNDNEXT )
  ENDDO

  *** Close the 'failed to open' MUTEX handle
  CloseHandle( lnMutexHandle )
ELSE
  *** Add a property to the FoxPro App so we can identify it again
  SetProp( _vfp.Hwnd, @lcAppName, 1)
  llRetVal = .T.
ENDIF

SET ASSERTS &lcAsserts

CLEAR DLLS CreateMutex
CLEAR DLLS GetProp
CLEAR DLLS SetProp
CLEAR DLLS CloseHandle
CLEAR DLLS GetLastError
CLEAR DLLS GetWindow
CLEAR DLLS GetDesktopWindow
CLEAR DLLS BringWindowToTop
CLEAR DLLS ShowWindow

RETURN llRetVal
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform