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

用 Delphi 7 创建 ActiveX 组件交 ASP 使用

2009-04-06 15:25 471 查看
用 Delphi 7 创建 ActiveX 组件交 ASP 使用

一、启动 Delphi 7 ,新建 ActiveX - ActiveX Library ;

二、引入类库先,若你的 Delphi 还没有引入 asp 组件的话。
Project - Import Type Library ,“Microsoft Active Server Pages Object Library(Version 3.0)”,对应的dll文件是:“系统盘:/Windows目录/system32/inetsrv/asp.dll”,为防止组件名称重复,将TRequest,TResponse,TSession 分别重命名为 TAspRequest,TAspResponse,TAspSession,再点击“Install...”。

三、新建 ActiveX - Automation Object ,coClassName ="test2",Instancing="Multiple Instance";

四、在"Itest2"上新建方法:

1、OnStartPage,参数:const unk:IUnknown,
2、OnEndPage
3、Test(测试方法)

五、代码:
unit Unit1;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
ComObj, ActiveX, Project1_TLB, StdVcl,ASPTypeLibrary_TLB; //1

type
TTestObject = class(TAutoObject, ITestObject)
protected
procedure OnStartPage(const unk: IUnknown); safecall;
procedure OnEndPage; safecall;
procedure test; safecall;
end;

implementation

uses ComServ;

var
m_scriptContext:IScriptingContext;  //2

procedure TTestObject.OnStartPage(const unk: IUnknown);
begin
m_scriptContext:=unk as IScriptingContext;  //3
end;

procedure TTestObject.OnEndPage;
begin
m_scriptContext:=nil;   //4
end;

//5
procedure TTestObject.test;
begin
m_scriptContext.Response.Write('ActiveX test for Delphi');
m_scriptContext.Response.Write('大家好,我是由Delphi创建的ActiveX组件的输出信息.');
end;

initialization
TAutoObjectFactory.Create(ComServer, TTestObject, Class_TestObject,
ciMultiInstance, tmApartment);
end.


六、注册ActiveX组件:
regsvr32 Project1.dll
反注册是:regsvr32 /u Project1.dll

注:若Delphi需重新编译工程,而IIS又在用这个dll,则重启w3c服务才能断开iis与dll的连接,才能编译。

七、ASP使用:

<div>
<%
Dim obj
Set obj=Server.CreateObject("Project1.TestObject")
obj.test
Set obj=nothing
%>
</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: