Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing a second instance of an exe being started

Status
Not open for further replies.

Bryan - Gendev

Programmer
Jan 9, 2011
408
AU
I am attempting to stop that second 'click' on a shortcut....

I have found this code amongst others as a preferred method.

It works on my dev Pc but not on a clients.

I call this procedure early in main.prg with

IF IsAppRun( APPID )
QUIT
ENDIF

after using

#DEFINE APPID "App0001-99"

at the start of main.prg

<code>
***************************************************************************
* Program....: IsAppRun.prg
* Compiler...: Visual FoxPro 06.00.8492.00 for Windows
* Abstract...: Checks for a window which is created with a Unique ID by and
* ...........: in the application. Combination of Semaphore and API.
* ...........: Based on code originally posted into the Public Domain
* ...........: by Christof Lange
***************************************************************************
*!* FUNCTION IsAppRun( tcUniqueID )
PARAMETERS tcUniqueID
LOCAL llRetVal, lcUniqueID
*** MUST pass an application ID to this function!
IF EMPTY(tcUniqueID) OR VARTYPE( tcUniqueID ) # "C"
MESSAGEBOX( 'An Application Specific Character ID is mandatory' + CHR(13) ;
+ 'when calling the IsAppRun() function', 16, 'Developer Error' )
RETURN .T.
ELSE
*** Strip out any spaces
lcUniqueID = STRTRAN( tcUniqueID, " " )
ENDIF
*** First check for the existence of the Semaphore window
IF WEXIST("_Semaphore_")
RETURN .T.
ENDIF
*** Look for an occurrence of this ID as a Window Name
DECLARE INTEGER FindWindow IN Win32Api AS FindApp String, String
IF FindApp( NULL, lcUniqueID ) > 0
*** We found one! Set Return Value
llRetVal = .T.
ELSE
*** Create a new window with this ID
DEFINE WINDOW _Semaphore_ IN DESKTOP FROM 1,1 TO 2,2 TITLE lcUniqueID
ENDIF
*** Return Status Flag
RETURN llRetVal

</code>

On his PC it throws a program error dialogue with
Not a character expression.

Works when I step in debugger and run the compiled code on my PC but not his.

Anyone have any clues who has used this?

Thanks

GenDev
 
Hello.

1 - You still want to find witch line is involved in the error. Maybe you can use Try Catch approach ;

Code:
TRY

WITH THIS
 
 ***
 *** Code here  ... **
 ***
 
 a = b

 
ENDWITH

CATCH TO loExcept
 lcMess = 'Error #: ' + LTRIM(STR(loExcept.errorNo)) + CHR(13) + ;
			'Message: ' + loExcept.Message + CHR(13)+ ;
			'Line: ' + LTRIM(STR(loExcept.LineNo)) + CHR(13)+ ;
			'Code: ' + loExcept.lineContents + CHR(13)+ ;
			'Method: ' + PROGRAM(loExcept.stackLevel) + CHR(13

 MESSAGEBOX(lcMess,0+16, "Error")

FINALLY

ENDTRY

2 – You also have to verify if “Debug Info” is checked in the project before compiling.

Good luck

Nro
 
does that , old-style code :)

sys(16) gives the prog/proc being executed , lineno() the line at time of error , so the exact code might be

******************************************************
Procedure prgerr
******************************************************
Parameters Prog,Line

lcmsg = "Proc: "+ prog + " Line: "+ transform(line)+ chr(13)+" Err num: " + transform(error())+ " Details: "+ message()
=Messagebox(lcmsg ,48)

amounts to same thing , old-style code vs newer exception try/catch/exception object

 
Mutex code now working successfully - thank to everyone who helped!

GenDev
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top