Site hosted by Angelfire.com: Build your free website today!

Visual Basic Guestbook CGI

What is VBGB?

This is a simple web application constructed in the form of a CGI program.  The program is written in Microsoft's Visual Basic.  The application is a very simple web site guestbook, thus the name Visual Basic Guestbook, or VBGB.

While VBGB is functional and could serve as the basis for a nice basic guestbook for your web site, its real purpose is tutorial.  It was meant to serve as an example of how one can do web application development in Visual Basic for web servers other than Microsoft's IIS.  This version is written in Visual Basic 6.0, but could probably be adapted to VB 5.0 or possibly even VB 4.0 in 32-bit mode.

Why not IIS?

IIS, Microsoft's Internet Information Server, is a perfectly fine web server.  Quite a powerful one at that.  With great power comes great responsibility however, and IIS is no exception.

Configuring and operating IIS in a manner safe from attacks of various types requires considerable knowledge and care.  Because of its ease of basic use and its power, IIS is very popular.  That very popularity tends to make it a target of those with ulterior motives.  I don't consider IIS the best choice for home or casual use for this reason.  It is frequently compromised in unsophisticated settings.

Alternatives?

There are actually quite a few.  One of IIS' attractions is the fact that it is bundled with most NT-based Windows operatings systems.  Even Windows 9x has a free Personal Web Server (PWS) with some of the same capabilities as full IIS - along with some of the same vulnerabilities.

Apache

This is a very popular alternative.  It is available as open source and totally free distributions are available for Windows and many other platforms.  It can be as difficult to properly secure and administer as IIS without experience though, and getting a basic configuration up and running is a little more effort.  It also doesn't have the popular ASP web application infrastructure available intrinsically.  The PHP alternative is readily available as an add-on however, and is quite popular among web developers.

Apache is still a good alternative to IIS on Windows, and is probably the number 2 web server for Windows today.

Many others

It doesn't take much of a search of the web to locate a number of other free or inexpensive web servers for Windows.  PHP and other scripting is often supported by these web servers, but ASP is seldom to be found.

My current pick

Right now for casual use on a Windows machine I recommend SimpleServer:WWW from AnalogX.

This web server is trivial to install, and works great for many puposes.  It runs as a user program rather than a Windows service, which can be limiting in a production web server but should not be an issue for development activities or lightweight casual hosting.

It supports CGI for web applications, and in theory should support the CGI version of the PHP package.  My interest in is CGI programs written in VB though, and it works great for this!  Just what I was looking for: a lightweight, secure web server I could write applications for using something I already understand: Visual Basic.

You could also use Apache or any of the many other alternatives - including IIS.

What is CGI?

CGI stands for Common Gateway Interface.  I believe it was intended as an interface between the web server and a gateway application that served to connect the web server to other facilities on the server platform such as databases or existing non-web applications.

Overview

Your program is the gateway.  This diagram shows these relationships:

Gateway diagram

In many web applications today there is no existing "legacy" application.  In such cases the Web Gateway and the Application are one.  In a sense the Gateway is gone, and your application uses the gateway interface to connect between itself and the web server.

ASP and PHP both provide interfaces strongly based upon CGI.  As you begin working with CGI in VB you will notice many similarities to both of those web application environments if you have prior experience with them.

What's the Interface Like?

A program written for CGI is executed as a normal console executable with a hidden console window.  It sounds pretty mundane, and it really is.  The idea was to exploit simplicity.

The CGI program gets fed input by the web server through three paths:

Of these the command line is used only rarely in most web applications.  For most CGI programming you can ignore it.

Output is sent back to the web server through two mechanisms:

Standard I/O?

A common metaphor in console programs is Standard I/O.  Unix has it, Windows has it, and many other environments offer it.

In most cases there is a "standard input" (stdin) and a "standard output" (stdout) stream that normally are used to interact with a console or terminal user.  These can typically be redirected or piped to files or the inputs or outputs of other programs.

To permit the program to communicate status or error information back to a user who may have stdout piped someplace, there is usually another output stream.  This is usually called "standard error" (stderr).

Standard I/O in VB?

Visual Basic (for Windows) does not provide a standard I/O facility!  VBDOS does of course, but that's another topic altogether.

To get the standard I/O streams in VB 5/6 and to read and write to them we need a few Win32 API calls.  These are simple enough, and examples can be found in VBGB's modCGISupp.bas module.

Environment Variables?

Yes, there are the same old environment variables we all know and love from our DOS days.  In VB you can get at these values via the Environ$() function.

VB Console Executable?

Yes, you read it correctly.  A CGI program must be a console executable.

VB is capable of creating these, but works very hard not to.  The IDE makes no provision to select the Windows subsystem (of which "console" is just one) you generate code for.  There are third-party add-ins that offer this extended functionality in the IDE.

I do it the way most others do: edit the compiled EXE using the LINK.EXE program that comes with VB.  This is fairly simple:

LINK /EDIT /SUBSYSTEM:CONSOLE {exe file name}

Actually I have taken to using a WSH script to do this for me.  I call it LinkConsole.vbs and it is included with VBGB.  This script is handy, because after compiling (or recompiling) my VB EXE, I can just drag the EXE's icon and drop it on LinkConsole's icon.  No more messing around typing long pathnames into console windows to run LINK.EXE!

Note that unless this step is done, your VB console (or CGI) programs won't work.

