您的位置:首页 > 编程语言 > ASP

Delphi编写asp组件(一)

2008-03-28 15:46 666 查看
如果你想脱离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



点击查看大图



点击查看大图

2.再次,文件->新建->其他->activex->activex Server Object,填写CoClassName:showhello,其他不变,点击ok。





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



点击查看大图

4.现在开始写程序,将Unit1保存成show.pas,然后添加方法sayworld的代码



点击查看大图

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, 并自动注册了。这时候会提示:





让你放到web服务器上运行,好了现在写个asp文件调用一下吧,注意Delphi已经生成了一个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的站点下运行看看效果吧:



点击查看大图

5.其他:

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



点击查看大图

6.还有asp页面和组件间传递参数,其实就是给调用的方法(函数)传递参数,注意Delphi里定义的时候要和vbs 的数据类型一致。这些还是大家多实践吧。这里主要是想大家学会封装asp核心代码的方法,起个抛砖引玉的作用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: