ASP calls DLL and encapsulates DLL instances

Source: Internet
Author: User

Open VB6 and create ActiveX DLL
2. Add Microsoft Active Server Pages object library selection to the project reference
3. Add Code As follows: Copy code The Code is 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 public functions (accessing ASP objects in VB, in VB, myapplication can be equivalent to application in ASP. myrequest is equivalent to request in ASP. myresponse is equivalent to response in ASP. myserver is equivalent to server in ASP. mysession is equivalent to session in ASP. use)
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 user-defined 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 run the register component command regsvr32 path \ testvbcode. dll on Windows to register the file. (The uninstall component command is regsvr32/u path \ testvbcode. dll)
6. Create the test. asp file. The Code is as follows:Copy codeThe Code is as follows: <%
'Vb self-built function call 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 result of running the test. asp file is as follows:
Hello World
========================================================== ==============================
For more complex applications, you can use this instance to scale out.
For example:Copy codeThe Code is 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 by the database connection code. Of course, ADO reference should be added here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.