======================================================
注:本文源代码点此下载
======================================================
http://www.delphibbs.com/delphibbs/dispq.asp?lid=140562
asp中的activex服务器组件为标准的automation activex组件,只要使
用由asp提供的接口和遵守asp有关规则便可。delphi 4.0作为一种高效、快
速、强大的开发语言,为开发com组件提供了很强的功能,但不知道什么原因,
很少有人运用delphi编写asp中的activex服务器组件。本文意在通过举例介绍
如何运用delphi编写asp中的activex 组件。
通过以下步骤创建asp中的activex 服务器组件:
1.创建一个activex library工程
打开delphi编辑器,选择选单中的file/new,在new item中选择 the
activex 项中的activex library选项,delphi将自动生成以下代码:
library project1;
uses
comserv;
exports
dllgetclassobject,
dllcanunloadnow,
dllregisterserver,
dllunregisterserver;
{$r *.res}
begin
end.
这是delphi编译com组件时必须的函数,dllgetclassobject函数负责将输
入调用从com库转换到相应的类工厂中;dllcanunloadnow函数是通知ole引擎
如果没有程序引用将自动释放内存;dllregisterserver与
dllunregisterserver函数用来登记com服务器。总之delphi已经为你做好了
一切辅助工作,你只需用心编写主要功能模块便可。
2.在该工程中新增一个automation对象
选择delphi编辑器选单中的file/new,在
new item中选择activex 项中的automation object选项,系统将显示the
automation object导向,在物件名称中输入testobject和选择multiple
instance选项,按ok键后系统将弹出类型编辑器,其实这时系统已经生成了两个
单元(project1—tlb.pas与unit1.pas),unit1.pas中有如下代码:
initialization
tautoobjectfactory.create(comserver, ttestobject,
class—testobject, cimultiinstance);
end.
这是告诉类工厂物件执行的方式与位置。
3.增加组件中的属性与方法
在类型编辑器中单击工具栏中的方法按钮增加以下方法:
onstartpage(unk:iunknown)
onendpage.
test.
注意在增加方法onstartpage时,在parameters项中增加参数unk,类型为
iunknown,增加三项方法后,在类型编辑器中按refrash键,unit1.pas中将会
出现三个函数,在每个函数下写如下程序:
procedure ttestobject.onstartpage(unk: iunknown);
begin
m_scriptcontext := unk as iscriptingcontext;
end;
procedure ttestobject.onendpage;
begin
m_scriptcontext := nil;
end;
procedure ttestobject.test;
begin
m—scriptcontext.response.write(′activex test for delphi′);
end;
当iis激活一个activex组件时它会自动寻找组件中是否有onstartpage与
onendpage方法,如果存在,服务器将在开启本asp页时自动执行onstartpage
方法和当本asp页所有脚本执行完毕后自动执行onendpage方法。
其中mscriptcontext 与iscriptingcontext是asp中负责将asp转换成
html格式必不可少的变量与类型,因此必须将unit1.pas文件中的uses加上
asptypelibrary—tlb变成
unit unit1;
interface
uses comobj, activex, delphi_tlb, asptypelibrary—tlb, sysutils;
asptypelibrary—tlb.pas文件可以通过类型库引入取得,打开delphi编
辑器中的选单project/import type library 项,选择the microsoft
active server pages type library,按ok,delphi便会自动生成一个
asptypelibrary_tlb.pas。
4.编译与注册
选择delphi编辑器中的选单project/compile 项编译文件,然后选择
run/register activex server对project1.dll文件注册。
5.在asp文件中运用project1.dll
在asp文件中加入以下文字:
set testasp = server.createobject(project1.testobject)
testasp.test
通过上面的例子,我们可以发现用delphi编写asp中的activex服务器组件
是十分容易的,加上delphi丰富的组件与强大的功能,使我们相信运用delphi
编写特殊的asp服务器组件,如数据库、服务器上各种信息与资源访问等,都会
十分方便。
问题如下:
1、编译时说m_scriptcontext 没说明。
2、如果我想在上边加一个form,当中有数据库控件等行吗?
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/