asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Active Server Components with Borland Delphi3
By Venkat
Rating: 3.8 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction


    Even though Delphi is been around for quite sometime, it has not become as popular as it should have become for building Active Server components. This is remarkable considering Delphi’s good support for building COM objects. The examples in this article use Delphi 3.02.

    In this article we will look into Delphi’s support for building Active Server components. Since any automation server can be used as an ASP component, the same methods that are used to develop an automation server can be used to develop the components. Delphi makes creation of these components very easy, almost trivial. It provides a good IDE where you can add methods and properties to your objects. You can also do the registration of the components within the IDE itself. Internally, it will create the required IDL files and generates TLB files for you. We will not look into how Delphi provides this support. For instance, we won’t examine the intricacies of Delphi’s implementation of automation servers. Rather, we will concentrate on the steps that are required to develop a simple ASP component that can be used to add additional functionality to your ASP page. Specifically, we will be looking into developing an inproc ASP component.

    The simple component that we are going to has four properties, and one method which will display these properties on the browser in a neat table.

    Creating an Active Server Component in Delphi can be accomplished in the following steps.

    1. Create the ActiveX library project.
    2. Add an automation object to the project library.
    3. Add the required methods and properties to the object.
    4. Implement the methods inside a unit that Delphi creates for you.
    5. Compile and register your component.
    We will look into each of the above steps in detail. In Step 1 you’ll learn how to create an ActiveX library using Delphi.

    Step 1 : Creating the library

    1. Open Delphi IDE
    2. Select File| New. Select the ActiveX tab
    Figure 1 : New Items Property Sheet

    Select the ActiveX Library Option and click on it. That will create the project file for you. The unit contains the following code.

    
    
    library Project1;
    
    uses
      ComServ;
    
    exports
      DllGetClassObject,
      DllCanUnloadNow,
      DllRegisterServer,
      DllUnregisterServer;
    
    {$R *.RES}
    
    begin
    end.
    
    
    This is the file where Delphi implements the required functions that are to be exported from all COM servers. The implementation of these functions is done in the ComServ unit. If you want, you can override the default implementations of these functions. But, for this example, you can use these defaults.

    Step 2 : Add an Object to the Library

    1. Select File | New and click the ActiveX tab and select the Automation object and click it. This will display the Automation Object Wizard.

      Figure 2 : New Items Property Sheet

    2. Enter the name of the object as SimpleObject and Select Multiple Instance option. This will bring up the Type Library editor

      Figure 3 : Automation Object Wizard

      Figure 4 : Project Type Library

      The above steps will create two units. You can see them by pressing Ctrl+F12 and selecting the unit. One unit (Project1_TLB.pas) is the Delphi version of the type library created for the server and the other unit(unit1.pas) is the unit where you will implement your object’s methods and properties. Open the unit1.pas file. At the end of this unit you can find the following piece of code:

      
      initialization
        TAutoObjectFactory.Create(ComServer, TSimpleObject,
      	Class_SimpleObject, ciMultiInstance);
      end.
      
      
      This is where the ClassFactory implementation for your object is done.

      Step 3 : Adding Methods and Properties to the Object

      Click on the method button on the toolbar and type the name of the method. Similarly, click on the property button on the toolbar and type the name of the property. You can change the type of the property to any of the Automation compatible datatypes. For this component we are going to use all WideString type properties. Refer to Delphi’s documentation for a description of all of the Automation compatible datatypes. In the status bar of the Type library editor Delphi displays the error information about what you are typing in the editor. Add the following properties to your object. Name, Address, State, Country. Give the data type as WideString to all the properties. Similarly, add a method named WriteInfo.

      And finally add the following two methods to the component.
      OnStartPage(unk:IUnknown)
      OnEndPage.

      We use these two methods to get the IScriptingContext interface. When IIS creates a component instance, it looks for the OnStartPage and OnEndPage methods associated with that component. If the component has implemented these methods, the server will automatically call the OnStartPage method during script processing before the component is used, and call the OnEndPage method when all scripts on the ASP page have been processed.

      Your TypeLibrary editor should look like this.

      Figure 5 : Project Type Library

      These operations added three methods and four properties to your object. You can see the changes in the two units that are created. You don’t have to worry about the IDL that is required for this. Delphi will manage the process of converting it to the required IDL format. A default skeleton code for the implementation of these methods and properties is also added in the unit(unit1.pas). Whenever you add a new method or property, Delphi will automatically add an empty function implementation code in the unit. But when you remove a method or property it won’t remove them from the unit. You have to manually remove them. Observe that Delphi automatically assigns the dispatch IDs for all of the properties and methods

      Step 4 : Implementation of the Components Methods and Properties

      Open the unit1.pas. Add the ASPTypeLibrary_TLB to your uses clause of the interface section. Your Uses section should look like this.

      
      
      unit unit1;
      
      interface
      
      uses
        ComObj,
        ActiveX,
        Delphi_TLB,
        ASPTypeLibrary_TLB,
        SysUtils;
      
      
      Now go to the definition of the class that implements your interface. You can see that your class is inherited from TAutoObject and ISimpleObject. ISimpleObject is the interface where your methods and properties of the component are defined. This interface is created for you by Delphi.You can see the definition of this interface in the Project_TLB.pas file.TAutoObject provides the implementation required for an Automation Object.

      Add the m_scriptContext, m_Name, m_Address, m_State, m_Country member variables to your class as shown in the code segment below.

      
      
      type
        TSimpleObject = class(TAutoObject, IExample)
        protected
          m_scriptContext : IScriptingContext;
          m_Name,
          m_Address,
          m_State,
          m_Country : String;
          procedure WriteInfo; safecall;
          procedure OnEndPage; safecall;
          procedure OnStartPage(unk: IUnknown); safecall;
          function Get_Name: WideString; safecall;
          procedure Set_Name(const Value: WideString); safecall;
          function Get_Address: WideString; safecall;
          function Get_Country: WideString; safecall;
          procedure Set_Address(const Value: WideString); safecall;
          procedure Set_Country(const Value: WideString); safecall;
          function Get_State: WideString; safecall;
          procedure Set_State(const Value: WideString); safecall;
        end;
      
      
      Now go to the place in the unit1.pas file where the code for implementation is given. Change the implementation to look like the code segment given below
      
      
      procedure TExample.OnStartPage(unk: IUnknown);
      begin
         m_scriptContext :=  unk as IScriptingContext;
      end;
      
      procedure TExample.OnEndPage;
      begin
         m_scriptContext := nil;
      end;
      
      
      By now you must have observed that Delphi creates corresponding get_property and set_property functions for each property you add to the component in the TypeLibrary editor. Now go to the place where the code for implementing these methods is written. Change the code to look like this. I have given an example for just one property implementation.
      
      
      procedure TExample.Set_Address(const Value: WideString);
      begin
         m_Address := Value;
      end;
      
      function TExample.Get_Address: WideString;
      begin
         Result := m_Address;
      end;
      
      
      On these same lines you can implement the rest of the property access. You can download the complete source.

      Finally, go to the place where the implementation for the WriteInfo method is located and change it to look like the code segment below. The code simply takes all of the property values and using the IScriptingContext interface writes to the browser using the Response object.

      
      
      procedure TExample.WriteInfo;
      begin
         m_scriptContext.Response.Write('<TABLE>');
      
         m_scriptContext.Response.Write('<TR><TD>UserName</TD>');
         m_scriptContext.Response.Write('<TD>'+m_Name+'</TD></TR>');
      
         m_scriptContext.Response.Write('<TR><TD>Address</TD>');
         m_scriptContext.Response.Write('<TD>'+m_Address+'</TD></TR>');
      
         m_scriptContext.Response.Write('<TR><TD>State</TD>');
         m_scriptContext.Response.Write('<TD>'+m_State+'</TD></TR>');
      
         m_scriptContext.Response.Write('<TR><TD>Country</TD>');
         m_scriptContext.Response.Write('<TD>'+m_Country+'</TD></TR>');
      
         m_scriptContext.Response.Write('<TABLE>');
      
      end;
      
      
      Save the Project as Delphi.dpr and save the unit as SimpleObject.pas. From the Project menu select the BuildAll option to compile and build the DLL for the component. If you get an error during this stage it will likely happen because of the ASP type library that we are using in the application. So go to the Project menu and select the import type library. This will list out all the type libraries present in your system. Select the Microsoft Active Server Pages Type library and click on OK. This will bring up the Delphi equivalent of the ASP type library. Then rebuild the component.(Project|Build All). If it compiles without any errors go to the next step

      Figure 6 : Import Type Library

      Step 5 : Registering the Server

      Go to Run | RegisterActiveX Server. This will register the component.

      Now you can use the component with the ProgramID Delphi.SimpleObject.

      You can call the component in an ASP page using

      
        Set ObjDelphi = Server.CreateObject(“Delphi.SimpleObject”)
      
      
      You can use this component just like any other component. A sample HTML and ASP file that uses this component can be downloaded here.

      Conclusion

      We have looked into the steps for creating a simple ASP component from Delphi and the implementation of its methods and properties. You can extend the same procedure to create more complicated and functional components. Delphi manages all the hard work of writing the IDL files and generating the skeleton code for implementing the methods and properties. Therefore, you can concentrate on implementing your business logic in the component. I have shown how to access the ASP type library from Delphi. Likewise, if you want to add the database interaction for your components, you can import the Active Data Objects type library and use it.

      Download

      The source presented in this article is available for download.
      [Code Source Download]

      About the Author

      Venkat is currently working at Icode Software Private Ltd (http://www.icode.com). Venkat has been working in ASP, Delphi from the past one and half years. His interests as far as software is concerned revolves around component based technologies and eCommerce based applications. He was educated at Nizam College, Hyderabad and later obtained a MCA from KREC Surathkal both in India.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Apr 27, 2004 - Develop and Customize Web Parts with Custom Tool Parts
    Tool Parts provide an interface for Web Part properties well beyond the capabilities of the default property pane. In this article Gayan Peiris shows how to customize Web Parts with custom Tool Parts.
    [Read This Article]  [Top]
    Apr 7, 2004 - Reusable Components in ASP.NET 2.0, Object Binding and Precompilation
    This article demonstrates how to create a reusable component in ASP.NET 2.0 and then consume it from an ASP.NET page. Also learn how the ObjectDataSource control can be used to directly bind the output of an object to the controls in an ASP.NET page and how precompilation can be used to increase the performance of the Web application and catch compilation errors.
    [Read This Article]  [Top]
    Mar 31, 2004 - Build a Managed BHO and Plug into the Browser
    Browser Helper Objects (BHOs) are COM components that communicate with Internet Explorer to enrich the browsing experience. Michele Leroux Bustamante returns to the world of COM to show you how to build a managed BHO with the help of the .NET Framework's COM interoperability features.
    [Read This Article]  [Top]
    Feb 18, 2004 - Customizing SharePoint Web Parts with Custom Properties
    In addition to creating custom Web Parts for SharePoint Portal Server, developers can actually create their own custom properties to further enhance Web Part appearance and behavior. Gayan Peiris explains the process and provides all the necessary code.
    [Read This Article]  [Top]
    Sep 26, 2003 - Accessing Shared Resources Using ASP.NET
    Accessing shared resources is a challenge for many ASP.NET developers. Tony Arslan explains how a simple serviced component can solve this infamous problem.
    [Read This Article]  [Top]
    Oct 2, 2002 - Function Pointers and COM
    Using callbacks and function pointers in VB can be risky and complicated. Ben Garcia explains his work-around for the function pointer issue he encountered while creating the VB version of his SNMP component.
    [Read This Article]  [Top]
    Sep 4, 2002 - Creating an SNMP Component - Part 2
    In part two of this intriguing article series, Ben Garcia shows how to build an updated and improved SNMP component in VC++ AND VB, and he briefly explains why limitations in VB make VC++ a better language for developing this type of application.
    [Read This Article]  [Top]
    Jul 23, 2002 - Creating an SNMP Component
    Ben Garcia sheds some light on the Simple Network Management Protocol (SNMP). First he provides a history of SNMP, then he dives right into its architecture. Finally, he shows how to build a COM component that communicates with SNMP-enabled devices.
    [Read This Article]  [Top]
    Jun 26, 2002 - Accessing Caller ID from the Web - Part 1
    Paul Apostolos begins his series on using Web services and the MSComm32.OCX component to access caller id information from a Web page. In part 1, learn how to write the Visual Basic program that runs on the server and updates a database with incoming callers.
    [Read This Article]  [Top]
    Nov 20, 2001 - Creating a Server Component with VB - Redesigned - Part 2
    Doug Dean explains different methods of retrieving and manipulating data from a database in a VB DLL so that it is ready to be rendered in a browser.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry