Because there have been a lot of netizens asking recently, I will simply sort it out. If you have any questions, you can leave them in the comments.
1. Create a new DLL
Open VB6-->File-->New Project-->Select ActiveX DLL-->OK
2. Rename the default project and class
Project rename: Project --> Project 1 Properties (this name corresponds to the project name in the form) --> Rename the "Project Name" to ASP2DLL in the open dialog box (after the DLL component is registered in the system, The default calling method in asp is "project name.class name") --> OK
Class Rename Rename the name to Demo in the properties window
3. Define ASP basic objects
Add module: Project-->Add module-->Select "Module"-->Open
Rename the module: Project Explorer-->Module--Module1-->Rename Module1 to "ASPMod" in the properties window
Add the module code. This code is applicable to almost all asp DLL components encapsulated in vb. Just change the ASP2DLL to the project name or keep it consistent when calling. The code is as follows:
Copy the code code as follows:
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
Public Sub ASP2DLL_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
Public Sub ASP2DLL_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 Function Eval(ByRef strEval)
Dim EvalObject As New ScriptControl
EvalObject.Language = "VBScript"
Eval = EvalObject.Eval(strEval)
Set EvalObject = Nothing
End Function
4. Save the newly created DLL
File-->Save project, confirm all the way, and save all modules, class modules, and project files in one folder
5. Call ASP objects in class modules
Project Explorer --> Class Module --> Double-click Demo to switch to the Demo class module code editor, paste the code, initialize class calling and class destruction, the code is as follows:
Copy the code code as follows:
Private Sub Class_Initialize()
ASP2DLL_Initialize
End Sub
Private Sub Class_Terminate()
ASP2DLL_Terminate
End Sub
At this point, a basic DLL framework is completed. Now you can complete the required encapsulated functions according to your own needs.
6. Create a new test function
Paste the following two test functions under the Demo class module.
Copy the code code as follows:
Public Sub hello()
Response.Write ("Hello World!")
Exit Sub
End Sub
7. Compile and generate DLL
File-->Generate ASP2DLL.dll-->Select the folder where the project is located and confirm, OK. If there is no error message at this point, it proves that the dll component has been compiled successfully.
8. Register and uninstall components
Created in the directory where the ASP2DLL.dll component is located
"Registration.bat" batch file, enter:
Copy the code code as follows:
iisreset/stop
regsvr32/sASP2DLL.dll
iisreset/start
"Uninstall.bat" batch file, enter:
Copy the code code as follows:
iisreset/stop
regsvr32 /u /sASP2DLL.dll
iisreset/start
Double-click to run the registration.bat. If the registration is successful, it will prompt: "DllRegisterServer in ASP2DLL.dll is successful."
9. Call the newly encapsulated DLL components and test functions in the ASP program
In step 2, we know that "when the DLL component is registered in the system, the default calling method in asp is 'project name. class name'", therefore, the object should be created like this: set Obj = Server.CreateObject("Project name. Class name"), the code is as follows:
Copy the code code as follows:
<%
DimASP2DLL
Set ASP2DLL = Server.CreateObject("ASP2DLL.Demo")
ASP2DLL.hello()
SetASP2DLL = Nothing
%>
10. Package download link including project files and all related content (20110221 update download link) Package download
A few points to note when compiling a DLL:
1. Avoid using VB reserved keywords as function or variable names;
2. Regular and some Vbscript functions such as Eval need to load "Project-->Reference-->Microsoft Script Control 1.0" and "Microsoft VBScript Regular Expressions 5.5";
3. Encapsulation cannot bring about an essential speed improvement, and virtual hosts and remote servers need component registration permissions;
4. For existing component names or modified dll files, you must first stop IIS and then restart IIS to register the dll component;
5. VB6 SP6 streamlined green version download link
Original text from:?cateID=20 Thanks to the author for his hard work. The stuff is great, but the website access is too slow.