Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to know that a program is running
Message
From
18/09/2006 12:29:38
 
 
To
18/09/2006 11:57:19
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01154741
Message ID:
01154754
Views:
10
There are a few ways to do this, here are a couple:
*--Look for a specific process - in this case, vfp9.exe
myapp = "vfp9.EXE"

_SCREEN.WINDOWSTATE = 2
lcComputer		= Iif(Vartype(tcComputer) = 'C' and not Empty(tcComputer), tcComputer, '.')
loWMIService	= GetObject("winmgmts://" + lcComputer + "/root/cimv2")
loProcesses		= loWMIService.ExecQuery("SELECT * FROM Win32_Process")
lcprocesses    = ""

DECLARE INTEGER TerminateProcess IN kernel32;
	INTEGER hProcess, INTEGER uExitCode

DIMENSION allprocs(1)
i = 0
lnrunning = 0
CREATE CURSOR openproc (cprocess c(50), nhandle c(10))
For Each loProcess in loProcesses
	with loProcess
		i = i + 1
		DIMENSION allprocs(i)
		allprocs(i) = .Name
		INSERT INTO openproc VALUES (allprocs(i), .handle)
		? .Name, .ExecutablePath, .caption, .commandline, Ctot(Transform(.CreationDate, '@R 9999-99-99T99:99:9999'))
		IF UPPER(ALLTRIM(allprocs(i))) = UPPER(ALLTRIM(myapp))
			lnrunning = lnrunning + 1
		ENDIF
	endwith
NEXT
IF lnrunning > 0
   =MESSAGEBOX(ALLTRIM(STR(lnrunning))+" copie(s) of "+myapp+" is/are running.")
ELSE
   =MESSAGEBOX(myapp+" is not running")
ENDIF
SELE openproc
BROWSE



declare integer EnumProcesses in psapi.dll string@, integer, integer@

lparray = replicate(chr(0),2048)

lnsizeneeded = 0

if EnumProcesses(@lparray,len(lparray),@lnsizeneeded) <> 0 then

   * lnsizeneeded is the size (in bytes) of the information returned in lparray; truncate if necessary

   lparray = left(lparray,lnsizeneeded)

   lctmp = space(0)

   for lxx = 1 to lnsizeneeded step 4

      lctmp = substr(lparray,lxx,4)

      * lctmp is a character reprentation of the DWORD representing the Process ID.  It needs to be converted to decimal.

      ? 'PID is ' + transform(asc(right(lctmp,1))*(256^3)+asc(substr(lctmp,3,1))*(256^2)+asc(substr(lctmp,2,1))*(256)+asc(left(lctmp,1)))
   endfor

endif

RETURN
and this one courtesy of Marcia Akins:
*--Courtesy Marcia Akins
LPARAMETERS tcAppName
*** This function uses the creation of a named MUTEX to determine whether
*** or not the application is already running. This function should be called
*** near the top of the application's main program 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 activate the FoxPro running application.

LOCAL lcMsg, lnAttributes, lnOwner, lcName, lnMutexHandle, lnhWnd, lnRelationship, lnStyle, llRetVal
LOCAL lcAppName

lcMsg = ''
SET ASSERTS ON
IF EMPTY( NVL( tcAppName, '' ) )
  TEXT TO lcMsg NOSHOW 
    This is another Brain Dead Programmer Error.
    You must pass the name of an application to FirstTime().
    Have a nice day now...
  ENDTEXT
  ASSERT .F. MESSAGE lcMsg
  RETURN 
ENDIF

lcAppName = UPPER( ALLTRIM( tcAppName ) ) + CHR( 0 )

*** Declare just API functions and constants
#DEFINE SW_MAXIMIZE              3
#DEFINE ERROR_ALREADY_EXISTS   183
#DEFINE GW_HWNDNEXT              2
#DEFINE GW_CHILD                 5

DECLARE INTEGER CreateMutex IN WIN32API INTEGER lnAttributes, INTEGER lnOwner, STRING @lcName
DECLARE INTEGER GetProp IN WIN32API INTEGER lnhWnd, STRING @lcName
DECLARE INTEGER SetProp IN WIN32API INTEGER lnhWnd, STRING @lcNam, INTEGER lnValue
DECLARE INTEGER CloseHandle IN WIN32API INTEGER lnMutexHandle
DECLARE INTEGER GetLastError IN WIN32API 
DECLARE INTEGER GetWindow IN USER32 INTEGER lnhWnd, INTEGER lnRelationship
DECLARE INTEGER GetDesktopWindow IN WIN32API 
DECLARE BringWindowToTop IN Win32APi INTEGER lnhWnd
DECLARE ShowWindow IN WIN32API INTEGER lnhWnd, INTEGER lnStyle

*** Try and create a new MUTEX with the name of the passed application
lnMutexHandle = CreateMutex( 0, 1, @lcAppName )

*** If the named MUTEX creation fails because it exists already, try to display
*** the existing application 
IF GetLastError() = ERROR_ALREADY_EXISTS

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

  *** Loop through the windows. 
  DO WHILE lnhWnd > 0

     *** Is this the one that we are looking for?
     *** Look for the property we added the first time
     *** we launched the application
     IF GetProp( lnhWnd, @lcAppName ) = 1
       *** Activate the app and exit stage left
       BringWindowToTop( lnhWnd )
       ShowWindow( lnhWnd, SW_MAXIMIZE )
       EXIT
     ENDIF
     lnhWnd = GetWindow( lnhWnd, GW_HWNDNEXT )
  ENDDO

  *** Close the 'failed to open' MUTEX handle
  CloseHandle( lnMutexHandle )
ELSE
  *** Add a property to the FoxPro App so we can identify it again
  SetProp( _vfp.Hwnd, @lcAppName, 1)
  llRetVal = .T.
ENDIF

RETURN llRetVal
>Hi all ,
>
>I would want to know if my program is running.
>
>thanks
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Previous
Reply
Map
View

Click here to load this message in the networking platform