ASP Lecture 10: Write Components by Yourself
Author:Eve Cole
Update Time:2009-05-30 19:54:53
In the previous content, we learned how to use components. Although there are many components on the Internet, often a small component requires you to shell out US dollars, and there are too few domestically produced components. If you want to spend some RMB, you can’t find the place. It's easy, forget it, just write it yourself. In fact, writing components is not difficult. It's just a matter of starting to write low-level components and slowly starting to write high-level components. To write ActiveX Server Components, there are many tools to choose from: Visual Basic, C++, Java, etc. Here we choose the simplest VB6.0. As long as you have used VB before, you can make one in less than 1 hour. The widget comes out.
1. Quick component writing - the most basic steps for writing components. This part mainly explains the basic steps for writing components. If you have the desire to learn further, please refer to the "Creating ActiveX DLL" section in the VB6.0 help document (documentation in MSDN Library The file name is Vbcon98.chm).
Purpose: Write the simplest adder component. This component has two properties and one method. Let's start from the most familiar place, which is to assume that this component already exists and look at the code that uses this component in ASP:
<%
Dim objSum
'Create the component first, the component identifier is TestDll.Sum
Set objSum = Server.CreateObject("TestDll.Sum")
'Remember "TestDll", "Sum" in TestDll.Sum.
objSum.augend = 10.52 'augend attribute is the summand
objSum.addend = 382.41 'addend attribute is the addend
Result = objSum.Plus 'Plus method sum
Response.Write Result
Set objSum = Nothing
%>
Please be sure to remember these keywords: TestDll, Sum, augend, addend, Plus, which will be closely related to the next work. Here are the detailed steps:
1. Run VB6.0, in the "File" menu, click "New Project" → in the "New Project" dialog box, double-click the "ActiveX DLL" icon → Visual Basic will automatically add a class module to the new project Class1 (can be seen in the "Project Explorer", as shown in Figure 1).
2. Press the F4 key to open the Properties window. Double-click the "Name" attribute and change "Class1" to Sum.
3. On the Project menu, click Project 1 Properties to open the Project Properties dialog box. Fill in TestDll in "Project Name" and "Create ActiveX DLL Example" in "Project Description". As shown in Figure 2.
Tip: Do you understand the meaning of the two identifiers TestDLL.Sum in the ASP creation component?
4. In the "File" menu, click "Save Project" and save the project files with the following names: SetupDll.cls and SetupDll.vbp.
5. Create properties for the Sum class:
(1) In the "Tools" menu, select "Add Process" to open the "Add Process" dialog box. In the "Name" box, enter augend, click "Properties", and then click "OK", as shown in Figure 3 (Note: The current focus should be on the code window, otherwise the "Add Procedure" menu will be invalid).
(2) In the "Tools" menu, select "Add Process" to open the "Add Process" dialog box. In the Name box, enter addend, click Properties, and then click OK.
6. Create a method for the Sum class:
On the Tools menu, click Add Procedure to open the Add Procedure dialog box. In the "Name" box, enter Plus. In the type, you can select "Subroutine" or "Function". In this example, since you want to return the sum of the addend and the summand, click "Function" and then click Click OK.
Now, the five key words in the previous ASP code have been used, so now you understand a lot of things.
7. All codes in the class module Sum code window are as follows:
Option Explicit
Private mdbl_augend As Double
Private mdbl_addend As Double
Public Property Get augend() As Double
augend = mdbl_augend
End Property
Public Property Let augend(ByVal vNewValue As Double)
mdbl_augend = vNewValue
End Property
Public Property Get addend() As Double
addend = mdbl_addend
End Property
Public Property Let addend(ByVal vNewValue As Double)
mdbl_addend = vNewValue
End Property
Public Function Plus()
Plus = augend + addend
End Function
You have noticed that the above code is slightly different from the system-generated code, mainly because the Public Property Get augend() As Variant part is replaced by the actual data type Double.
Note: In fact, when the x = objSum.augend statement is executed, the Property Get procedure is called to obtain the value of mdbl_augend. When the objSum.augend = 10.52 statement is executed, the Property Let procedure is called and 10.52 is assigned to mdbl_augend.
8. The last step is to click "Generate .Dll(K)..." in the "File" menu to generate the file SetupDll.dll.
At this point, the component has been created. Now, we can first write a test project in VB6 to test whether the file is correct.
1. In the "File" menu, click "New Project" → double-click "Standard EXE".
2. In the "Project" menu, click "Reference" → press the "Browse" button in the "Reference" dialog box, select the just compiled "SetupDll.dll" file → click "OK".
3. Finally write the following code in the code window:
Option Explicit
Private Sub Form_Load()
Dim objSum As TestDll.Sum
Set objSum = New Sum
objSum.augend = 10.52
objSum.addend = 382.41
MsgBox objSum.Plus
End Sub
Run it and the result is correct. Next we use this component in ASP. Copy SetupDll.dll to the Web server, and use the "Regsvr32.exe setupdll.dll" command to register the component, and then execute the ASP program at the beginning of this section in the browser. If that doesn't work, you can try to package it with the VB6 packaging tool first, and then install it. The components will be automatically registered, and some necessary VB6 running files will also be installed automatically.
Note: All programs in this part are in Setupdll.zip of the download package.
In the above example, we use the property procedures Property Get and Property Let to add properties (Property Get reads the value of the property, and Property Let assigns the value to the property). If you need to verify the set value, you can write appropriate code in the Property Let process. code. Such as the following code:
Public Property Let augend(ByVal vNewValue As Double)
If vNewValue < 100 Then
MsgBox "What are you doing! If the value is less than 100, just do the math yourself."
Else
mdbl_augend = vNewValue
End If
End Property
2. Other ways to add properties and methods to components
1. The simplest way to add attributes: add public variables, as in the above example, the simplest code is as follows:
Option Explicit
Public augend As Double ' Add augend attribute
Public addend As Double ' Add addend attribute
Public Function Plus() As Double
Plus = augend + addend
End Function
However, this method can only set read/write properties and cannot verify the assigned value. If you only need to add a read-only attribute: In the above example, delete the Public Property Let augend(ByVal vNewValue As Double) process, then augend becomes a read-only attribute and cannot be assigned a value. Of course, if you want to add a write-only property, just delete the Property Get procedure.
2. Using the class builder utility
There is also a tool for adding properties and methods in VB6: In the "Add-in" menu, click "Add-in Manager", select "VB Class Builder Utility" in the pop-up Add-in Manager dialog box, and load Select "Load/Unload" in the behavior (Figure 4). Then click Class Builder Utility in the Add-Ins menu and you'll know how to use it.
3. Tell you a few issues to pay attention to
1. The above example is an extremely simple component. More practical components generally have at least one module.
2. If you are not careful, Visual Basic can produce "single-threaded" components.
Perhaps one of the most common pitfalls is using components that are not designed to run under ASP, such as "single-threaded" components. Therefore, make sure that the threading module is set to "Apartment Threading" on the "General" tab of the "Project Properties" page.
3. About the "type mismatch" error.
A good suggestion is to declare the Out parameter as Variant. Note that this is not set in the above example.
4. About the use of global variables.
Try to avoid using global variables in components. In Visual Basic terms, this means that there are no Public or Global variables in a standard .BAS module. Because Global variables are not truly global. Each thread has its own copy, and if several methods happen to be executed in the same thread, they will see the same variables; otherwise they access different copies of these variables. This means that you may assign a value to a global variable (in thread A), but another user of it (executing in thread B) cannot see the new value. The reason for this is that Visual Basic internally uses "Thread Local Storage (TLS)" to reference global variables. This means that each thread has its own copy of the Public variable, and because there are multiple copies of it, the global data is not really "global". That is, users who happen to be running in the same thread will access the same variable, whether they expect it to or not.
Finally, by the way, VB6.0 introduces a new type of Visual Basic application: IIS application. You can create IIS applications just like you create ActiveX DLLs. Any application created with Active Server Pages can be created in the VB development environment. You can completely replace all Active Server Pages with a single VB application.