Page 1 of 1 1
Topic Options
#175806 - 2007-04-30 08:28 AM Detect running program?
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
How do i get KiX to detect a program if its running or not? and the display something to the user?
Top
#175807 - 2007-04-30 10:29 AM Re: Detect running program? [Re: daddster]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I think you should at least have some idea how to recognise the program you want to monitor.
Top
#175816 - 2007-04-30 03:46 PM Re: Detect running program? [Re: Witto]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
He could always use the EnumProcess() UDF to enumerate what's running or to check for a specific process.

Assuming, of course, that he knows what process he's looking for.

Top
#175882 - 2007-05-02 09:45 AM Re: Detect running program? [Re: eriqjaffe]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
I know the prosess yes. So something i know at least \:\) The problem is i can't get the EnumProcess to work. Getting lots of errors. And don't understand all i need to place into it. Im just thrown into this and never programmed anything in my life.

Edited by daddster (2007-05-02 09:46 AM)

Top
#175883 - 2007-05-02 09:54 AM Re: Detect running program? [Re: daddster]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
The script below checks if word is running.
You should change the $pid= EnumProcess("WinWord.exe") part to the program you want to check for.

 Code:
Break on

$pid= EnumProcess("WinWord.exe")
If $pid <> ""
	?"MS Word is running"
Else
	?"MS Word is NOT running"
EndIf

Sleep 5

