Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How can I avoid to multiple run of VFP application on on
Message
From
26/05/2005 12:27:17
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
26/05/2005 12:15:17
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Environment versions
Visual FoxPro:
VFP 7 SP1
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01017848
Message ID:
01017863
Views:
20
>Could somebody tell me the way to avoid multiple running of one VFP application on
>one machine at the same time. I have some tip to use CreateMutex(...), but it seems
>doesn't work:
>
>
>*!* APPClass.Init
>LPARAMETERS pScreenTitle, pMutex
>#DEFINE ERROR_ALREADY_EXISTS 255
>DECLARE INTEGER CreateMutex	        IN win32api INTEGER, INTEGER, STRING @
>DECLARE INTEGER CloseHandle	        IN win32api INTEGER
>DECLARE INTEGER GetLastError        IN win32api
>DECLARE INTEGER FindWindow          IN Win32api STRING, STRING
>DECLARE INTEGER IsIconic            IN Win32api INTEGER
>DECLARE         ShowWindow          IN Win32api INTEGER, INTEGER
>DECLARE         SetForegroundWindow IN Win32api INTEGER
>
>LOCAL lMutex, nWinHandle,lnError
>lMutex = IIF(pcount()<2, pScreenTitle, pMutex)
>THIS.nExeHwnd = CreateMutex(0, 1, @lMutex)
>lnError=GetLastError()
>IF lnError = ERROR_ALREADY_EXISTS
>	CloseHandle(THIS.nExeHwnd)
>	MESSAGEBOX("ERROR"+CHR(13)+"This application is allready running."+CHR(13)+ "Shut down...", ;
>		MB_OK+MB_ICONSTOP, pScreenTitle)
>		MB_OK+MB_ICONSTOP, pScreenTitle)
>	nWinHandle = FindWindow(NULL, pScreenTitle)
>	IF nWinHandle # 0
>		IF IsIconic(nWinHandle) = 1
>			ShowWindow(nWinHandle,4)
>		ENDIF
>		SetForegroundWindow(nWinHandle)
>	ENDIF
>	RETURN .F.
>ENDIF
>
>
>Thanks for your reactions.

Petr,
ERROR_ALREADY_EXISTS sounds to be defined wrong. Here is what I use:
if AppAlreadyRunning("SomeUniqueStringForMyAPPInstance")
*...

Function AppAlreadyRunning
	Lparameters tcSemaphoreName
	Local hsem, lpszSemName
	#Define ERROR_ALREADY_EXISTS 183
	Declare Integer GetLastError In win32API
	Declare Integer CreateSemaphore In WIN32API ;
		string @ lpSemaphoreAttributes, ;
		LONG lInitialCount, ;
		LONG lMaximumCount, ;
		string @ lpName
	hsem = CreateSemaphore(0,0,1,tcSemaphoreName)
	Return (hsem # 0 And GetLastError() == ERROR_ALREADY_EXISTS)
PS:I'd never use _screen.title or a window title for that matter for semaphore,mutex name.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform