|
Creating ASP com objects with Delphi 5 Author: Jan Verhoeven (Article taken from http://jansfreeware.com, reproduced by permission.) 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.
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.
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.
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.
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; Contribute to IDR: To contribute an article to IDR, a click here.
|
|