Library Zone Articles
External Articles
Byte Size

Discovery Zone Catalogue
Diary
Links
Bookstore
Interactive Zone Ask the Gurus
Discussion Groups
Newsletters
Feedback
Etc Cartoons
Humour
COMpetition
Advertising
Site Builder ASP Web Ring ASP Web Ring

WIN MS WINDOWS 2000 ADVANCED SERVER!!
The Developer's Resource & Community Site
COM XML ASP Java & Misc. NEW: VS.NET
International This Week Forums Author Central Find a Job

Creating ASP com objects with Delphi 5

(Article taken from http://jansfreeware.com, reproduced by permission.)

Download article and Code for printing

Creating ASP com objects, or Active Server Objects with Delphi 5 is very easy, once you know how to do it.

What follows is a step by step instruction for creating your first ASP com object.

  1. File - New - ActiveX - ActiveX Library
  2. File - New - ActiveX - Active Server Object
    • CoClassName=janaspdemo
    • Instancing=Muliple Instance
    • Threading Model=Both
    • Active Server Type=Page Level Events Model
    • Options= (checked) Generate a template test script for this object
  3. In the type libary editor select Ijanaspdemo
  4. Click Method on the toolbar and enter report as method name
  5. Select the parameters tab
  6. Click under Name and type strInOut
  7. Click under Type and select Variant*
  8. Click under Modifier, click the ellipsed and check In and Out in the flags popup
  9. Click Refresh on the toolbar

Move the type library editor window away so that you can see Unit1 which was generated by the type library editor

Press ctrl+F12 (view units) and select Project1_TLB

Have a look at the type library generated by the editor. You normally don't have to touch the Porject1_TLB unit, as the type library editor will do the changes for you.

View Unit1, positition the cursor on procedure report and press ctrl+shift+down to jump to the procedure report implementation. You can see that the type library editor has created an empty sceleton for you.

For the time being just type // implement later between the begin and end;

Switch back to the type library editor.

  1. In the type libary editor select Ijanaspdemo
  2. Click Property Read/Write on the toolbar
  3. Change the first property1 name to heading. The second porperty1 will be automatically changed to heading as well.
  4. Select the first heading, the one with modifier [out,retval] and long* as Type.
  5. Click under Type and select Variant* (we want the header to be a string, not an integer)
  6. Select the second heading, the one with modifier [in] and long as Type.
  7. Click under Type and select BSTR.
  8. Click Refresh on the toolbar.

Now first have a look in Project1_TLB and look below: Ijanaspdemo = interface(IDispatch)

See how the type library editor has added three lines:


function Get_heading: WideString; safecall;
procedure Set_heading(const Value: WideString); safecall;
property heading: WideString read Get_heading write Set_heading;

Move to Unit1 and have a look at the top part:


function Get_heading: WideString; safecall;
procedure Set_heading(const Value: WideString); safecall;

Note that you don't see a property here, just the Get_heading and Set_heading function and procedure.

Note

When adding methods or properties to an Active Server Object, always do this in the type library editor. This will create the code for you, both in Project1_TLB and in Unit1.

Put the cursor on function get_heading and press ctrl+shift+down to jump to the implementation part.

See that it is still empy. In this case I want both the get_heading and the set_heading to use a private field.

Go to the top and insert the private FHeading as shown below:


type
Tjanaspdemo = class(TASPObject, Ijanaspdemo)
private
FHeading:widestring;
protected
procedure OnEndPage; safecall;

You don't have to do this is you want to handle your header in a different way, but many times you just want to use a private field of the object.

Now go the Get_heading implementation and type:

result:=FHeading;

Go to the Set_heading implementation and type:

FHeading:=Value;

Now it is time to save your project.

  1. Press the Save All button on the IDE toolbar.
  2. Save the unit as janspdemoU.pas
  3. Save the asp template as janaspdemo.asp
  4. Save the project as janaspdemo.

Build the project and register the asp component with Run - Register ActiveX Server.

Your janaspdemo component is contained within the just generated janaspdemo.dll.

 

Modify the asp file

You are now ready to modify the asp template.

Open it with e.g. Notepad.

It contains the following:


<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("janaspdemo.janaspdemo") 
DelphiASPObj.{Insert Method name here}
%>
<HR>
</BODY>
</HTML>

Change Server.CreateObject("Project1.janaspdemo") to

Server.CreateObject("janaspdemo.janaspdemo")

Change DelphiASPObj.{Insert Method name here} to

DelphiASPObj.heading ="my first aspdemo heading"

Response.write DelphiASPObj.heading

Save janaspdemo.asp

We are not ready yet but first something important:

When you request an asp page in your browser from Personal Web Server, any dll loaded will remain in memory, even after you move to a different page, or after you closed your browser, or even after you stopped PWS. This means that when in Delphi you try to make changes and recompile you get a message telling you that the output file can not be created. This is because it has not been released from memory.

Can we then only recompile after we have restarted the computer?

How do I unlock a Active Server Library DLL?

At first I thought that the only way to unlock a ASP component DLL was to reboot the computer. There is however a better way. Create a new start menu shortcut:

c:\windows\system\inetsrv\pws.exe /stop /y

It is the /y that does the trick. Not only will it stop PWS, as does the stop button in the Personal Web Manager, but it will also unload the Inetinfo.exe process from memory, unlocking any locked ASP component library DLL's.

This way you can compile your ASP component with Delphi, start your web application and use the just created shortcut to unload the DLL from memory, allowing you to recompile from Delphi without rebooting your computer. A big time saver!

Unlocking Active Server Library DLL's under Windows NT

Type net stop iisadmin /y to shutdown the parent service of IIS, IIS Admin. This will also shut down FTP, STMP, and any other services that are children of IIS Admin. It will unload the inetinfo.exe process from memory. If you type only net stop w3svc, to unload just the Web server, inetinfo.exe will not be unloaded.

Type net start w3svc to restart the Web server.

So how do I test an asp component?

The way to go here is to create a new project specifically for testing your asp component.

Somethings just can't be checked within a test project but most routines can be checked and tested and it will save you a lot of rebooting.

Create a test application

As explained above, asp components created with server.createobject stay in memory until you shutdown your computer. This prohibits recompiling from Delphi. So the way out is a test application.

  1. Start a new Delphi project with File - New Application.
  2. Add a private field to the form: FVar:OleVariant;
  3. In the formcreate event handler include: FVar:=createoleobject('janaspdemo.janaspdemo');
  4. In the formdestroy event handler include: FVar:=Unassigned;

You can now add buttons,menu items, text boxes etc, to the form. Whatever you need to test the asp component.

You can set the heading with: Fvar.heading:="my first heading";

Or retrieve it with s:=Fvar.heading;


Click here

Contribute to IDR:

To contribute an article to IDR, a click here.

To contact us at IDevResource.com, use our feedback form, or email us.

To comment on the site contact our webmaster.

Promoted by CyberSavvy UK - website promotion experts

All content © Copyright 2000 IDevResource.com, Disclaimer notice

Code Project

WTL Introduction

Join the Developers Webring

WTL Architecture by Richard Grimes

Visit the IDR Bookstore!