Quick Start: How to write CGI programs in Delphi
[Contents]


To write a CGI program:

  1. Start with a new project
  2. In the Linker Options (under Project Options) select 'Generate console application'
  3. Put the following code in the OnShow event handler for the form:

close;

Your CGI program should perform any processing, and produce output, in the FormCreate handler for the form. You should create an instance of the CGI component in the FormCreate handler, ie:

procedure TForm1.Create(Sender: TObject);
var
cgi: TCGI;
begin
cgi:=TCGI.Create(Application);
{Get any values passed}
{perform and required processing}
{Send output}
end;

Your unit must include the file CGICOMP in its USES clause.

How to get the values of variables passed to the program

Lets say that your CGI program was called in the following fashion:

/cgi-bin/test.exe?name=Andrew&age=17&likes=computers&likes=food

You can use the GetFirstValue and GetNextValue methods to retreieve these values.

So GetFirstValue('name') would return 'Andrew'. If a variable has more than one variable (as in 'likes' above) use GetNextValue, ie:

GetFirstValue('likes'); //returns 'computers'
GetNextValue; //returns 'food'

When reading multiple values in this way the values must be read serially, that is the following will not work:

GetFirstValue('likes'); //returns 'computers'
GetFirstValue('name');
GetNextValue //returns nothing as 'name' has no second value

For more information about this see the documentaion for the GetFirstValue and GetNextValue methods.

How to send output

First you need to set the ContentType property. Use ctGIFImage if your CGI program is going to output an image or ctHTMLPage if your CGI program will be outputting a HTML page. Then, before doing anything else you must call the StartSend method. Then,