SetMultiple Not working..

SetMultiple Not working..

Postby fraxzi » Mon Oct 18, 2010 6:49 am

Dear All

When I set to Setmultiple( .F. )... the app can still be open/execute. How to avoid running same program at the same time?


I'm using FWH 10.6


Kind Regards,
Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: SetMultiple Not working..

Postby Enrico Maria Giordano » Mon Oct 18, 2010 7:23 am

Use this:

Code: Select all  Expand view
IF ISEXERUNNING( CFILENOEXT( HB_ARGV( 0 ) ) )
    SHOWWINDOW( FINDWINDOW( 0, cMainWindowCaption ), 9 )
    SETFOREGROUNDWINDOW( FINDWINDOW( 0, cMainWindowCaption ) )
    RETURN NIL
ENDIF


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8711
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: SetMultiple Not working..

Postby fraxzi » Mon Oct 18, 2010 3:48 pm

Dear EMG,

It works.. but if you copy the .exe and run the copied .exe it will run again..
Is there a way to avoid running the copied .exe when the original .exe is currently running?



Kind regards,
Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: SetMultiple Not working..

Postby Enrico Maria Giordano » Mon Oct 18, 2010 4:09 pm

Try (not tested):

Code: Select all  Expand view
IF FINDWINDOW( 0, cMainWindowCaption ) > 0
    SHOWWINDOW( FINDWINDOW( 0, cMainWindowCaption ), 9 )
    SETFOREGROUNDWINDOW( FINDWINDOW( 0, cMainWindowCaption ) )
    RETURN NIL
ENDIF


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8711
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: SetMultiple Not working..

Postby carlos vargas » Mon Oct 18, 2010 8:38 pm

I use this code in my app, i use xharbour, the GLOBAL is exclusive in xharbour, but you can use static with harbour.
the functions "main_ini" and "main_end" is calls automatic for xharbour. not is need call manually in the code.


sorry for my bad english.

salu2
carlos vargas


add this line al begin in your main prg.
Code: Select all  Expand view

...
GLOBAL oMutex
...
 


add this functions to main prg
Code: Select all  Expand view

...
INIT PROCEDURE Main_Ini()

   /*crea objeto tmutex*/
   oMutex := TMutex():Open( NIL, FALSE, "KDSoft" )

   /*valida mutex*/
   IF oMutex:hMutex==0
      oMutex := TMutex():Create( NIL, FALSE, "KDSoft" )
   ELSE
      MsgAlert("La aplicación esta en ejecución actualmente!")
      oMutex:Close()
      QUIT
   ENDIF

RETURN

/*Elimina mutex creado*/
EXIT PROCEDURE Main_End()

   IF oMutex != Nil
      oMutex:Close()
   ENDIF

RETURN

...

 




add tyhis file tmutex.prg to your project.
Code: Select all  Expand view

//-----------------------------------------------------------------------------

#include "FiveWin.ch"

//-----------------------------------------------------------------------------

#define iKERNEL         "Kernel32.dll"
#define iASPASCAL       .T.

#define iTRUE           1
#define iFALSE          0

#define MUTANT_QUERY_STATE            1
#define STANDARD_RIGHTS_REQUIRED      983040
#define SYNCHRONIZE                   1048576
#define MUTEX_ALL_ACCESS              nOr( STANDARD_RIGHTS_REQUIRED, SYNCHRONIZE, MUTANT_QUERY_STATE )

#xtranslate Bool2Int( <lVar> )  =>  If( <lVar>, iTRUE, iFALSE )

//-----------------------------------------------------------------------------

CREATE CLASS TMutex
   DATA cName
   DATA hMutex

   METHOD Create( sMutexAttr, lInitialOwner, cName ) CONSTRUCTOR
   METHOD New( sMutexAttr, lInitialOwner, cName ) INLINE ::Create( sMutexAttr, lInitialOwner, cName )
   METHOD Open( nAccess, lInitialOwner, cName ) Constructor
   METHOD Close()
   METHOD Release()
   METHOD End() INLINE ::Close()
   METHOD Failed() HIDDEN

ENDCLASS

//-----------------------------------------------------------------------------

METHOD Create( sMutexAttr, lInitialOwner, cName ) CLASS TMutex
   LOCAL hDLL    := LoadLibrary( iKERNEL )
   LOCAL cFunc   := "CreateMutexA"
   LOCAL cBuffer := NIL
   LOCAL cFarProc

   DEFAULT lInitialOwner := .F., cName := "FiveWin App"

   ::hMutex := 0
   ::cName  := ""

   IF ValType( sMutexAttr ) == "O" .and. Upper( sMutexAttr:className() ) == "TSTRUCT"
      cBuffer := sMutexAttr:cBuffer
   Endif

   ::cName := cName

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG, LONG, STRING )
      ::hMutex := FWCallDLL( cFarProc,  iif( cBuffer != NIL, @cBuffer, cBuffer ), Bool2Int( lInitialOwner ), cName + Chr(0) )
      FreeLibrary( hDLL )
      IF cBuffer != NIL
         sMutexAttr:cBuffer := cBuffer
      ENDIF
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN Self

