Navigation:  Reference > Functions By Category > Misc. >

OT4XB_SINGLE_INSTANCE()

Previous pageReturn to chapter overviewNext page

OT4XB_SINGLE_INSTANCE(cUniqueId,oAppHandler,aParams) -> lAlreadyRunning

 

 

Parameters:

<cUniqueId>

String that identify the application, can be any string

but the ideal is to use a UUID

<oAppHandler>

Any object that accept the method ::OnNewInstance( aParameters )

If oAppHandler is a class object, the method ::OnNewInstance() must be also

a class method

oAppHandler:OnNewInstance() will receive 1 array with the parameters passed

to 2nd and successive instances.

<aParams>

Array of parameters that new instances will notify to the running instance

 

You must run OT4XB_SINGLE_INSTANCE() early in Programm start and if it
return .T. you must quit the application as soon as possible

 

Returns:

.T. -> another instance is running in the same desktop

.F. -> this is the first instance

 

Example !

 

Bug fixed in ot4xb build 1.5.3.186 (internal):

in previous builds OT4XB_SINGLE_INSTANCE() will produce a runtime error if called before main()

( appsys,errorsys) , now it's safe to use before main.

 

The workaround for use ot4xb_Single_Instance() in appsys() with ot4xb previous to build1.5.3.186:

put this line before call ot4xb_Single_Instance()

 

@user32:RegisterClassExA( {48,0,nGetProcAddress("user32","DefWindowProcA"),;

                          0,0,AppInstance(),0,0,0,0,;

                          _xgrab("_OT4XB_GENERIC_WINDOW_"),0,})

 

 

This function was tested with Win98, XP, Vista and Win2008 Server x64

 

how it works ?

ot4xb_Single_Instance() search for a window of the class "_OT4XB_GENERIC_WINDOW_"

with the caption <cUniqueId>. If found it sends a WM_COPYDATA using a signature to validate the app

and the param info.

 

The first instance of "_OT4XB_GENERIC_WINDOW_"/<cUniqueId> not found, create a message only window

in the GUI thread to attend requests for new instances.

When a WM_COPYDATA is received the signature is checked and if contain a valid NewInstance transaction

will call to the method OnNewInstance() of the provided apphandler object passing the parameters received

from the new instance.

 

The efficiency of this function is determined from the param cUniqueId.

I recommend to use a UUID for every application. There are several free tools to

generate UUIDs and also some text editors have built in functions to paste a new UUID.

 

With Xbase++ 1.90.35x (SL1) there will come a Xbase++ build in function:

 

cUUID := UuidToChar( UuidCreate() ) 

 

If  you don't have any UUID generator you can build yourself in a few PRG lines

 

#include "ot4xb.ch"

proc main(cFile)

   DEFAULT cFile := "MyAppUuid.ch"

   lMemoWrite( cFile, "#define MYAPP_UUID " + UuidCreateStr() + CRLF)

return