How to Test if a program is already running.

Xbase++ 2.0 Build 554 or later
Post Reply
Message
Author
User avatar
PedroAlex
Posts: 231
Joined: Tue Feb 09, 2010 3:06 am

How to Test if a program is already running.

#1 Post by PedroAlex »

Hello!

In Xbase 2.0, is it possible to test whether a given program not developed in xBase is already running?

In other words, within my xbase application I need to know if a specific program not developed in xBase is already running on Windows.
If it isn't, I have to run it with runshell.

Has anyone ever had this need?
Can you give me a tip!?
Many Thanks in advance.
Pedro Alexandre

User avatar
Auge_Ohr
Posts: 1407
Joined: Wed Feb 24, 2010 3:44 pm

Re: How to Test if a program is already running.

#2 Post by Auge_Ohr »

i use this CODE Syntax under Xbase++ v1.9 to check Tasklist if APP if running

Code: Select all

^*+˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛
*+
*+    Source Module => d:\ALASKA\XPPTEL\CHECKAPP.PRG
*+
*+    Functions: Function ChkPrevApp()
*+               Function gettasklist()
*+               Function GetWindow()
*+               Function GetWindowTextA()
*+               Function IsWindowVisible()
*+
*+    Reformatted by Click! 2.03a on Feb-19-2019 at  7:58 am
*+
*+˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛˛

*PROCEDURE AppSys
*   LOCAL oDlg, oXbp
*
*   // Check if a copy is already running
*   IF ChkPrevApp( "MyApp" )
*      QUIT
*   ENDIF
*   ...
*   ...
*   ...
*   ...
*
*RETURN
*

*****************************************************************************
* Routines to check for application already running
*****************************************************************************

#include "DLL.ch"
#include "common.ch"

#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

/*
 * ShowWindow() Commands, from WINUSER.H
 */
#define SW_HIDE             0
#define SW_SHOWNORMAL       1
#define SW_NORMAL           1
#define SW_SHOWMINIMIZED    2
#define SW_SHOWMAXIMIZED    3
#define SW_MAXIMIZE         3
#define SW_SHOWNOACTIVATE   4
#define SW_SHOW             5
#define SW_MINIMIZE         6
#define SW_SHOWMINNOACTIVE  7
#define SW_SHOWNA           8
#define SW_RESTORE          9
#define SW_SHOWDEFAULT      10
#define SW_MAX              10

DLLFUNCTION ShowWindow( nHwnd, nCmdShow ) USING STDCALL FROM USER32.DLL
DLLFUNCTION BringWindowToTop( nHwnd ) USING STDCALL FROM USER32.DLL
DLLFUNCTION SetForegroundWindow( nHwnd ) USING STDCALL FROM USER32.DLL

*******************************************************************************

*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
*+    Function ChkPrevApp()
*+
*+    Called from ( xpptel.prg   )   1 - procedure appsys()
*+
*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
FUNCTION ChkPrevApp( cNaam )

   *******************************************************************************
LOCAL oDlg
LOCAL aTasklist
LOCAL aSize     := { 0, 0 }
LOCAL aPos      := { 0, 0 }
LOCAL lRunnin   := .F.
LOCAL i
LOCAL nHwnd
LOCAL cWind

   oDlg := XBPDIALOG() :new( APPDESKTOP(),, aPos, aSize,, .F. )
   oDlg:clipSiblings := .T.
   oDlg:drawingArea:ClipChildren := .T.
   oDlg:create()

   SETAPPFOCUS( oDlg )

   aTasklist := GetTaskList( oDlg:gethWnd( ) )

   FOR i = 1 TO LEN( aTasklist )
      cWind := TRIM( UPPER( SUBSTR( aTasklist[ i ], 9 ) ) )
      cWind := SUBSTR( cWind, 1, LEN( cWind ) - 1 )
      IF cWind == TRIM( UPPER( cNaam ) )
         lRunnin := .T.
         nHwnd := VAL( LEFT( aTasklist[ i ], 8 ) )

         oDlg:destroy()

         ShowWindow( nHwnd, SW_RESTORE )
         BringWindowToTop( nHwnd )
         SetForegroundWindow( nHwnd )

         RETURN lRunnin
      ENDIF
   NEXT

   oDlg:destroy()