But VB Creates GUI programs

Not necessarily!

Think of the various project types available in VB.  Not all of them use forms.

To write a VB CGI program you start with a Standard EXE project.  Then you remove the "free" Form the IDE's template provides.  Next you add in a Module.  Finally you enter a Sub Main() in that module and check the project's properties to be sure Sub Main() is set as the "startup object."

Main() is where your program will begin execution.

When putting your CGI EXE "into production" you may want to make sure you check Unattended Execution in the project properties as well.  This keeps error dialogs and MsgBox's and such from trying to pop up and "hang" your CGI.

Where do these alerts go then?  Well on an NT-based system they go into the Application Event Log.  In Windows 9x they default to a file - in the Windows directory someplace if I recall correctly.

This means you may well want to include an App.StartLogging {path/file}, vbLogToFile call at the start of Main(), to control where errors are reported.  You can also use the App.LogEvent method to log information and error messages of your own there.  You can write to stderr to put information into the web server's log.

Note:  No forms, no events.  The event-driven programming we've grown accustomed to is tightly coupled to the VB Forms mechanism.  DoEvents is useless.

You're Leaving a Lot Out

Yes, I am.

There are a lot more details to CGI, to be sure:

The answers to these and more questions are needed before you can write a CGI program.  The best place to find answers is in sources such as CGI: Common Gateway Interface.

VBGB in a Nutshell

VBGB is a simple guestbook application.  It is written in VB6 as a CGI program, and stores control information and guestbook entries in a Jet 4.0 MDB file.  An MS Access 2000 database, if you will.

But please note that you do not need Microsoft Access installed to run this application.

Functions:

Pretty basic.  Browse/view/sign the guestbook are done in one page.  An admin logon is a second page, and the administrative actions form is a third page. 

Here is the flow of actions among the pages:

Flow diagram

VBGB Architecture

VBGB consists of several standard modules:

The "page" modules are largely made up of two subroutines.  One of them is used to return HTML to the browser, the other is used to process input or actions from the browser on those pages.

Downloading VBGB

The download package is here.  A simple zip file with a version of this article and the VB 6.0 project files, etc.

Building VBGB

VBGB comes with a project file VBGB.vbp that you can open in the VB6 IDE.  This project has all of the options set to compile a "production" unattended CGI EXE.

The build steps are:

That's it!

Installing VBGB

I'll assume you have installed SimpleServer:WWW and have already set its web root directory.  You will find some interesting entries under registry key:

HKEY_CURRENT_USER\Software\AnalogX\SimpleServer\WWW

The most useful ones to alter are probably those dealing with startup behavior, logging, and the port number for the server.  The latter is especially important if you are already running IIS, Apache, etc. on port 80.

You'll also need the VB6 runtime installed on your server machine, but no other special components need to be installed or registered.

Installation

This part is easy:

  1. Navigate to your web root via Windows Explorer.
  2. Create a folder named cgi-bin.
  3. Open the folder, and within it create a folder named vbgb.
  4. Copy the compiled, reLINKed vbgb.exe as vbgb.cgi into cgi-bin.
  5. Copy the provided vbgb.mdb into cgi-bin\vbgb.

Done!

If you recompile and reLINK, you need only recopy the new program over the old vbgb.cgi - unless you have made changes that require database alterations as well.

Note: You can put the program into cgi-bin as vbgb.exe if you choose.  It will work fine.

Configuration

This is easy too:

http://localhost/cgi-bin/vbgb.cgi

... or use "localhost:8080" or (if on another box) the IP address or Windows Network name of the machine, DNS name if it has one, etc.

You're done.

George?

If you want to change the admin ID from George to something else, open the database in Access and change it there.  This will be easy to find, there are only two tables.

Better still, modify VBGB to offer you the option to change the ID via your browser.

Conclusion

Using VBGB should be pretty self-explanatory.  Users will seldom venture off the "Sign In" page.  There they can sign the guestbook and read others' comments.

There isn't much to administering the application either.  Periodically you may want to delete old entries, and you can do this from the Admin page.   Occasionally you may want to do a "compact and repair" either via Access or the free Jetcomp.exe utility from Microsoft.

Things to Look For, to Try

Take a good look through the source code.  There are lots of ways to improve VBGB.

Dig into how VBGB manages state for a logged on admin user.  No "Session" object.  No cookies used.  Take a look at how Form and Query String parameters are handled in VBGB.

How can writing the response back to stdout be improved?  Is there a better way to embed the bulky HTML strings in a VB program?

VBGB uses local time, but the web is normally all GMT internally.  How can you handle GMT and time zones better in a VB program?

Jazz the pages up.  Add a stylesheet, some graphics.  Links back to your main site.  Host the application in a frames page on your main site.

Your Own Projects

What sorts of applications can you build as VB CGIs?  If it can go into a database, you can probably build a web front-end to it using VB CGI.

How would you "source" dynamic graphics from a VB CGI program?  Say, a graphic hit counter?  Maybe dynamically generated charts?

What about a program to accept file uploads?  Serve dynamic downloads, perhaps of files pulled from a database?  Generated on the fly (RTF?)?

You can take much of what is out there in the ASP world and port the concepts if not much of the code to VB CGIs.  And you don't have to be bound to IIS if you don't choose to.  No Perl or PHP required, either.

January 2005