1. Introduction
The server-side script running environment is simple and easy to use, does not require compilation and connection, scripts can be run directly on the server side, and it supports multi-users and multi-threads, and has been widely used in Web development. Server-side components are different from client-side components. Client-side components are transmitted over the network, rely on HTML to function, and are only available on IE. But the server-side component runs on the server side and performs various operations on the server. Therefore, all browsers can enjoy it, and it relies on the server rather than the browser. However, because ASP scripts are in plain text format, malicious actors can easily see page content that they should not see through the source code. Therefore, protecting ASP source code is very important. Encapsulating ASP code into DLL not only speeds up the execution of ASP code, but also protects the original code. When IIS is requested to execute an ASP program, it will first find the code between the tags in the ASP file and execute it (it can also be the code between). If this ASP program has been called before, then it will use the compiled program in memory to return HTML code to the user. If not, then it will recompile. This greatly saves server resources.
2. Implementation method
Start your VB and select the ActiveX icon. This icon can be found in the new project! VB will provide a default project name (project1) and class name (class1). Before starting, please first confirm that we have the Microsoft ActiveX Data Object 2.0 Library, which is very useful in our program. Select "Project" from the menu, then select "Reference" there, the "Reference" window will appear, select Microsoft ActiveX Data Object 2.0 Library.
Now we have our own project (project1) and class name (class1). We will use their names to refer to this component in ASP code in the future. In ASP we quote it like this, as follows:
Set ObjReference = Server.CreateObject("ProjectName.ClassName")
In order to use ASP methods in a class, you must write the initialization and termination functions in this class. Enter the following code: in the program list
Private Sub Class_Initialize() and Private Sub Class_Terminate() in Class1.cls
make the key functions in the ASP code into a dynamic link library (.dll), partially hiding the ASP source code. For example: the code entered in Global.bas and Class1.cls in the program listing (the main function of the code is to retrieve records in the database and display them).
Select File → Generate article.dll → Select the directory to save in the drop-down menu of VB. Find article.dll and copy it to the system32 folder of the system disk. The last step is to register the DLL file. Select Run in the Start menu and enter regsvr32 c:winntsystem32article.dll.
3.
Code in program list 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 "<head>" & vbCrLf
Response.Write "<meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312"">" & vbCrLf
Response.Write "<title>Article Management System-CSSTUDIO</title>" & vbCrLf
Response.Write "</head>" & vbCrLf
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 "</html>" & vbCrLf
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 using article.dll:
<%Dim AspTransBuilderObject
Set AspTransBuilderObject = Server.CreateObject("article.Class1")
AspTransBuilderObject.AspClassInit
Set AspTransBuilderObject = Nothing%>
Code in index.asp before using 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>Article 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("Added date") %></td>
</tr>
</table>
<% Rs.MoveNext
Wend %>
</body>
</html>
<% rs.close
set rs=nothing
conn.close
set conn=nothing %>
4. Conclusion
Because these codes run on the server side, the client does not need to install anything. This is just a small example of what can be achieved with ActiveX DLLs. You can write your own larger components, and you can also use many controls in VB. Let's use components to expand the functionality of our program! I also hope to see more components from our Chinese people. I hope this article can serve as a starting point.