If you want to break away from the level of ASP enthusiasts, then you should learn to write components for ASP. I searched on Google and found that there are only a few articles about writing asp components in Delphi, so today I wrote this basic article about writing asp components in Delphi. I hope it will be helpful to novices.
To get started, let's write a "hello world!" example. I'm using Delphi 7 here.
1.File->New->Other->ActiveX->ActiveX Library, then save it as showdll.dpr
2. Again, go to File->New->Others->ActiveX->ActiveX Server Object, fill in CoClassName: showhello, leave everything else unchanged, and click OK.
3. Now start writing the program and add a method first. Select Ishowhello->right-click->New->Method and fill in the method name: sayworld.
4. Now start writing the program, save Unit1 as show.pas, and then add the code of the method sayworld
The entire code of show.pas is as follows:
unit show;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, AspTlb, showdll_TLB, StdVcl;
type
Tshowhello = class(TASPObject, Ishowhello)
protected
procedure OnEndPage; safecall
; procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
procedure sayworld; safecall; //sayworld method
end;
implementation
uses ComServ;
procedure Tshowhello.OnEndPage;
begin
inherited OnEndPage;
end;
procedure Tshowhello.OnStartPage(const AScriptingContext: IUnknown);
begin
inherited OnStartPage(AScriptingContext);
end;
procedure Tshowhello.sayworld(); //Define the sayworld method
begin
response.Write('Hello world'); //The syntax and asp inside It’s written in the same way, just encapsulated here.
end;
initialization
TAutoObjectFactory.Create(ComServer, Tshowhello, Class_showhello,
ciMultiInstance, tmApartment);
end.
4. Click Run to compile it into a dll and register it automatically. At this time it will prompt:
Let you run it on the web server. Okay, now write an asp file and call it. Note that Delphi has already generated an asp file. We can just change the calling method.
The modified showhello.asp code is as follows:
<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("showdll.showhello")
DelphiASPObj.sayworld
%>
<HR>
</BODY>
</HTML>
Run it on the iis site to see the effect:
5. Others:
For components written in Delphi, after registering with win2000 component service, you can view the interface method of the component.
6. There are also parameters passed between asp pages and components. In fact, parameters are passed to the called methods (functions). Note that when defined in Delphi, they must be consistent with the data type of vbs. It’s better for everyone to practice these more. The main purpose here is to let everyone learn how to encapsulate the core code of asp, and to serve as a starting point.
There are not many words to write these, but taking screenshots is a bit troublesome. My level is limited. If I make a mistake, please slap it gently! ! ! !
One wind and one
cloud2004-10-18