Author: Michael Gonzalez
Frequently Answered Questions
Miscellaneous

File Text Search & Replace Utility
HTML Text Extraction using innerText
E-mail (CDONTS.NewMail) Sample Code
SQL Server 7.0/2000

Incorporating ASP and SQL Server
100's of T-SQL Scripts
Don't Use @@ERROR with UPDATE Statements
Exporting Tables to Text Files
Creating SQL Server Databases
ASP (SQL) Query Analyzer
Increasing SQL Server Performance with Indexes
Distributed SQL Server Transactions & Queries
COM/COM+ Development
What is COM?
Isn't ActiveX and COM the same?
How can Components benefit my ASPs?
Am I using COM Components now?
How do I use COM Components in my ASPs?
Creating your First COM Component
Creating a COM Component that uses ASP Intrinsic Objects
Creating a COM Component to access an MS-Access Database
MTS Component Template
MSMQ Component Template / Example

Creating your First COM Component

Before we continue, you will need to make sure you have a few things ready:
  • Visual Basic 6 (for creating your component)
  • PWS (Personal Web Server) or IIS4 (Internet Information Server v.4)
  • Notepad editor or Visual InterDev (for ASP editing)
I will be giving ASP examples in both VBScript and Javascript so don't worry about what scripting language your most familiar with.

We will be creating a COM Component that does one thing: adds two numbers and returns the result. Please follow each step as outlined below:
  1. Startup Microsoft Visual Basic 6.0
  2. From the New tab, click on ActiveX DLL then click on the Open button.
  3. Click on the Project menu then click on Project1 Properties....
  4. Change the project name from Project1 to Calculator.
  5. Click on the Unattended Execution checkbox - this will make sure that Visual Basic does not compile your component if it contains illegal code such as MsgBox().
  6. Click on the OK button.
  7. Change the name of the ClassModule to Adder as shown below:

    NOTE: A Class Module in Visual Basic means the same thing as a COM Component, the Project name is the default name of the COM Server (the DLL or EXE compiled result), and the component's ProgID is a combination of both the Project name and Class Module (Calculator.Adder).
  8. The easiest way to add properties to your component is by using the the Class Builder. Click on the Add-Ins menu then click on Add-In Manager....
  9. Double-Click on VB 6 Class Builder Utility then click on the OK button.
  10. Click on the Add-Ins menu then click on Class Builder Utility....
  11. Click on the class in the left pane.
  12. Click on the button to add a new property.
  13. Enter intOne as the Name: and change the Date Type: to Integer
    .
  14. Perform steps 12 and 13 again BUT this time set the property name to intTwo.
  15. Click on the File menu then click on Update Project.
  16. Close the Class Builder. You will notice that the Class Builder has added code to the code window.
  17. Now we need to add the function/method that will add intOne to intTwo. Add the following code to the bottom of the code window: Public Function add() As Integer add = intOne + intTwo End Function
  18. Save your work and call your files whatever you like.
  19. Next, we need to compile the component so click on the File then click on Make Calculator.dll....
  20. Choose a location where to save your COM Server. Remember where it is as you will need to register it on the PWS or IIS4 server machine in order to use it.
  21. Now we have to register the COM Server on the PWS or IIS4 server. If the Calculator.dll COM Server is not on the web server, copy it to a location on that server now.
  22. At the server, register the COM Server by using the following syntax:
    regsvr32 C:\Path\Calculator.dll
    Replace C:\Path with the actual location you stored the DLL. You can run regsvr32 from either the Start/Run... dialogue box or the DOS Command Prompt. Once registering your COM Server, you should get a confirmation dialogue box like the one below:


  23. Now that your all done creating and registering your first component, let's get an ASP page ready to use it!

  24. Using Visual InterDev or Windows' Notepad, create an ASP page on your web server's virtual directory called Calc.asp.
  25. Now we fill in the ASP page with code:

    Using Javascript: var objCalc = Server.CreateObject("Calculator.Adder"); objCalc.intOne = 5; objCalc.intTwo = 4; var ans = objCalc.add(); Response.Write(ans); var objCalc = null Using VBScript: Set objCalc = Server.CreateObject("Calculator.Adder") objCalc.intOne = 5 objCalc.intTwo = 4 ans = objCalc.add() Response.Write(ans) Set objCalc = Nothing
  26. Run the Calc.asp page from a browser. The only thing you should see on the browser window is the number 9. This is a result of the method objCalc.add being invoked which adds the values 5 and 4 (intOne and intTwo respectively).
Comments & Questions Form

Send It!