Delphi编写asp组件(一)

 如果你想脱离asp爱好者的水平,那么你就应该学会为asp编写组件。我在google上搜索了一下,Delphi编写asp组件的文章一共就几篇,所以今天写了这篇Delphi编写asp组件的基础文章,希望对新手有点帮助。如果你要学习VB编写asp组件的话,建议看看龙卷风大哥的文章(http://blog.csdn.net/online/category/9939.aspx),不是我可以比的。呵呵:)

开始吧,让我们一起编写一个”hello world!”的示例。我这里用的是Delphi 7。

1.文件->新建->其他->activex->activex Library,然后保存成showdll.dpr

Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图一)

点击查看大图



Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图二)

点击查看大图

2.再次,文件->新建->其他->activex->activex Server Object,填写CoClassName:showhello,其他不变,点击ok。
Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图三)


3.现在开始写程序,先添加一个方法。选择Ishowhello->右键->New->Method,填写方法名称:sayworld。

Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图四)

点击查看大图

4.现在开始写程序,将Unit1保存成show.pas,然后添加方法sayworld的代码
Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图五)
点击查看大图


show.pas的全部代码如下:

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方法

  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(); //定义sayworld方法

begin

 response.write('hello world');   //里边的语法和asp一样的写法了,就在这里封装了。

end;

initialization

  TAutoObjectFactory.Create(ComServer, Tshowhello, Class_showhello,

    ciMultiInstance, tmApartment);

end.

4.点击运行,编译成dll, 并自动注册了。这时候会提示:

Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图六)
让你放到web服务器上运行,好了现在写个asp文件调用一下吧,注意Delphi已经生成了一个asp文件,我们改一下调用的方法就可以了。

Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图七)

点击查看大图

修改后的showhello.asp代码如下:

<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>

iis的站点下运行看看效果吧:
Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图八)

点击查看大图


5.其他:

delphi编写的组件,用win2000的组件服务注册后可以看该组件的接口的方法

Delphi编写组件封装asp代码的基本步骤(Asp组件系列)(图九)

点击查看大图

6.还有asp页面和组件间传递参数,其实就是给调用的方法(函数)传递参数,注意Delphi里定义的时候要和vbs 的数据类型一致。这些还是大家多实践吧。这里主要是想大家学会封装asp核心代码的方法,起个抛砖引玉的作用。

申明

非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

博文欢迎转载,但请给出原文连接。

原文地址:https://www.cnblogs.com/Athrun/p/1127791.html