Part one of the series of Basic Introduction and Mastery of Asp Components
. I have always wanted to write some component applications. During this time, I can finally write something I like.
I hope that after studying these tutorials, you can write your own components as you like.
Each article may not be related, but just writes about some problems that arise during component writing.
Welcome everyone to criticize and correct
the environment: winxp+vb6+sp6+visual interdev6.0
As the first article, let's first write a relatively simple component
that performs the following functions: input two numbers and add them, return the added result
, open vb6, and create a new Activex Dll project. Change the project name to fCom and the class name to fC1.
Click Menu->Tools->Add Process.
We enter Add in the name, select function as type, select public as scope, and then confirm to
generate the following code. We will continue to improve it.
program code
Option Explicit
Public Function Add(ByVal a As Long, ByVal b As Long) As Long
Add=a+b
End Function
Ok, a simple component has been written. Click menu->File->Generate fCom.dll file
to confirm. There will be fCom.dll file in the directory.
Test
open visual interdev6.0 and generate an asp file. Why should we use it? Interdev, because it has a code prompt function, is consistent with the IDE environment of VB, and is convenient for writing
program code
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
set obj=server.CreateObject("fCom.fC1")
'Pay attention to the following sentence, because the function has a return value, you cannot write it in the following method, otherwise an error will be reported in IE
'obj.Add(3,4)
'Error type:
'Microsoft VBScript compiler error (0x800A0414)
'You cannot use parentheses when calling a subroutine
'/xml/fc1.asp, line 9, column 12
'obj.Add(3,4)
'The following is the correct way to write
dim c
c=obj.Add(3,4)
Response.Write c
%>
</BODY>
</HTML>
Configure the virtual directory, execute this asp file in IE, and get the result 7.
The first article is over. I wish you all a happy study.
Asp Components Basic Introduction and Mastery Series 2
How to Register Components
1. The absolute path of the regsvr32 component
is as follows:
program code
regsvr32 c:testfc1.dll
Because the component can be used as long as it is registered, it has nothing to do with the location of the component.
2.
When is this registration requiredwhen registering in the COM+ component manager
: Components need to be registered before they can execute normally after using COM+ services. If the component is only registered with regsvr32, it still cannot be used
. How to use the component
program code
set obj=server.createobject("Project name. Class name")
Then call its method attribute.
How to uninstall the component
1. The absolute path of the regsvr32 component
is as follows:
program code
regsvr32 c:testfc1.dll /u
u parameter anti-registration component
2. Just delete the component in the COM+ component manager.
Asp component entry and mastery series part three:
How to use properties
to open vb6 and create a new Activex Dll project. Change the project name to fCom and the class name to fC2.
Click Menu->Tools->Add Process.
We enter myName in the name, select the attribute as the type, select public as the scope, and then confirm
the operation again: enter Age in the name, select the attribute as the type, Select public in the scope, and then confirm
and operate again: enter peopleInfo in the name, select function as the type, select public in the scope, and then confirm
. The code is as follows:
program code
Option Explicit
'Local variables that hold attribute values can only be used in classes
Private mvarmyName As String
Private mvarAge As Integer
'Let write attributes (Let attributes: This process assigns a value to an attribute.)
Public Property Let Age(ByVal vData As Integer)
mvarAge = vData
End Property
'Get read attribute (This process gets the value of an attribute.)
Public Property Get Age() As Integer
Age = mvarAge
End Property
Public Property Let myName(ByVal vData As String)
mvarmyName = vData
End Property
Public Property Get myName() As String
myName = mvarmyName
End Property
Public Function peopleInfo() As String
peopleInfo = "Name: " & mvarmyName & " Age: " & mvarAge
End Function
Ok, a simple component has been written. Click menu->File->Generate fCom.dll file
to confirm. There will be fCom.dll file in the directory.
Test
open visual interdev6.0 and generate an asp file. Why should we use it? Interdev, because it has a code prompt function, is consistent with the IDE environment of VB, and is convenient for writing
program code
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
set obj=server.CreateObject("fCom.fC2")
dim c
'What is called here is the Let property of the component
obj.myName="Tornado"
obj.Age =20
c=obj.peopleInfo()
Response.Write c
'What is called here is the Get property of the component
Response.Write "<br>"
Response.Write obj.myName
Response.Write "<br>"
Response.Write obj.Age
%>
</BODY>
</HTML>
Configure the virtual directory and execute this asp file in IE. The results are as follows:
Name: Tornado Age: 20
tornado
20
To be continued
Asp component entry-level introduction and mastery series 4
Array problems
Arrays are used a lot in programs, and they are more likely to cause problems.
Let's take a look at it through a small example
. Open vb6 and create a new Activex Dll project. Change the project name to fCom and the class name to fC4.
Click Menu->Tools->Add Process.
We enter AcceptArray1 in the name, select subroutine as the type, select public as the range, and then confirm
the operation again: enter AcceptArray2 in the name, and select the function as the type. , select public as the scope, and then confirm
program code
'Function: Pass the array address to the component, use the ByRef keyword, and assign and return
Public Sub AcceptArray1(ByRef varray As Variant) As Variant
varray(0) = "Tornado"
varray(1) = "20"
End Sub
'Function: Return a string array
Public Function AcceptArray2() As Variant
Dim a(2) as Variant
a(0) = "Tornado"
a(1) = "20"
AcceptArray2=a
End Function
Ok, a component is written. Click menu->File->Generate fCom.dll file
to confirm. There will be fCom.dll file in the directory.
Test
open visual interdev6.0 and generate an asp file
program code.
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj = server.createobject("fCom.fC4")
dim a(2)
'Test the first component method
obj.AcceptArray1(a)
response.write a(0)
response.write "<br>"
response.write a(1)
response.write "<br>"
'Test the second component method
dim b
b=obj.AcceptArray2()
for i=0 to ubound(b)
Response.Write b(i)
response.write "<br>"
next
%>
</BODY>
</HTML>
Configure the virtual directory and execute this asp file in IE. The results are as follows:
Tornado
20
tornado
20
To summarize:
strings and numbers are passed by value or returned as a return value.
If passing by reference, set the type of the parameter to Variant. Doing this can avoid some mistakes. However, try to reduce the parameters passed by reference as much as possible.
To be continued.
Asp component entry-level introduction and mastery series 5.
Often we can see that when connecting to the database and opening the record set, as follows:
program code
rs.Open strsql, conn,adOpenDynamic,adLockPessimistic
When typing ",", a list of cursor types or lock types will appear for selection.
Sometimes in order to simplify, we directly rs.open strsql,conn,1,3.
Is the first method more professional? Let's take a look at how to
open vb6 in asp and create a new Activex Dll project. The project name is changed to fCom and the class name is changed to fC5
program code
Option Explicit
'Define enumeration type
Public Enum Interfacedig
icfirst = 1
icsecond = 2
icthree = 3
icfour = 4
icfive=5
icsix=6
icserven=7
iceight = 8
End Enum
'Define function
Public Function CallDat(ByVal idig As Integer, ByVal ics As Interfacedig) As Variant
CallDat = idig * ics
End Function
Ok, a component is written. Click menu->File->Generate fCom.dll file
to confirm. There will be fCom.dll file in the directory.
Test
open visual interdev6.0 and generate an asp file
program code.
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
'Definition, you can also put this part into a file, just like ado's record set <!--#include file="adovbs.inc"-->
const icfirst = 1
const icsecond = 2
const icthree = 3
const icfour = 4
const icfive = 5
const icsix = 6
const icserven = 7
const iceight = 8
set obj=server.CreateObject("fCom.fc5")
'iceight or 8 can be used here, but the former makes the code more readable
a= obj.CallDat(4,iceight)
Response.Write a
Response.Write "<br>"
a= obj.CallDat(4,8)
Response.Write a
%>
<P> </P>
</BODY>
</HTML>
Configure the virtual directory and execute this asp file in IE. The results are as follows:
32
32
Asp Component Basic Introduction and Mastery Series 6
Error Handling
If there is an error on the page and there is no error handling, then the page will display an error that the user may not understand.
Can be used in asp script
program code
On Error Resume Next
…
if Err.Number<>0 then
Response.Write Err.Description
End if
But what if something goes wrong in the component? This method can catch errors, but how to know the specific error?
We can add error handling to the component to return errors, so that we can easily see more detailed error information and help us troubleshoot errors.
Use Err.Raise, Raise is used to generate runtime errors.
Open vb6 and create a new Activex Dll project. The project name is changed to fCom and the class name is changed to fC6
program code
Option Explicit
Public Sub showerror1()
On Error GoTo ErrorHandle
Dim i As Double
i=1/0
ErrorHandle:
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
'Generate custom errors
Public Sub showerror2()
Err.Raise 600, "Self-defined error 600", "This is an error describing your own program"
End Sub
Ok, a component is written. Click menu->File->Generate fCom.dll file
to confirm. There will be fCom.dll file in the directory.
Test
open visual interdev6.0 and generate an asp file
program code.
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
'The following sentence is very important
on error resume next
set obj=server.CreateObject("fCom.fc6")
obj.showerror1()
'If there is no error handling, an error interface will be generated, which is very unprofessional.
'The range from 0–512 is reserved for system errors; the range from 513–65535 can be used for user-defined errors.
'If it is a retained error, then the error number in the component is consistent with the error number of page processing
if err.number <>0 then
Response.Write "Error message" & err.number & err.Description
end if
Response.Write "<br>"
'If it is a user-defined error, it can be processed separately on the page
obj.showerror2()
if err.number<>0 then
if err.number =600 then
Response.Write err.number & err.Source & err.Description
end if
end if
%>
</BODY>
</HTML>
Configure the virtual directory and execute this asp file in IE. The results are as follows:
Error message 11 is divided by zero.
600 Self-defined error 600 This is an error describing your own program.
Asp Component Beginner's Introduction and Mastery Series Part 7.
When running the examples in the previous chapters, you may encounter some problems more or less
, such as: after the component is compiled, it needs to be modified. , errors such as "Permission denied, 'f:csdnfcom.dll'" etc. occur.
When browsing the asp page, open the task manager and you will see a process of dllhost.exe with the user name IWAM_YANG. IWAM_YANG will vary depending on the computer name.
You can use the following methods to solve
1. Restart iis.
Find Management Tools->Internet Information Services->right-click the local computer on the second level of the tree on the left in the control panel->all tasks->restart IIS. This operation takes a while.
At this time you can see that the dllhost.exe process has been closed
2. The disadvantage of the above method is that all websites are currently shut down until restarted.
Is there any solution for a single website or virtual directory? Find
Administrative Tools -> Internet Information Services -> Find the virtual directory where your component is running in the Control Panel. ->Right-click Properties->In the first virtual directory->Application Protection->Click Uninstall.
At this time you can see that the dllhost.exe process has been closed
3. If you find it troublesome, just end the process directly in Windows Task Manager
4. The last method
is to find the management tools in the control panel -> Component Services -> click Computer, My Computer, Running Processes -> IIS Out-Of-Process Pooled Applications (2860) -> right-click to close it. But
at this time you can see that the dllhost.exe process has been closed.