Use VB to encapsulate ASP code into DLL

Source: Internet
Author: User

I. Introduction

Server-side script running environment, which is easy to use and does not require compilation and connection. scripts can be run directly on the server side. It supports multiple users and multithreading and has been widely used in Web development. The server component is different from the client component. The client components are transmitted over the network and work with HTML, and can only be used on IE. However, the server component runs on the server and performs various operations on the server. Therefore, all browsers can enjoy it, relying on servers rather than browsers. However, because ASP scripts are in plain text format, attackers canSource codeYou can easily see the content of the page that was not supposed to be viewed. Therefore, ASP source protectionCodeIt is very important. Encapsulating ASP code into DLL not only speeds up ASP code execution, but also protects the original code. When IIS is requested to execute an ASPProgramFirst, it finds the code between tags in the ASP file and executes it (or code between tags ). If this ASP program was previously called, it will return HTML code to the user using the compiled program in the memory. If not, it will be re-compiled. This greatly saves the server resources.

II. Implementation Method

Start your VB and select the ActiveX icon. This icon can be found in the new project! VB provides a default project name (project1) and Class Name (class1 ). Before you start, make sure that we have Microsoft ActiveX Data Object 2.0 library, which is very useful in our program. Select "project" from the menu, and then select "Reference". The "Reference" window appears, where Microsoft ActiveX Data Object 2.0 library is selected.

Now we have our own project (project1) and Class Name (class1 ). In the future, we will use their names in ASP code to reference this component. In ASP, We reference it as follows:
Set objreference = server. Createobject ("projectname. classname ")

To use ASP methods in a class, you must write the initial and termination functions in this class. Enter the following code:
Private sub class_initialize () and private sub class_terminate () in class1.cls ()

Make the key functions in ASP code into a dynamic link library (. dll) to partially hide the ASP source code. For example, the Code entered in global. Bas and class1.cls in the program list (the main function of the code is to retrieve and display records in the database ).

In the VB drop-down menu, select File → generate article. dll → select the directory to save. Find article. dll and copy it to the System32 folder on the system disk. register the DLL file in the last step. In the Start menu, select Run enter regsvr32 c: \ winnt \ system32 \ article. dll.

3. program list

Code in global. Bas:
Public objcontext as objectcontext

Public Application as asptypelibrary. Application

Public server as asptypelibrary. Server

Public session as asptypelibrary. Session

Public response as asptypelibrary. Response

Public request as asptypelibrary. Request

Code in class1.cls:
Private sub class_initialize ()

On Error resume next

Set objcontext = getobjectcontext

Set Application = objcontext. Item ("application ")

Set Server = objcontext. Item ("server ")

Set session = objcontext. Item ("session ")

Set Request = objcontext. Item ("request ")

Set response = objcontext. Item ("response ")

End sub

Private sub class_terminate ()

On Error resume next

Set Application = nothing

Set Server = nothing

Set session = nothing

Set Request = nothing

Set response = nothing

Set objcontext = nothing

End sub

Public sub aspclassinit ()

On Error goto err

Set conn = server. Createobject ("ADODB. Connection ")

Strcon = "provider = Microsoft. Jet. oledb.4.0 ;"&_

"Data Source =" & server. mappath ("article. mdb ")

Conn. Open strcon

Set rs = server. Createobject ("ADODB. recordset ")

SQL = "select * from article order by ArticleID DESC"

Rs. Open SQL, Conn, 1, 1

Response. Write "<HTML>" & vbcrlf
Response. Write "Response. Write "<meta http-equiv =" "Content-Type" "content =" "text/html; charset = gb2312" ">" & vbcrlf
Response. Write "<title> Article Management System-csstudio </title> "& vbcrlf
Response. Write "Response. Write "<body bgcolor =" "# ffffff" "topmargin =" "0"> "& vbcrlf
Response. Write "<Table width =" "100" "border =" "0" "cellpadding =" 2 "" cellspacing = "0" ">" & vbcrlf
Response. Write "<tr>" & vbcrlf
Response. Write "<TD width =" "742" "Height =" 20 ""> Article Title </TD> "& vbcrlf
Response. Write "<TD width =" 90 ""> click </TD> "& vbcrlf
Response. Write "<TD width =" "145"> Add date </TD> "& vbcrlf
Response. Write "</tr>" & vbcrlf
Response. Write "</table>" & vbcrlf
While not Rs. EOF and rows <Rs. pagesize
Response. Write "<Table width =" "100" "border =" "0" "cellspacing =" 0 "" cellpadding = "0"> "& vbcrlf
Response. Write "<tr>" & vbcrlf
Response. Write "<TD width =" "747" "Height =" 20 ""> ☆< a href = "" view. asp? Id ="
Response. Write RS ("ArticleID ")
Response. Write ""> "& vbcrlf
Response. Write ""
Response. Write RS ("title ")
Response. Write "</a> </TD>" & vbcrlf
Response. Write "<TD width =" "94" ">"
Response. Write RS ("click ")
Response. Write "</TD>" & vbcrlf
Response. Write "<TD width =" "148" ">"
Response. Write RS ("add date ")
Response. Write "</TD>" & vbcrlf
Response. Write "</tr>" & vbcrlf
Response. Write "</table>" & vbcrlf
Rs. movenext
Wend
Response. Write "</body>" & vbcrlf
Response. Write "

Rs. Close

Set rs = nothing

Conn. Close

Set conn = nothing

Exit sub

Err:

If err. Number =-13572468 then exit sub

Resume next

End sub

 

Code in index. asp after article. dll is used:
<Dim asptransbuilderobject

Set asptransbuilderobject = server. Createobject ("article. class1 ")

Asptransbuilderobject. aspclassinit

Set asptransbuilderobject = nothing>

Use the code in index. asp before article. dll:
<Set conn = server. Createobject ("ADODB. Connection ")

Strcon = "provider = Microsoft. Jet. oledb.4.0 ;"&_

"Data Source =" & server. mappath ("article. mdb ")

Conn. Open strcon>

<Set rs = server. Createobject ("ADODB. recordset ")

SQL = "select * from article order by ArticleID DESC"

Rs. Open SQL, Conn, 1, 1>

<HTML>

<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Document Management System-csstudio </title>
</Head>
<Body bgcolor = "# ffffff" topmargin = "0">
<Table width = "100" border = "0" cellpadding = "2"
Cellspacing = "0">
<Tr>
<TD width = "742" Height = "20"> Article Title </TD>
<TD width = "90"> click </TD>
<TD width = "145"> Add date </TD>
</Tr>
</Table>
<While not Rs. EOF and rows <Rs. pagesize>
<Table width = "100" border = "0" cellspacing = "0"
Cellpadding = "0">
<Tr>
<TD width = "747" Height = "20"> ☆< a href = "view. asp? Id = <
= RS ("ArticleID")> ">
<= RS ("title")> </a> </TD>
<TD width = "94"> <= RS ("click")> </TD>
<TD width = "148"> <= RS ("add date")> </TD>
</Tr>
</Table>
<Rs. movenext
Wend>
</Body>
</Html>
<Rs. Close
Set rs = nothing
Conn. Close
Set conn = nothing>

Iv. Conclusion

Because the code runs on the server, the client does not need to install anything. This is just a small example of the functions implemented by ActiveX DLL. You can write your own larger components and use many widgets in VB. Let's use components to expand the functions of our program! We also hope to see a lot of Chinese components. I hope this article will serve as a reference.

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.