Creating ASP Objects with Delphi 5
Posted: (EET/GMT+2)
Creating ASP Objects with Delphi 5
Jan 21, 2001
Borland Delphi 5 Professional or Enterprise
Windows 2000 & IIS 5.0
If you are using Microsoft IIS as your web server, you have without doubt noted its support for Active Server Pages, or ASP. Having created COM objects with Delphi, you can easily call your components from ASP scripts. The idea is similar to building COM objects for use with PHP.
To start creating ASP objects, or ASOs (Active Server Objects) with Delphi 5, you should first read Chapter 49 in the Delphi 5 Developer's Guide. However, sometimes an illustrated walk-through suits the needs better.
To start creating ASOs, first choose the New command from Delphi's File menu. This opens the New Items dialog box. Click the ActiveX tab, choose the ActiveX Library icon, and click OK.

This creates a bare-bones ActiveX library code, as shown in the following code excerpt:
library Project1;
uses
ComServ;
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
{$R *.RES}
begin
end.
Although you haven't yet written any code, it is best to save the project at this stage. This will make the following steps easier to accomplish.
Creating an ASP object
To create an ASP object ove the library you just created, choose the File / New command again -- but this time choosing the Active Server Object icon on the ActiveX tab.

After you click OK, Delphi displays the New Active Server Object dialog box:

The Instancing and Threading Model options are not relevant here (see the Developer's Guide for details), but if you are using Windows 2000 and IIS 5 (Internet Information Server), you must choose Object Context as the Active Server Type. As for the CoClass name, our example uses BiolifeQueryObject, but you can use anything you wish.
After clicking OK, you should see the Type Library Editor:

Implementing the object
Since using the Type Library Editor has already been covered in the PHP article, we will simply go to the implementation of the method, GetFishNames. You've probably guessed it already, but the idea of the sample ASO is to return the names of the fishes in the Biolife example table. To support this, the example object has a datamodule that includes a single query component, set up like this:
object BiolifeQuery: TQuery
DatabaseName = 'DBDEMOS'
SQL.Strings = (
'SELECT common_name FROM biolife'
'ORDER BY common_name')
Left = 80
Top = 32
object BiolifeQueryCommon_Name: TStringField
FieldName = 'common_name'
Origin = 'DBDEMOS."biolife.DB".Common_Name'
Size = 30
end
end
Of course, to use the query, you need to include the datamodule to the Uses clause of the ASO class. After that, you can implement the GetFishNames method like this:
uses ComServ, DataModule;
procedure TBiolifeQueryObject.GetFishNames(var Result: OleVariant);
Var FishNames : String;
begin
DataModule1 := TDataModule1.Create(nil);
Try
FishNames := '';
With DataModule1.BiolifeQuery do Begin
Open;
While (Not EOF) do Begin
FishNames := FishNames+
FieldByName('Common_Name').AsString+'<BR>';
Next;
End;
Close;
End;
Result := FishNames;
Finally
DataModule1.Free;
End;
end;
After compiling the code, register the library to the system by using the Register ActiveX Server command on Delphi's Run menu.
Testing the object on Windows 2000
If you are using Windows 2000 Server and IIS 5 as your web server platform, there are several things you might need to do before testing your object. First and foremost, you must give enough privileges to the IUSR_machine account on your system.
Other than these security issues, you should also make sure that your application settings are set correctly in the Internet Services Manager (Start / Programs / Administrative Tools). By displaying the properties of the Default Web Site, you should see the following dialog box (after clicking on the Home Directory tab):

It is important that you choose the Medium (pooled) Application Protection when testing ASOs. Using Internal allows you to debug ISAPI applications, but once you use an ASP page to load your ASO into memory, it will stay there until you restart IIS (by the way, easiest way to restart IIS 5 is to use the IISReset.exe command-line utility).
By choosing Medium, you can use the Unload button (see previous figure) to unload your application from the IIS address space. If you don't do this, Delphi will not allow you to recompile your ASO since the DLL file is in use. Only an error message "Could not create output file" will result.
Once you have IIS 5 properly configured, it is time to test your object. To do so, you need to edit the ASP file Delphi has generated. The following code illustrates:
<HTML>
<BODY>
<TITLE> Testing Delphi ASP </TITLE>
<CENTER>
<H3> You should see the results of your Delphi Active Server method below </H3>
</CENTER>
<HR>
<%
Set DelphiASPObj = Server.CreateObject("MyASPObject.BiolifeQueryObject")
DelphiASPObj.GetFishNames Result
Response.Write Result
%>
<HR>
</BODY>
</HTML>
Of course, the most important point is to edit the ASP code between the <% and %> characters. After this, you can copy the ASP file to the wwwroot of IIS, and the use your browser to activate it. Results should look something like this:

Couldn't be much easier!
Final note about IIS 5
If are using ASOs to their fullest potential, you will quickly note the Request and Response objects provided by the TASPMTSObject class in the ASPTLB unit. However, if you are using the original version of Delphi 5, you can quickly run into the following error message:
HTTP 500.100 - Internal Server Error - ASP error Internet Information Services Error Type: (0x8000FFFF) Catastrophic failure /BiolifeQueryObject.asp, line 10
To resolve this problem, you will need to download the Update Pack 1 for Delphi 5. This can be found from the Borland Delphi support site, which also notes the problem (the GetObjectContext problem in the MTX unit). You can use the Help / About dialog box to see which version of Delphi you are using.
Download the example code
Download creatingaspobjectswithdelphi5.zip (292 kB) which contains the sample Active Server Object "MyASPObject". Please note that the sample application will require Delphi 5 Professional or Enterprise as well as Internet Information Server (IIS) 5.0 or compatible.
* * *
Need help developing web applications with Delphi? Contact us!