RETURN lRunnin

*******************************************************************************

*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
*+    Function gettasklist()
*+
*+    Called from ( checkapp.prg )   1 - function chkprevapp()
*+                                   1 - function is_osk()
*+
*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
FUNCTION gettasklist( hWnd, lVisible )

   *******************************************************************************
LOCAL aList       := {}
LOCAL cWindowName
LOCAL nVisible

   DEFAULT lVisible TO .T.

   WHILE hWnd != 0
      cWindowname := SPACE( 100 )
      IF ( getwindowtexta( hWnd, @cWindowName, LEN( cWindowName ) ) <> 0 )

         IF lVisible
            nVisible := IsWindowVisible( hWnd )
            IF nVisible == 1
               AADD( aList, STR( hWnd, 8 ) + cWindowname )
            ENDIF
         ELSE
            AADD( aList, STR( hWnd, 8 ) + cWindowname )
         ENDIF

      ENDIF
      hWnd = GetWindow( hWnd, GW_HWNDNEXT )
   END
RETURN aList

*******************************************************************************

*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
*+    Function GetWindow()
*+
*+    Called from ( checkapp.prg )   1 - function gettasklist()
*+
*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
FUNCTION GetWindow( hWnd, uCmd )

   *******************************************************************************
LOCAL nDll := DllLoad( "USER32.DLL" )
LOCAL xRet := DllCall( nDll, DLL_STDCALL, "GetWindow", hWnd, uCmd )
   DllUnLoad( nDll )
RETURN xRet

*******************************************************************************

*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
*+    Function GetWindowTextA()
*+
*+    Called from ( checkapp.prg )   1 - function gettasklist()
*+                ( dxe_dlg.prg  )   1 - function enumwindow()
*+
*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
FUNCTION GetWindowTextA( hWnd, lPstring, nMax )

   *******************************************************************************
LOCAL nDll := DllLoad( "USER32.DLL" )
LOCAL xRet := DllCall( nDll, DLL_STDCALL, "GetWindowTextA", hWnd, @lPstring, nMax )
   DllUnLoad( nDll )
RETURN xRet

*******************************************************************************

*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
*+    Function IsWindowVisible()
*+
*+    Called from ( checkapp.prg )   1 - function gettasklist()
*+
*+±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*+
FUNCTION IsWindowVisible( hWnd )

   *******************************************************************************
LOCAL nDll := DllLoad( "USER32.DLL" )
LOCAL xRet := DllCall( nDll, DLL_STDCALL, "IsWindowVisible", hWnd )
   DllUnLoad( nDll )
RETURN xRet

*+ EOF: CHECKAPP.PRG
greetings by OHR
Jimmy

User avatar
Tom
Posts: 1172
Joined: Thu Jan 28, 2010 12:59 am
Location: Berlin, Germany

Re: How to Test if a program is already running.

#3 Post by Tom »

As an eXpress+-user, you have a ready-made function for this:

Code: Select all

DC_IsAppRunning( <cClass>, ;
                 <cTitle>, ;
                 [<cFile>], ;
                 [<lRestore>] ) -> lStatus
Best regards,
Tom

"Did I offend you?"
"No."
"Okay, give me a second chance."

User avatar
PedroAlex
Posts: 231
Joined: Tue Feb 09, 2010 3:06 am

Re: How to Test if a program is already running.

#4 Post by PedroAlex »

Jimmy.
Many Thanks.
I Will try it.

Tom.
Dc_IsAppruning only check for xbase aps! Right?
Pedro Alexandre

User avatar
Tom
Posts: 1172
Joined: Thu Jan 28, 2010 12:59 am
Location: Berlin, Germany

Re: How to Test if a program is already running.

#5 Post by Tom »

Dc_IsAppruning only check for xbase aps! Right?
Right.
Best regards,
Tom

"Did I offend you?"
"No."
"Okay, give me a second chance."

Post Reply