//-----------------------------------------------------------------------------

METHOD Open( nAccess, lInitialOwner, cName ) CLASS TMutex
   LOCAL hDLL    := LoadLibrary( iKERNEL )
   LOCAL cFunc   := "OpenMutexA"
   LOCAL cFarProc

   DEFAULT nAccess := MUTEX_ALL_ACCESS, lInitialOwner := .F., cName := "FiveWin App"

   ::cName := cName

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG, LONG, STRING )
      ::hMutex := FWCallDLL( cFarProc, nAccess, Bool2Int( lInitialOwner ), cName + Chr(0) )
      FreeLibrary( hDLL )
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN Self

//-----------------------------------------------------------------------------

METHOD Close() CLASS TMutex
   LOCAL hDLL  := LoadLibrary( iKERNEL )
   LOCAL cFunc := "CloseHandle"
   LOCAL nResult
   LOCAL cFarProc

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG )
      nResult  := FWCallDLL( cFarProc, ::hMutex )
      FreeLibrary( hDLL )
      ::hMutex := 0
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN nResult

//-----------------------------------------------------------------------------

METHOD Release() CLASS TMutex
   LOCAL hDLL  := LoadLibrary( iKERNEL )
   LOCAL cFunc := "ReleaseMutex"
   LOCAL cFarProc
   LOCAL nResult

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG )
      nResult := FWCallDLL( cFarProc, ::hMutex )
      FreeLibrary( hDLL )
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN nResult

//-----------------------------------------------------------------------------

METHOD Failed( nError, cFunc ) CLASS TMutex
   MsgAlert( "Error: " + LTrim( Str( nError ) ) + " al cargar " + iKERNEL + CRLF + "Función: " + cFunc, "Clase " + ::className() )
RETURN Self


//-----------------------------------------------------------------------------
//EOF
//-----------------------------------------------------------------------------
 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: SetMultiple Not working..

Postby fraxzi » Tue Oct 19, 2010 12:00 am

Enrico Maria Giordano wrote:Try (not tested):

Code: Select all  Expand view
IF FINDWINDOW( 0, cMainWindowCaption ) > 0
    SHOWWINDOW( FINDWINDOW( 0, cMainWindowCaption ), 9 )
    SETFOREGROUNDWINDOW( FINDWINDOW( 0, cMainWindowCaption ) )
    RETURN NIL
ENDIF


EMG



Dear EMG,

It didn't work...


Kind Regards,
Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: SetMultiple Not working..

Postby fraxzi » Tue Oct 19, 2010 12:34 am

Dear Mr. Carlos,

Your solution works.

I run and I copied the original .exe and run again the copied .exe and successfully detected another program of the same .exe is running.
It works in XP SP3 x86/Vista Ultimate x86/Win7 ultimate x86/Win2003 Server x86/WinServer Web 2008 R2 Ent x64


Can you explain more details?


Kind Regards,
Frances
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: SetMultiple Not working..

Postby carlos vargas » Tue Oct 19, 2010 3:56 am

Sorry, this code is work a another friend.
in this forums a found this code.

salu2
carlos vargas
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: SetMultiple Not working..

Postby Enrico Maria Giordano » Tue Oct 19, 2010 6:53 am

fraxzi wrote:
Enrico Maria Giordano wrote:Try (not tested):

Code: Select all  Expand view
IF FINDWINDOW( 0, cMainWindowCaption ) > 0
    SHOWWINDOW( FINDWINDOW( 0, cMainWindowCaption ), 9 )
    SETFOREGROUNDWINDOW( FINDWINDOW( 0, cMainWindowCaption ) )
    RETURN NIL
ENDIF


EMG



Dear EMG,

It didn't work...


Kind Regards,
Frances


This is a working sample:

Code: Select all  Expand view
#include "Fivewin.ch"


FUNCTION MAIN()

    LOCAL oWnd

    LOCAL cTitle := "This is a test"

    IF FINDWINDOW( 0, cTitle ) > 0
        SHOWWINDOW( FINDWINDOW( 0, cTitle ), 9 )
        SETFOREGROUNDWINDOW( FINDWINDOW( 0, cTitle ) )
        RETURN NIL
    ENDIF

    DEFINE WINDOW oWnd;
           TITLE cTitle

    ACTIVATE WINDOW oWnd

    RETURN NIL


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8711
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: nageswaragunupudi and 106 guests