How to write COM components with VB6 (RPM)

Source: Internet
Author: User
Tags object model
Fortunately, we can use COM components to maximize the functionality of the ASP. You know, anything that can be written in a language such as VB,VC and VJ and other programming languages can be applied to the development of your Web site, which is COM (Component Object model). COM can be used to write objects that can be invoked by ASP. When the programming environment such as VB to complete the compilation of components, it can be placed in the ASP page is called by the ASP. In this article, we'll see how to use VB6 to write a COM object, how to register it on a server, and call it on an ASP page. But before I start, let me briefly introduce a little knowledge about COM.

A COM object typically behaves as a. dll file and is a compiled executable program. This means that COM is running much faster than pure ASP code. Before you can invoke a COM object, you must register it on the machine that is running the IIS server, and you cannot register the COM components through ASP code, which means that you must have permission to use the server.

Now let's take a look at what we're going to accomplish: we need to create a component that, when you pass a year on it, can tell you whether it's a leap year. Of course, this task can be done in VBScript. Here's a simple example, just for the sake of simplicity of the program, to focus more on how to create components and how to interact with ASP. In fact, as mentioned above, you can use any of the features of VB to create this component.

The first thing to do is to write an ActiveX DLL build, and as to how to call it in ASP, we'll mention it later. When you open visual Basic, a dialog box prompts you to choose a new project type, which of course selects "ActiveX DLL."

If the engineering browser is not open, you can open it by selecting the View | Engineering browser above the menu bar. If the property window is not open, the same method can open the Properties window.

Now click Project1 in the Engineering browser, and then in the Properties window you can see the project's Name property is Project1 and change it to checkyear. Of course, if you are lazy, you can also keep these default settings, just in the back of the ASP to call it, also use the appropriate engineering name and class name.

Now in engineering, a default class has been built, and you can rename it to your liking. Name the class here as Leapyear.

Fill in the following code in the class:

Option Explicit

' A function used to determine whether a year is a leap years

Public Function isleapyear (yr as Variant) as Boolean

' If the year can be divisible by 4 and not divisible by 100, or divisible by 400

' Then it's a leap year.

If (yr mod 4 = 0 and yr mod < > 0) Or yr mod = 0 Then

Isleapyear = True

Else

Isleapyear = False

End If

End Function

The above is all the required code, in fact, very simple. The next thing to do is to compile it into a DLL file. From the File menu, choose Create CheckYear.dll ..., and then select the path to save the file to. Generally, you can build a server components directory under the Inetpub directory on the server, specifically for all components used in ASP. But this is not necessary, regardless of where you save the DLL file, in the compilation process, the compiler will automatically register the components in the system.
If your VB and Web server are not on a single computer, after you copy the DLL file to the server, if you want to make the component available, you must register it manually.

First you need to send the. dll file from the machine you developed it to your server. In general, the WWW server can create a directory for all the components written for ASP, and copy the. dll file to that directory. For example, create a server components directory under Inetpub. After the copy is complete, you can run Regsvr32.exe to register the component. In the Start menu, select Run, and then in the Run dialog box, enter the name of the Regsvr32 and the component you want to register and its full path. For example:

Regsvr32 "C:inetpubserver ComponentsMyComponent.dll"

As simple as this, after registering successfully, you will get a successful registration of information. You can then refer to the COM object in the ASP page. Of course, to complete these you need to have access to the server, you can register directly on the server or through such as pcanywhere software to achieve remote registration.

Then, we can call this component in ASP. Create an ASP page and add the following code:

<% Option Explicit

Dim Ocheckyear

Dim Isleapyear

Dim year

Year = 1900 ' years used to detect components

' Create a reference to the component you just made

Set ocheckyear = CreateObject ("Checkyear.leapyear")

' Call the Isleapyear function in the component and report the result

Isleapyear = Ocheckyear.isleapyear (year)

' Clear references to components, good programming habits

Set ocheckyear = Nothing

% >

< HTML >

< head >

< TITLE > Example </title >


< BODY >

<%

' Print output

If isleapyear = True Then

Response.Write "< P >< B >" & Year & "</b > is Leap Year </p >"

Else

Response.Write "< P >< B >" & Year & "</b > not be leap Year </p >"

End If

% >

</body >


The code is simple, and the only thing to explain is how to establish a reference to a custom object in the ASP. Its syntax is: CreateObject ("Projectname.classname"). You should remember, in the front we named the project Checkyear and named the class Leapyear, so here's the statement: CreateObject ("Checkyear.leapyear"). Once you have established a reference to the object, you can access all of its public elements in the ASP page. The rest is as simple as invoking any other ASP built-in objects.





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.