Author: Daniel Wischnewski
How to create an ActiveX that can be used in Active Server Pages for dynamic
content?
Answer:
This article shows you how to create a server-side ActiveX library you can use
within ASP (Active Server
Pages). We'll keep it very simple.
CREATING THE ACTIVEX SERVER
First, start Delphi and/or close all current projects. Select File|New and change
to the "ActiveX" tab. Select the "ActiveX library" project type. AxtiveX libraries,
as this can be used for ASP. They are DLL that are implemented by the
Server.CreateObject (VBScript) directive.
An ActiveX library must export 4 routines, allowing it to be un-/registered, export
its objects and tell the calling process whether it may be unloaded. These for four
functions are provided through the ComServ unit, by Borland. You do not have worry
about them.
Next, we will create an "Active Server Object". You find these within the File|New
dialog on the "ActiveX" tab, too. You will see a dialog with quite a few options.
First we will have to enter CoClass Name. This is the name we will use when
accessing the object from within our ASP page. Let's call it "Sample". Now we have
to choose the "instancing model" and the "threading model". Choose "Single
Instance" and "Apartment" from the drop-down lists. (I will explain these models in
another article, later on. Drop me an e-mail if you want me to tell you when.) For
"Active Server Type" choose "Object Context", leave the check mark in the "Generate
template..." check box and press OK.
Delphi will create a new unit (unit1), an ASP (sample.asp), and a type library
(project1_tlb). Let's save these files now. (Choose save all and use the following
names: uSample.pas, Sample.asp, First.dpr). Goto to the menu and select View|Type
Library. This window shows all the objects with their methods and properties within
the type library we create. The root object is named "First" (if you've used the
same file names). This is your type library name, it must be unique on the machine
it is running. Underneath "First" you see two more entries (ISample and Sample).
One is the public dispatch interface declaration where all methods and properties
are names, the other is the object name that is used within the applications
accessing you ActiveX library.
Select the ISampe entry. In the tool bar you will see a green icon (New Method) and
a blue icon (New Property) to its right. These are the two we are going to check
out in this article.
First select new method. A new entry ("Method1) will appear underneath the ISample
entry. Change its name to ShowTime. Select the "Parameters" tab on the right and
add a new parameter to the list. Set its name to "TheTime", the type to "BSTR *".
Double-click the modifier with your mouse and select "out" and "retval". The "in"
box will be deselected automatically. It is not working with "retval" together. We
have declared the Parameter "TheTime" as WideString (ASO does not support Pascal
Type Strings) as a Return Value. Close the Box by pressing OK.
Save your project files (!), goto to your editor window and select the uSample
unit. Delphi has added the following method, automatically:
function ShowTime: WideString; safecall;
Since the parameter "TheTime" was declared as Return Value, Delphi created a
function with the Result Type
WideString. The safecall directive tells the Delphi Compiler to ensure the proper
DLL/COM wrapping for the
function.
Fill out the ShowTime function as follows:
1 function TSample.ShowTime: WideString;
2 begin
3 Result := FormatDateTime(
4 '"This function was called on " dddd, mmmm d, yyyy, " at " hh:mm AM/PM',
5 Now
6 );
7 end;
That's all for the Delphi code. Save your project and select Run|Register ActiveX
Server from your menu. The ActiveX server has to be installed on the machine where
it is used. Without Delphi you can do this by calling
regsvr32 "Drive:\Path\FileName.dll"
from the command line. Add the parameter /u to unregister the ActiveX server.
THE ACTIVE SERVER PAGE
Delphi has prepare the Sample.asp file already. we just have to fill in the missing
parts.
In our case we have to change the line
DelphiASPObj.{Insert Method name here}
into
Response.Write DelphiASPObj.ShowTime
"Response.Write" tells ASP to write the following text (provided by our function)
into the current place of our HTML code.
Copy the sample ASP into your web server directory (/test) and call it like:
http://localhost/test/sample.asp.
You should have installed either the MS Internet Information Server (IIS) or MS
Personal Web Server (PWS).
Good luck and stay tuned for more.
|