ASP 调用dll及封装dll实例
程序员文章站
2022-03-31 14:02:15
打开vb6,新建activex dll 2、在工程引用中加入microsoft active server pages object library选择 3、填加代码如下:...
打开vb6,新建activex dll
2、在工程引用中加入microsoft active server pages object library选择
3、填加代码如下:
'code start
'声明部分
private myscriptingcontext as scriptingcontext
private myapplication as application
private myrequest as request
private myresponse as response
private myserver as server
private mysession as session
'下面定义公用函数(在vb中访问asp对象,即在vb中可以用myapplication等同于asp中的application、myrequest等同于asp中的request、 myresponse等同于asp中的response、 myserver等同于asp中的server、 mysession等同于asp中的session 使用)
public sub onstartpage(passedscriptingcontext as scriptingcontext)
set myscriptingcontext = passedscriptingcontext
set myapplication = myscriptingcontext.application
set myrequest = myscriptingcontext.request
set myresponse = myscriptingcontext.response
set myserver = myscriptingcontext.server
set mysession = myscriptingcontext.session
end sub
public sub onendpage()
set myscriptingcontext = nothing
set myapplication = nothing
set myrequest = nothing
set myresponse = nothing
set myserver = nothing
set mysession = nothing
end sub
'建立自定义函数sayhello
public sub sayhello()
myresponse.write ("hello world")
end sub
'code end
4、将类名改为:helloworld 将工程名改为:testvbcode
5、生成testvbcode.dll文件,并使用windows运行注册组件命令regsvr32 路径\testvbcode.dll注册后即可使用。(卸载组件命令为regsvr32 /u 路径\testvbcode.dll)
6、建立test.asp文件,代码如下
<%
'vb自建函数调用格式
'set 对象名=server.createobject("工程名.类名")
'对象名.自建函数名
set mytestobj = server.createobject("testvbcode.helloworld")
mytestobj.sayhello
%>
7、运行test.asp文件结果显示如下:
hello world
===========================================================
对于更复杂的运用,大家可以通过这个实例向外扩展就可以了.
如:
public sub connstr2()
set conn = myserver.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq=" & myserver.mappath("codata.mdb")
set rs = conn.execute("select * from news")
do while not rs.eof
myresponse.write (rs("news_title") & "<br>")
rs.movenext
loop
rs.close
set conn = nothing
end sub
这个是用数据库连接的代码封装,当然这里要添加ado引用的.
2、在工程引用中加入microsoft active server pages object library选择
3、填加代码如下:
复制代码 代码如下:
'code start
'声明部分
private myscriptingcontext as scriptingcontext
private myapplication as application
private myrequest as request
private myresponse as response
private myserver as server
private mysession as session
'下面定义公用函数(在vb中访问asp对象,即在vb中可以用myapplication等同于asp中的application、myrequest等同于asp中的request、 myresponse等同于asp中的response、 myserver等同于asp中的server、 mysession等同于asp中的session 使用)
public sub onstartpage(passedscriptingcontext as scriptingcontext)
set myscriptingcontext = passedscriptingcontext
set myapplication = myscriptingcontext.application
set myrequest = myscriptingcontext.request
set myresponse = myscriptingcontext.response
set myserver = myscriptingcontext.server
set mysession = myscriptingcontext.session
end sub
public sub onendpage()
set myscriptingcontext = nothing
set myapplication = nothing
set myrequest = nothing
set myresponse = nothing
set myserver = nothing
set mysession = nothing
end sub
'建立自定义函数sayhello
public sub sayhello()
myresponse.write ("hello world")
end sub
'code end
4、将类名改为:helloworld 将工程名改为:testvbcode
5、生成testvbcode.dll文件,并使用windows运行注册组件命令regsvr32 路径\testvbcode.dll注册后即可使用。(卸载组件命令为regsvr32 /u 路径\testvbcode.dll)
6、建立test.asp文件,代码如下
复制代码 代码如下:
<%
'vb自建函数调用格式
'set 对象名=server.createobject("工程名.类名")
'对象名.自建函数名
set mytestobj = server.createobject("testvbcode.helloworld")
mytestobj.sayhello
%>
7、运行test.asp文件结果显示如下:
hello world
===========================================================
对于更复杂的运用,大家可以通过这个实例向外扩展就可以了.
如:
复制代码 代码如下:
public sub connstr2()
set conn = myserver.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq=" & myserver.mappath("codata.mdb")
set rs = conn.execute("select * from news")
do while not rs.eof
myresponse.write (rs("news_title") & "<br>")
rs.movenext
loop
rs.close
set conn = nothing
end sub
这个是用数据库连接的代码封装,当然这里要添加ado引用的.