Controlar la Ejucucion del sistema....

Oscar
28 de Enero del 2005
El tema es el siguiente, quiero controlar mediante un archivo de recursos, que no me permita ejecutar más de una instancia de mi sistema....Si alguien tiene idea de como hacerlo se los agradesco......

sgomez
28 de Enero del 2005
Copiáte este codigo en un prg al que lo tenés que invocar desde la primer linea del programa que no querés que se ejecute mas de una vez.
abuscar es el string que busca en el titulo de las ventanas principal. Por ejemplo, si no querés que se ejecute mas de una vez la calculadora pasás como parametro el string "CALCULADORA", en este codigo, si pasás como parametro "CALCULADOR" hace lo mismo, pero si querés que la busqueda sea exacta reemplaza la linea:

AND upper(abuscar)$upper(SUBSTR(cTmp, 1, nLength))

con

AND upper(abuscar)=upper(SUBSTR(cTmp, 1, nLength))


Ahí vá el código

parameter abuscar
DECLARE Integer SetForegroundWindow IN WIN32API Integer nHwnd
DECLARE INTEGER FindWindow IN WIN32API STRING cClassName, STRING cWindName
DECLARE Integer GetWindow IN WIN32API Integer nHwnd,Integer nCmd
DECLARE Integer GetWindowText IN WIN32API Integer nhWnd, String @cString, Integer nMaxCount
DECLARE Integer GetWindowTextLength IN WIN32API Integer nWnd

#define GW_HWNDFIRST 0
#define GW_HWNDLAST 1
#define GW_HWNDNEXT 2
#define GW_HWNDPREV 3
#define GW_OWNER 4
#define GW_CHILD 5
#define GW_MAX 5


nFoxHwnd = FindWindow( 0, _SCREEN.Caption )

*** Esta será la primera ventana
nCurrWnd = GetWindow( nFoxHwnd, GW_HWNDFIRST )

*** Recorrer todas las ventanas
DO WHILE nCurrWnd # 0
*** Tamaño del título
nLength = GetWindowTextLength( nCurrWnd )
IF nLength > 0
*** Preparar un buffer para el título
cTmp = REPLICATE( CHR(0), nLength + 1 )
*** Obtener el título
=GetWindowText( nCurrWnd, @cTmp, nLength + 1 )
*** Si el handle es distinto
*** y el título igual
IF nFoxHwnd != nCurrWnd AND upper(abuscar)$upper(SUBSTR(cTmp, 1, nLength))
*** Activar la otra ventana
*** y terminar la ejecución
=SetForegroundWindow( nCurrWnd )
QUIT
ENDIF
ENDIF
*** Obtener la siguiente ventana
nCurrWnd = GetWindow( nCurrWnd, GW_HWNDNEXT )
ENDDO