home

Visual C++ CGI Tutorial

HELLO WORLD

Introduction

In this tutorial, I'm going to take you through the steps you need to take to create a simple CGI Application using Microsoft Visual C++ 6.0 and the Req_Request CGI library.
NOTE: This tutorial is not complete yet - I'm presenting what I have so far for those of you who need to know as much as possible right away

Tutorial Contents

  1. Download and Install the CGI libraries
  2. Create a new project/application
  3. Modify the project settings to find the CGI Libraries
  4. Test Compile to see if Library is installed correctly
  5. Finish the CGI Code
  6. Compile and Test the application
  7. Build the Release Version
  8. Install the Application


1. Download and Install the CGI libraries

The first thing you need to do is grab the correct CGI libraries for this project. We're using Microsoft Visual C++ 6.0 for this, so we need to download the Visual C++ libraries from this site. We also need the header file with the class definition in it so our application knows how to communicate with the Library. Before we get these files,we need to make a directory structure to hold the files so we're on the same page throughout the tutorial.
  1. On your Desktop, make a folder called "request_component"
  2. Inside that folder, make 2 more folders, one called "lib", and the other called "include"
  3. Download the Req_Request header file - req_request.h - put it in the "request_component/include" directory you just made.
  4. Download the Debug Req_Request library - req_20b_visual_sthread_debug.lib (1.0MB) - put it in the "request_component/lib" directory.
  5. Download the Release Req_Request library - req_20b_visual_sthread.lib (353.5KB) - put it in the "request_component/lib" directory.


2. Create a new project/application

Start up Visual C++. We're going to make a console application, which is what CGI applications have to be in order to work properly.
  1. From the Menu, go to File -> New...
  2. On the dialog that pops up, click on the Projects tab
  3. Highlight Win32 Console Application
  4. Under Project name, type "Req Hello World"
  5. The Location: should now read:

    C:\Program Files\Microsoft Visual Studio\MyProjects\Req Hello World

  6. Click OK
  7. when it asks you what kind of Console Application you want to create, select:

    A "Hello World" application

  8. Click finish, then click OK


3. Modify the project settings to find the CGI Libraries

Before we can use the libraries and the include file we downloaded, we need to tell Visual Studio where to find them.

First, let's add our request_component/include directory to Visual Studio's search path so it can find the req_request.h header file.

  1. From the Menu, click Tools -> Options...
  2. Click on the left and right arrows in the upper right hand corner of the dialog box until you see a tab labelled Directories - click the tab.
  3. In the Directories section, there is a drop down list under Show directories for:, make sure Include files is selected
  4. Now we add our include directory. In the huge "Directories:" window, click under the last entry in the list. This will allow you to enter another directory. Browse to your Desktop ( Usually either C:\Windows\Desktop or C:\Documents and Settings\[your login name]\Desktop ) and then continue browsing to the request_component/include directory. This is the directory we want to add here.
  5. Once the include directory is in the list, click OK to close the dialog.
Second, we need to add our Debug version of the Req_Request library to our project (req_20b_visual_sthread_debug.lib). Don't add both libraries - just add the debug one. (We'll swap libraries in step 7 when we build our release version)
  1. From the Menu, click Project -> Add To Project -> Files...
  2. On the dialog box that opens, there is a drop down list labelled "Files of Type". Click the drop down arrow and select "Library Files (.lib)"
  3. Navigate to your desktop, then continue navigating to "request_component/lib", and you should see the two library files we downloaded earlier. If you can't see them, either you didn't put them in there, or you have the "Files of Type:" selection wrong. Anyway, once you see them, select the debug library and click OK.


4. Test Compile to see if Library is installed correctly

Let's open up the source code file to our application - Req Hello World.cpp
  1. Click open the File View Tab in case its not already open
    (If you don't see a File View Tab, type [ALT] + 0 on your keyboard)
  2. You should see a little File explorer tree, look under the Source Files folder and double click on "Req Hello World.cpp".
  3. If the source file wasn't already visible, it is now....

Now we'll add a few lines to the source code so that the application uses the Library.

Now we'll do a test compile just to make sure the header is included alright.

Now we'll do a test Build to make sure the library has been included.


5. Finish the CGI Code

Now that we know the programming environment is set up properly, we can concentrate on coding. The first thing we need to do in order to make this a valid CGI application is add relevant response headers as output. These are required by the Web-Server, which expects the program to be responsible for telling the client (web browser) what kind of data it is receiving. These response headers should be the first thing that your application prints to stdout. In most CGI apps, we are returning text/html. So, let's add the reponse header that says that...

Finally, let's pretend that we are going to submit a form that asks for a person's first name, last name and telephone number. we are going to extract the information and print it out with this program.


6. Compile and Test the application


7. Build the Release Version


8. Install the Application

Copyright © 2002, 2003 Matt Flood and RudeServer.com - All rights reserved