The article How to Create a Simple VBScript CGI Web Page shows just how easily the web application developer can use VBScript as a CGI (Common Gateway Interface) programming language. The techniques is quite simple:

  • one VBScript file (e.g. call_hello_world.vbs) contains the shebang line (the essential CGI line that identifies the location of the script interpreter). This shebang line also calls a second VBScript file
  • the second VBScript (e.g. hello_world.vbs) contains the actual CGI code.

However, there is one (slight) drawback to this technique: two files are needed for each CGI script (the CGI code file and the calling file). A much better solution would be for:

  • the shebang line of the VBScript CGI file calls a header file
  • the header file then returns control to the calling VBScript file

Two files will still be needed, but at least the same shebang line can be used in every CGI file rather than a different shebang line for each VBScript file.

An Example VBScript CGI Script

The first line of the CGI file must be the shebang line. This calls the “header.vbs” file which, in turn, returns control to the calling CGI file:

#!c:\WINDOWS\system32\cscript.exe /nologo "C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\header.vbs"

Next the obligatory HTTP header must be sent to the web browser:

wscript.echo "Content-type: text/html" & vbcrlf

And then the actual CGI code can be added:

wscript.echo "<h1>Hello World</h1>"
wscript.echo "This is is an output from VBScript"

If this code is saved into the server’s cgi-bin directory (as, for example, “hello.vbs”) then it is almost ready to use. The next step is to create the “header.vbs” file.

Passing Control Between VBScript CGI Scripts

The VBScript CGI technique is quite simple:

  • a VBScript CGI file calls a second VBScript file (in this example it’s called “header.vbs”)
  • the second VBScript then opens the original and processes its contents

Therefore the first job that the second file must do is to create a File System Object:

Set fso = CreateObject("Scripting.FileSystemObject")

That’s then used to open the original file (the name of which is passes to the new file as an argument):

Set original = FSO.OpenTextFile(WScript.Arguments(0))

Once the original file is open then its contents can be processed. Of course, the first line is the shebang line, and so that is simply read in (since it’s already been processed by the web server):

firstline = original.ReadLine

The remainder of the file is then executed by using VBScript’s ExecuteGlobal method:

ExecuteGlobal cgi.ReadAll

If the “hello.vbs” file is now entered as a Url into a web browser( for example http://localhost/cgi-bin/hello.vbs) then the VBScript will be run on the server and the HTML from the code will be displayed in the web browser (as shown in Figure 1 at the bottom of this article).

One final advantage to using this technique is, of course, that any additional processing that is required (such as handling the GET and POST queries) can be placed in the “header.vbs” file and that will automatically be passed on to of the CGI files that use it. And so, the VBScript programmer can smoothly and effortlessly glide into the world of the CGI web page.