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
- Download and Install the CGI libraries
- Create a new project/application
- Modify the project settings to find the CGI Libraries
- Test Compile to see if Library is installed correctly
- Finish the CGI Code
- Compile and Test the application
- Build the Release Version
- 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.
- On your Desktop, make a folder called "request_component"
- Inside that folder, make 2 more folders, one called "lib", and the other called "include"
- Download the Req_Request header file - req_request.h - put it in the "request_component/include" directory you just made.
- Download the Debug Req_Request library - req_20b_visual_sthread_debug.lib (1.0MB) - put it in the "request_component/lib" directory.
- 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.
- From the Menu, go to File -> New...
- On the dialog that pops up, click on the Projects tab
- Highlight Win32 Console Application
- Under Project name, type "Req Hello World"
- The Location: should now read:
C:\Program Files\Microsoft Visual Studio\MyProjects\Req Hello World
- Click OK
- when it asks you what kind of Console Application you want to create, select:
A "Hello World" application
- 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.
- From the Menu, click Tools -> Options...
- 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.
- In the Directories section, there is a drop down list under Show directories for:, make sure Include files is selected
- 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.
- 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)
- From the Menu, click Project -> Add To Project -> Files...
- 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)"
- 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
- 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)
- You should see a little File explorer tree, look under the Source Files folder and double click on "Req Hello World.cpp".
- 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.
- Add the following two hightlighted lines to the source code:
// Req Hello World.cpp: Defines the entry point for the .....
#include "stdafx.h"
#include "req_request.h"
int main(int argc, char* argv[])
{
Req_Request *parser = Req_Request::instance();
printf("Hello World");
return 0;
}
|
Now we'll do a test compile just to make sure the header is included alright.
- Type [CTRL] + F7 to compile the program. If there are no errors, then the include file has been found and everything is fine.
Now we'll do a test Build to make sure the library has been included.
- Type F7 to build the program. If there are no errors, then the library is working fine.
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...
- Add the following hightlighted lines to the source code:
// Req Hello World.cpp: Defines the entry point for the .....
#include "stdafx.h"
#include "req_request.h"
int main(int argc, char* argv[])
{
// Output response headers
//
printf("Content-Type: text/html\n\n");
Req_Request *parser = Req_Request::instance();
printf("Hello World");
return 0;
}
|
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.
- Add the following hightlighted lines to the source code:
// Req Hello World.cpp: Defines the entry point for the .....
#include "stdafx.h"
#include "req_request.h"
int main(int argc, char* argv[])
{
// Output response headers
//
printf("Content-Type: text/html\n\n");
Req_Request *parser = Req_Request::instance();
// print out the start of the HTML page
//
printf("<HTML><BODY>\n");
printf("<TITLE>");
printf("Hello World");
printf("</TITLE>");
// Extract data that was submitted by the web browser and print it out
//
printf("First Name: %s<br>\n", parser->value("fname"));
printf("Last Name: %s<br>\n", parser->value("lname"));
printf("Phone Number: %s<br>\n", parser->value("phone"));
// print out the bottom of the HTML page
//
printf("</BODY></HTML>\n");
return 0;
}
|
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