ASP calls the dll and encapsulates the dll instance. Encapsulation into the dll can provide running efficiency and encrypt the code. Open VB6 and create a new ActiveX DLL
2. Add the Microsoft Active Server Pages Object Library selection to the project reference
3. Fill in the code as follows:
Copy the code code as follows:
'Code Start
'Declaration part
Private MyScriptingContext As ScriptingContext
Private MyApplication As Application
Private MyRequest As Request
Private MyResponse As Response
Private MyServer As Server
Private MySession As Session
'The following defines common functions (to access ASP objects in VB, that is, in VB you can use MyApplication to be equivalent to Application in ASP, MyRequest to be equivalent to Request in ASP, MyResponse to be equivalent to Response in ASP, and MyServer to be equivalent to Server in ASP , MySession is equivalent to Session usage in ASP)
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
'Create a custom function SayHello
Public Sub SayHello()
MyResponse.Write(Hello World)
End Sub
'Code End
4. Change the class name to: HelloWorld and change the project name to: TestVBCode
5. Generate the TestVBCode.DLL file and use the Windows run registration component command Regsvr32 path/TestVBCode.DLL to register it for use. (The command to uninstall the component is Regsvr32 /u path/TestVBCode.DLL)
6. Create the Test.asp file, the code is as follows
Copy the code code as follows:
<%
'VB self-built function calling format
'Set object name=Server.CreateObject(project name.class name)
'Object name.Self-built function name
Set MyTestObj = Server.CreateObject(TestVBCode.HelloWorld)
MyTestObj.SayHello
%>
7. The results of running the Test.asp file are as follows:
Hello World
================================================== =========
For more complex applications, you can expand outward through this example.
like:
Copy the code code as follows:
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
This is encapsulated with database connection code. Of course, ADO references need to be added here.