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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how not to open twice an prg exe

Status
Not open for further replies.

twintwin

Programmer
Jun 2, 2005
1
FR
How to prevent a second opening of an executable prog soon open on the desktop
 

A quick look in the FAQ section of this forum returns the following:

one instance of program without use of WINDOW TITLE
FAQ184-2442
Only one instance of an application Faq184-1767
Having only one instance of App, bring window to top
faq184-1998
How to ensure that only one instance of my application is running in my desktop.
faq184-839


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
To read a detailed analysis on the different ways of accomplishing this and a few other ways not detailed in the FAQ section see:

I've been working on some code with
thread184-672388

boyd.gif

 
Other way:

Code:
? IsRunning(YourApplicationUNIQUEString)

FUNCTION IsRunning(tcSemaphoreName)
    LOCAL lpszSemName
    #Define ERROR_ALREADY_EXISTS 183
    Declare Integer GetLastError In win32API
    Declare Integer CreateSemaphore In WIN32API ;
            string @ lpSemaphoreAttributes, ;
            LONG lInitialCount, ;
            LONG lMaximumCount, ;
            string @ lpName

    aladin_href = CreateSemaphore(0,0,1,tcSemaphoreName)
RETURN (aladin_href # 0 .AND. GetLastError() == ERROR_ALREADY_EXISTS)

Borislav Borissov
 
Put this in the start of your PRG:
Code:
DO DupStart
and this near the end of the same PRG:
Code:
PROCEDURE DupStart

#DEFINE SW_NORMAL    1
#DEFINE SW_MAXIMIZE  3
#DEFINE SW_MINIMIZE  6

DECLARE Long FindWindow in user32 String, String
DECLARE Long BringWindowToTop in user32 Long
DECLARE Long ShowWindow in user32 Long, Long

LOCAL lnHWND,lcTitle
lcTitle = _screen.Caption
_SCREEN.Caption = Sys(3)
lnHWND = FindWindow(null,m.lcTitle)
_SCREEN.Caption = m.lcTitle

IF m.lnHWND >0
  =messagebox("This program is already running "+ ;
  "(or make up your own words)"+ ;
  space(10),0+64+0,"System Message")
  BringWindowToTop(m.lnHWND)
  ShowWindow(m.lnHWND,SW_MAXIMIZE)
  QUIT
ENDIF

ENDPROC

Good luck
Lee....

Visual FoxPro Versions: 6 & 9
Operating System: Windows XP
 
I get this code from the internet but I do not remeber where

Code:
************************************************************
*  FUNCTION FirstInstance()
************************************************************
*  Author............: Gary Foster, Pointsource Consulting, Inc.  104171.3677@compuserve.com
*  Project...........: Starbase
*  Created...........: 03/13/98  15:39:58
*  Copyright.........: Public Domain
*) Description.......: This function uses the creation of a named MUTEX
*)                   : to determine existing instance of an application.
*)                   : Call this function very early in an application's
*)                   : startup to create a named object that can be checked
*)                   : every time the application is started.  If the named 
*)                   : object exists, the function will try to bring foreward
*)                   : the applications main frame.  In this case, the main frame
*)                   : is the FoxPro _SCREEN. Any other hWnd would do as well.
*)                   : 
*  Calling Samples...: IF !FirstInstance()
*                    :    QUIT  && Or something along that line.
*                    : ELSE
*                    :    yada yada yada...
*  Parameter List....: 
*  Major change list.:
************************************************************

FUNCTION _FirstInstance()
	PARAMETERS __MUTEX

   *-- Declare just enough API functions and constant defines to support this function.
   #DEFINE SW_RESTORE               9
   #DEFINE ERROR_ALREADY_EXISTS   183
   #DEFINE GW_HWNDNEXT              2
   #DEFINE GW_CHILD                 5

   DECLARE INTEGER CreateMutex IN win32api INTEGER, INTEGER, STRING @
   DECLARE INTEGER CloseHandle IN win32api INTEGER
   DECLARE INTEGER GetLastError IN win32api
   DECLARE INTEGER SetProp IN win32api INTEGER, STRING @, INTEGER
   DECLARE INTEGER GetProp IN win32api INTEGER, STRING @
   DECLARE INTEGER RemoveProp IN win32api INTEGER, STRING @
   DECLARE INTEGER IsIconic IN win32api INTEGER
   DECLARE INTEGER SetForegroundWindow IN USER32 INTEGER
   DECLARE INTEGER GetWindow IN USER32 INTEGER, INTEGER
   DECLARE INTEGER ShowWindow IN WIN32API INTEGER, INTEGER
   DECLARE INTEGER GetDesktopWindow IN WIN32API

   *-- We need a function from Foxtools. I know I could get this from the API, but I'm lazy.
*   IF !("FOXTOOLS" $ UPPER(SET("LIBRARY")))
*      SET LIBRARY TO FOXTOOLS.fll ADDITIVE 
*   ENDIF
   
   LOCAL cExeFlag  && Name of the MUTEX
   LOCAL nExeHwnd  && MUTEX handle
   LOCAL hWnd      && Window handle
   LOCAL lRetVal   && Return value of this function

   *-- Try and create a new MUTEX with this name.
   cExeFlag = __MUTEX + CHR(0)
   nExeHwnd = CreateMutex(0,1,@cExeFlag)

   *-- If the named MUTEX creation fails because it exists already, try to display
   *-- the existing application and have this function return .F., presumably
   *-- to a calling routine that is deciding whether to continue instantiation.
   IF GetLastError() = ERROR_ALREADY_EXISTS

      *-- Get the hWnd of the first top level window on the Windows Desktop.
      HWND = GetWindow(GetDesktopWindow(), GW_CHILD)

      *-- Loop through the windows. If the application has no top level window, 
      *-- the loop will quickly run out of desktop windows and simply exit.
      DO WHILE HWND > 0

         *-- Is this the proper window? GetProp looks() for a property we added
         *-- the first time, not a caption.
         IF GetProp(HWND, @cExeFlag) = 1

            *-- If the application window is minimized, open 'er up to the 
            *-- previous state.
            IF IsIconic(HWND) > 0
               ShowWindow(HWND,SW_RESTORE)
            ENDIF

            *-- Show me the money, oops, I meant the window, Show Me The Window!
            SetForegroundWindow(HWND)

            *-- Our work here is done, ride off into the sunset.
            EXIT

         ENDIF

         *-- Get the next window handle.
         HWND = GetWindow(HWND,GW_HWNDNEXT)

      ENDDO

      *-- Close the 'failed to open' MUTEX handle, we don't need it any more.
      CloseHandle(nExeHwnd)

      lRetVal = .F.
   ELSE
      *-- Add a property to the FoxPro main frame so we can identify this 
      *-- window again regardless of caption changes.
		SetProp(application.HWND, @cExeFlag, 1)
      lRetVal = .T.

   ENDIF

   CLEAR DLLS ;
   		[CreateMutex],;
   		[CloseHandle],;
   		[GetLastError],;
   		[SetProp],;
   		[GetProp],;
   		[RemoveProp],;
   		[IsIconic],;
   		[SetForegroundWindow],;
   		[GetWindow],;
   		[ShowWindow];
   		[GetDesktopWindow]

   RETURN lRetVal
ENDFUNC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top