Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to detect app already runs and kill other instance
Message
De
05/03/2007 13:10:22
 
 
À
05/03/2007 09:37:41
Information générale
Forum:
Visual FoxPro
Catégorie:
Problèmes
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01200641
Message ID:
01200810
Vues:
20
Thank you very much, Denis. It helps me to find the right processid. The only problem left, is that TerminateProcess() does not take a Pid, but a process-handle, and those things seem to be entirely different. How to come from Pid to PHandle I still don't know.

Lennert

>>Hello everybody,
>>
>>Sometimes our app is seen to be still existing (when opening the task manager).
>>
>>- How could I detect it is already running and..
>>- How can I kill the process of the oldest instance by a call from the newer instance?
>>
>>TIA,
>>
>>Lennert
>
>I don't remember who this code came from but perhaps it could help
>
>
>		*--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 representation 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
>
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform