Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Know if a program is already running
Message
 
To
20/06/2003 06:57:39
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00802126
Message ID:
00802149
Views:
18
Tony

Here is another one. I didn't write this, but got it off the net, the authors name is in the file. One reason I like this one, is because if another copy is already running, this one quits and brings the running version up front. Very easy to call.
************************************************************
*  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()

   *-- 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 ADDI
   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 = "STARBASE"+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(MainHwnd(), @cExeFlag, 1)
      lRetVal = .T.
   ENDIF

   RETURN lRetVal

ENDFUNC
Previous
Reply
Map
View

Click here to load this message in the networking platform