Topic Options
#1379 - 07/16/03 03:43 PM Chapter 02 – Running Programs
Luke Tomasello Administrator Offline
Member

Registered: 09/17/00
Posts: 740
Loc: San Jose, CA., USA
Chapter 02 – Running Programs, Part I

In the first chapter we learned how to create a simple script, and to load that script as a standalone job.
We also learned how to add that same script to our master schedule to have it execute on-time.

In this chapter we’ll learn how to run a program (.EXE, .BAT, .CMD, etc..)

Fortunately, TaskGhost ships with several pre-made scripts that do exactly what we want.
Lets take a look at the CreateProcess.vbs script:
Code:
Sub main(commandline)

	Dim exit_code
	exit_code = TGCtrl.CreateProcess(0,commandline)
	
	If exit_code = -1 Then 
		TGCtrl.Print(commandline & " failed to start!") 
	Else 
		TGCtrl.Print(commandline & " returned " & exit_code)
	End If
	
End Sub
As you can see, this script takes one parameter, ‘commandline’ and feeds it to the CreateProcess function. (The CreateProcess.vbs script simply wraps the TGCtrl.CreateProcess() function for convenience.)

So. If we wanted to execute my_prog.exe, then we would call the CreateProcess.vbs from Schedule.vbs like this:
If TGCtrl.CheckTime (8, tc, "* * * * * * * *") = true Then TGCtrl.Run 0, "CreateProcess.vbs", "my_prog.exe"

Okay, so lets give this a try.
There isn’t really a program called; “my_prog.exe”, so we’ll use the ‘test’ program that ships with TaskGhost; “TG_TEST.EXE”.

Add the following line to TaskGhost\Sctipts\Schedule.vbs
If TGCtrl.CheckTime (8, tc, "* * * * * * * *") = true Then TGCtrl.Run 0, "CreateProcess.vbs", "tg_test.exe -return 2"

As always, make sure that the job identifier (8) is unique. Give it a new id if (8) is already in use.

Save Schedule.vbs and then Load it.
After it runs once or twice, press the Stop button.
You should see something like the following:
Tick 5:41:49 PM
tg_test.exe -return 2 returned 2
tg_test.exe -return 2 returned 2
STOP: Schedule.vbs exiting...


If you see something similar to what’s displayed above, then you’ve successfully run the tg_test.exe program from a TaskGhost schedule.

In Part II of this chapter, we’ll learn a how TaskGhost finds programs to run, and a little bit about the tg_test program.
_________________________
Regards,
Luke Tomasello

Top
#1380 - 07/16/03 05:07 PM Re: Chapter 02 – Running Programs
Luke Tomasello Administrator Offline
Member

Registered: 09/17/00
Posts: 740
Loc: San Jose, CA., USA
Chapter 02 – Running Programs, Part II

How TaskGhost finds scripts and executable programs:

Most computer systems today have a PATH environment variable, to store information about the location of programs.
The environment variable is nothing more than a list of directories to check when looking for a program to execute. For example: C:\BIN;C:\WINDOWS;C:\WINDOWS\SYSTEM32;
TaskGhost uses the low-level Win32 facilities for Process Creation and these facilities rely on the PATH environment variable.

Please read the section on How TaskGhost Finds Programs (LINK HERE)

A little bit about tg_test:
The tg_test.exe program doesn’t perform useful work, but is instead included with TaskGhost for educational purposes. It is also used in the TaskGhost battery of regression tests.
In Part I of this Chapter, tg_test.exe was called with the -return switch followed by the value 2. I.e.:
tg_test.exe -return 2
Invoking tg_test.exe in this way will simply cause tg_test to exit with the return value of 2.
In the TaskGhost output listed above, you see: tg_test.exe -return 2 returned 2
This output is TaskGhost’s way of informing you that it launched the commandline “tg_test.exe -return 2” and that it generated the return value 2.

In Part III of this chapter, we’ll learn a how to launch: Documents, URLs, Media files, etc. using ShellExecute.
_________________________
Regards,
Luke Tomasello

Top
#1381 - 07/16/03 06:21 PM Re: Chapter 02 – Running Programs
Luke Tomasello Administrator Offline
Member

Registered: 09/17/00
Posts: 740
Loc: San Jose, CA., USA
Chapter 02 – Running Programs, Part III

How to launch: Documents, URLs, Media files, etc. using ShellExecute.

Similar to the way in which CreateProcess launches programs, ShellExecute launches documents. This facility is the same facility that is invoked when you double click a Word document, .JPG file, etc. When you double click a document or other data file, the Windows 'Shell' looks-up the program associated with the targeted document and launches it with the document as the argument.
For instance, when you double click a .DOC file, Windows Shell looks in the Windows Registry to find the program associated with .DOC. When it finds the program, in this case Word.exe, it then checks to see what Verbs are supported. (Verbs are just actions like 'Open", 'Print', etc.). The Windows Shell then launches Word.exe with the document clicked on and the appropriate verb; in this case, "open".

So, ShellExecute just gives us programmers a way to launch 'documents' in the same way the Windows Shell does. But ShellExecute is not limited to documents alone, it can also launch media files, URLs, browsers, etc..

We can use the ShellExecute.vbs script to call the ShellExecute() function, or we can call the ShellExecute() function directly. The advantage of using ShellExecute.vbs is that it handles errors for us.

Lets have a look at ShellExecute.vbs:
Code:
Sub main(args)

	Dim verb
	Dim file
	file = args
	verb = "open"
	
	Dim exit_code
	exit_code = TGCtrl.ShellExecute(0, verb, file, vbNullString)
	
	If exit_code = -1 Then 
		TGCtrl.Print(args & " failed to start!") 
	Else 
		TGCtrl.Print(args & " returned " & exit_code)
	End If
	
End Sub
You'll notice the 'Verb' here is hard-coded to "open". If you wish to use ShellExecute.vbs, then you will need to change the verb if you want to do something different, like "print".

You can try ShellExecute by going through the same steps as in Part I of this chapter, only use "ShellExecute.vbs" instead of "CreateProcess.vbs". Also you'll want to pass "ShellExecute.vbs" a document name and not an executable program. (An executable program will work however, it's just not very interesting.)

Experiment:

Try entering the following into Schedule.vbs and running it.
What do you think will happen?

If TGCtrl.CheckTime (8, tc, "* * * * * * * 0,15,30,45") = true Then TGCtrl.Run 0, "ShellExecute.vbs", "http://www.taskghost.com/"
_________________________
Regards,
Luke Tomasello

Top


Moderator:  Luke Tomasello