Function EnumProcess($exe, optional $terminate, optional $Computer)
	Dim $winmgmts, $ExecQuery, $Process, $id, $getobject, $
	If Not $computer	$computer=@wksta	EndIf
	$winmgmts="winmgmts:{impersonationLevel=impersonate}!//$COMPUTER"
	Select
		Case Val($exe)>0
			$ExecQuery="select * from Win32_Process where ProcessId='$exe'"
			$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
			For Each $Process in $GetObject
				If $terminate		$=$Process.Terminate	EndIf
				$EnumProcess = $Process.name
				Next
			$GetObject=''
		Case VarType($exe)=8
			$ExecQuery="select * from Win32_Process where Name='$exe'"
			$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
			For Each $Process in $GetObject
				If $terminate		$=$Process.Terminate	EndIf
				$id=$Process.ProcessId
				$EnumProcess = "$Id" + "|" + "$EnumProcess"
				Next
			$EnumProcess=Left($EnumProcess,Len($EnumProcess)-1)
			$GetObject=''
		Case 1
			Exit 1
		EndSelect
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#175890 - 2007-05-02 01:37 PM Re: Detect running program? [Re: Mart]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
Tnx a lot. Just what i needed \:\)
Top
#175954 - 2007-05-04 09:42 AM Re: Detect running program? [Re: daddster]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
When i run it now i get error: ERROR:unknown command [winmgmts:{impersonationLevel]!

And when i use Kix2EXE since i need to distribute the script it somehow always detect that the program is running. And i know it ain't. If i ; out the funktion to check for the running program whole script goes in compiled EXE mode as intended.

Top
#175955 - 2007-05-04 09:45 AM Re: Detect running program? [Re: daddster]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Quote:

....
And when i use Kix2EXE since i need to distribute the script it somehow always detect that the program is running. And i know it ain't.
....


So you are checking if kix32.exe is running from a kix script?
If so then it’s normal to get back that kix32.exe is running because you are executing the script.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#175956 - 2007-05-04 09:45 AM Re: Detect running program? [Re: daddster]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
 Code:
$pid= EnumProcess("proxy.exe")
If $pid <> ""
			If $debug = "yes"
				MESSAGEBOX ("Program running", "Test")
			EndIf

		Run "c:\kix\kix2exe-v121-installer.exe
		Exit
EndIf

Sleep 5

Function EnumProcess($exe, optional $terminate, optional $Computer)
	DIM $winmgmts, $ExecQuery, $Process, $id, $getobject, $
	If Not $computer	$computer=@WKSTA	EndIf
	$winmgmts="winmgmts:{impersonationLevel=impersonate}!//$COMPUTER"

	Select
		Case Val($exe)>0
			$ExecQuery="select * from Win32_Process where ProcessId='$exe'"
			$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
			For Each $Process In $GetObject
				If $terminate		$=$Process.Terminate	EndIf
				$EnumProcess = $Process.name
				Next
			$GetObject=''
		Case VarType($exe)=8
			$ExecQuery="select * from Win32_Process where Name='$exe'"
			$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
			For Each $Process In $GetObject
				If $terminate		$=$Process.Terminate	EndIf
				$id=$Process.ProcessId
				$EnumProcess = "$Id" + "|" + "$EnumProcess"
				Next
			$EnumProcess=Left($EnumProcess,Len($EnumProcess)-1)
			$GetObject=''
		Case 1
			Exit 1
		EndSelect
EndFunction


Edited by daddster (2007-05-04 01:34 PM)

Top
#175957 - 2007-05-04 09:45 AM Re: Detect running program? [Re: daddster]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
This is the one i use when i get the error
Top
#175958 - 2007-05-04 09:56 AM Re: Detect running program? [Re: daddster]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Please use the code tags next time you post code so the formatting gets preserved and it's much more readable.

Looking at your script you are trying to distibute kix2exe (Kix2Exe by Senser). Why? All your users need to make exe files from kix scripts? Please tell us a bit more about what you are trying to do. And post the entire code because in the code above I do not see where $debug gets set.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#175959 - 2007-05-04 10:05 AM Re: Detect running program? [Re: Mart]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
is the code tags
 Code:
;;;;;;;;;;;;;;;
; Utrulling av xxx til lokal maskin.
; Laget av xxx, 02.05.2007
;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;   				
; Om gammel Windows versjon eller Vista vises tekst og beskjed om at det må oppgraderes.
;;;;;;;;;;;;;;;
$debug = "yes"
If @DOS="4.0" 
; OR @DOS="6.0"
	MESSAGEBOX ("xxx støtter ikke ditt operativ system, vennligst kontakt lokal IT ansvarlig", "xxx", 48)
	Exit
Endif

;;;;;;;;;;;;;;;
; Lager en variabel til hvor xxx filene ligger.
;;;;;;;;;;;;;;;
IF EXIST ("q:\xxx\xxx.exe")
	$fildir = q:\xxx\
	If $debug = "yes"
		MESSAGEBOX ("filedir = q", "xxx")
	EndIf
		
EndIf

If Exist ("h:\xxx\xxx.exe")
	$fildir = h:\xxx\
	If $debug = "yes"
		MESSAGEBOX ("filedir = h", "xxx")
	EndIf
	
EndIf

If Exist ("p:\xxx\xxx.exe")
	$fildir = p:\xxx\
	If $debug = "yes"
		MESSAGEBOX ("filedir = p", "xxx")
	EndIf
EndIf


;;;;;;;;;;;;;;;
; Funksjon for å se om xxx kjører
;;;;;;;;;;;;;;;
Break on

$pid= EnumProcess("xxx.exe")
If $pid <> ""
			If $debug = "yes"
				MESSAGEBOX ("xxx kjører funksjon", "xxx")
			EndIf

		Run "%programfiles%\xxx\xxx.exe"
		Exit
EndIf

Sleep 5

Function EnumProcess($exe, optional $terminate, optional $Computer)
	DIM $winmgmts, $ExecQuery, $Process, $id, $getobject, $
	If Not $computer	$computer=@WKSTA	EndIf
	$winmgmts="winmgmts:{impersonationLevel=impersonate}!//$COMPUTER"
	Select
		Case Val($exe)>0
			$ExecQuery="select * from Win32_Process where ProcessId='$exe'"
			$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
			For Each $Process In $GetObject
				If $terminate		$=$Process.Terminate	EndIf
				$EnumProcess = $Process.name
				Next
			$GetObject=''
		Case VarType($exe)=8
			$ExecQuery="select * from Win32_Process where Name='$exe'"
			$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
			For Each $Process In $GetObject
				If $terminate		$=$Process.Terminate	EndIf
				$id=$Process.ProcessId
				$EnumProcess = "$Id" + "|" + "$EnumProcess"
				Next
			$EnumProcess=Left($EnumProcess,Len($EnumProcess)-1)
			$GetObject=''
		Case 1
			Exit 1
		EndSelect
EndFunction

;;;;;;;;;;;;;;;
; Oppretter xxx Directory i Program filer mappen uavhengig av OS språk. 
; NB !!! VIRKER IKKE PÅ VISTA ENDA !!!   					  			
;;;;;;;;;;;;;;;
;	If $debug = "yes"
;		MESSAGEBOX ("Lage fildir", "xxx")
;	EndIf

;CD %programfiles%
;MD "xxx"

;;;;;;;;;;;;;;;						   				
; @DOS=5.0 er Windows 2000, @DOS=5.1 er Windows XP, @DOS=5.2 er Windows 2003, @DOS=6.0 er Windows Vista
; Speiler mappen på serveren ut på klientmaskin. I silent mode.
; NB !!! VIRKER IKKE PÅ VISTA ENDA !!!
;;;;;;;;;;;;;;;
	
MESSAGEBOX ("Oppgradering pågår.  Vennligst Vent. 
xxx vil starte automatisk når oppgradreingen er ferdig", "xxx Oppgragering", 64, 5)

	If $debug = "yes"
		MESSAGEBOX ("Sjekke Windows versjon og kopiere", "xxx")
	EndIf

If @DOS="5.0" OR @DOS="5.1" OR @DOS="5.2" OR @DOS="5.3" OR @DOS="6.0"
	If Exist ("q:\xxx\xxx.exe")
		Shell 'ROBOCOPY q:\xxx\distribusjon\ "%programfiles%\xxx\ /MIR /R:1 /W:1'
	Else
	If Exist ("p:\xxx\xxx.exe")
;		Shell 'ROBOCOPY p:\xxx\distribusjon\ "%programfiles%\xxx  /MIR /R:1 /W:1'
		Shell 'ROBOCOPY p:\xxx\distribusjon\ c:\testdir\xxx\ /MIR /R:1 /W:1'
	Else
	If Exist ("H:\xxx\xxx.exe")
		Shell 'ROBOCOPY h:\xxx\distribusjon\ "%programfiles%\xxx\ /MIR /R:1 /W:1'
	EndIf
EndIf
					

;;;;;;;;;;;;;;;
; Funksjon for laging av Shortcut.
;;;;;;;;;;;;;;;

	If $debug = "yes"
		MESSAGEBOX ("shortcut", "xxx")
	EndIf


function WshShortCut($path,$targetpath,optional $arguments,optional $startdir,optional $iconpath,optional $style)

dim $shell,$shortcut
$shell=createobject("wscript.shell")
if $shell
   	$shortcut=$shell.createshortcut($path)
if $shortcut
     	$shortcut.targetpath=$targetpath
	if $arguments
       	$shortcut.arguments=$arguments
	endif
	if $startdir
  	     	$shortcut.workingdirectory=$startdir
	endif
	if $iconpath
    		$shortcut.iconlocation=$iconpath
	endif
	if $style
    		$shortcut.windowstyle=$style
	endif
$shortcut.save
$shortcut=0
endif
$shell=0
endif
exit @error
endfunction

WshShortCut("%USERPROFILE%\Desktop\xxx.lnk","$fildir\xxx.EXE") 
EndIF

;;;;;;;;;;;;;;;
; Avslutte med å starte xxx
;;;;;;;;;;;;;;;

	If $debug = "yes"
		MESSAGEBOX ("starte xxx", "xxx")
	EndIf


run "%programfiles%\xxx\xxx.exe"


Here u go \:\)


Edited by daddster (2007-05-04 10:11 AM)

Top
#175960 - 2007-05-04 10:11 AM Re: Detect running program? [Re: daddster]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Ok you found the code tags.
First thing I see is the missing quotes around the $fildir = lines in the first part of your code.


Edited by Mart (2007-05-04 10:21 AM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#175961 - 2007-05-04 10:20 AM Re: Detect running program? [Re: Mart]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
we want to update programfiles on local computer from a server, with the Robocopy /mir function. The Function to check running program was so it would not copy when the program is in use, since its and oracle database u can have many open instances. so something like:

Copy to local server -> when user start program it starts script then program -> the script check if program is running -> if running not do anything but start a new instance and that will result in an error from that. -> if not running then copy files from local server to local computer and start them from there at the end.

Top
#175969 - 2007-05-04 12:30 PM Re: Detect running program? [Re: daddster]
daddster Offline
Fresh Scripter

Registered: 2007-04-18
Posts: 15
Found the error \:\) Called the executable file same as the check if running file. Can i in that function also ask on description?
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 764 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.066 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org