|
-
Sep 13th, 2007, 04:45 AM
#1
Thread Starter
Junior Member
VBScript code produces an Expected Statement error???
Hi, this is the VBScript code for the program described in this thread: http://www.codeguru.com/forum/showthread.php?t=433810
Code:
rem This application allows a user to run only one program (in this case "game.exe")
rem In case other programs are running, it terminates them
rem The same action will be taken with newly-executed programs
rem Set mandatory variables and objects
strComputer = "."
sec = 0
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery (" Select * from Win32_Process Where Name = 'game.exe' " )
rem Count how many processes where found (should be one as we compared only against 'game.exe')
rem This is done to check whether the game is started
colProcessListCounter = 0
For Each objProcess in colProcessList
colProcessListCounter = colProcessListCounter + 1
Next
rem Game is started? Good, let's go:
If colProcessListCounter = 1 Then
While colProcessListCounter = 1
rem The next three lines form an interval mechanism which
rem makes the process scanning below happen every 2 seconds
sec = Second(Time)
sec = sec / 2
If InStr(sec, ".") = 0 Then
rem Here we scan for rebellious applicationds which are not Game.exe and must be terminated:
Set colProcessList = objWMIService.ExecQuery (" Select * from Win32_Process Where Name <> 'game.exe' " )
For Each objProcess in colProcessList
rem Kill!
objProcess.Terminate()
Next
rem Now we check again whether Game.exe is still on - otherwise, we can end the script.
Set colProcessList = objWMIService.ExecQuery (" Select * from Win32_Process Where Name = 'game.exe' " )
colProcessListCounter = 0
For Each objProcess in colProcessList
colProcessListCounter = colProcessListCounter + 1
Next
end if
Wend
end if
End
However, the code produces an 'Expected Statement' error, and is not working. I'd really like to find out why
Thanks you for your kind help!
Last edited by pilau; Sep 13th, 2007 at 04:48 AM.
-
Sep 13th, 2007, 04:56 AM
#2
Re: VBScript code produces an Expected Statement error???
take out the end statement on the last line
how do you know it is not working?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 13th, 2007, 05:13 AM
#3
Thread Starter
Junior Member
Re: VBScript code produces an Expected Statement error???
Okay, now it's working, but I have a problem - My time mechanism isn't good, as it's consuming a lot of CPU because the loop goes on and on.
Maybe you have a suggestion fr a beter interval code?
-
Sep 13th, 2007, 06:43 AM
#4
Re: VBScript code produces an Expected Statement error???
you should always make a way to exit from the loop and stop the script
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 13th, 2007, 07:59 AM
#5
Thread Starter
Junior Member
Re: VBScript code produces an Expected Statement error???
Thanks for your time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|