oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Writing Your First COM Components, Part 1

by Abel Banda

With all of the buzz surrounding COM+ components and n-tier applications, you're probably eager to get in on all the hype by learning how to build your first COM component. This short guide steps you through the process of creating your first component in minutes. Prior to developing in COM, let's understand the benefits of COM components.

Step 1 : Writing Your Component

  1. Launch Visual Basic, and from the "New" tab (default), select "ActiveX DLL," and then click "OPEN".

  2. You are now well on your way towards creating your first COM component. Notice that you have just started a new project, and that there is a class within the project. Following this object model, change the project name and the class name, respectively, to something that will remind you of the component's functionality. For this sample, we shall name the project myfirstcomponent and the class functions. If you are new to Dynamic Link Libraries (DLLs), it's important to note that the project name will most likely be the name of the DLL when it is compiled.

    Here are some basic rules towards understanding DLLs:

    • Each DLL has a set of classes.
    • Each class has its own set-up functions and sub-routines.
    • Public functions can be accessed outside of the DLL.
    • Private function cannot be accessed outside of the DLL.

    For this quick sample, we will be creating a simple public function.

  3. Save the project (with new names).

  4. Double-click on the functions class from the project panel to get started.

  5. Enter the following code for our simple example:

    Example 1: My First Component

    Public Function GetGreeting ( ) As String
    'You can add database access to retrive values, taking full advantage of COM.
    GetGreeting = "My First Com Component Worked!"
    End Function
  6. Save the project.

  7. From the Project menu, click on the "myfirstcomponent Properties..." menu item. From the General tab, make sure that Threading Model is set to Apartment Threading.

  8. Save The Project.

  9. From the File drop-down menu, select Make myfirstcomponent.dll. You will be asked to specify a location. Save it to your system32 directory. It will most likely be at c:\winnt\system32 if you're using Internet Information Server. If you're using Personal Web Server, it will most likely be at c:\windows\system32. Click OK to compile the DLL in that folder.

  10. Done! All we need to do now is register it, and we're ready to access it from your next Web page. To register, enter the following command in the Run field (available from the Start menu), substituting the appropriate path and file name as appropriate:

    regsvr32 c:\winnt\system32\myfirstcomponent.dll

    A message will tell you if it was successfully registered. If the registration did not work, make certain that you typed in the path and file name correctly.

Step 2: Accessing Your Component

  1. Now that we've compiled our first COM component, let's get to the fun stuff. Open your Web-development client (FrontPage, Visual Interdev, etc.).

  2. Enter the following code and save it to your wwwroot directory as myfirstcom.asp

    option explicit  'good asp practice
    Dim obj  'declare your variable for the com object connection
     'here, you are telling the server to access your
     component, 'and its "functions" class in particular
    set obj = Server.CreateObject("myfirstcomponent.functions") 
    'output the greeting! Response.Write obj.GetGreeting( ) 'clear memory< set obj = Nothing %>
  3. Save the file.

  4. Access the file by opening up a browser and going to the proper Web address (http://localhost/myfirstcom.asp for this sample) to view the output.

The next part of this two-part series will show you how to register the component in "component services" on Windows 2000.

(c) copyright 2000, Darwin IT Solutions, All Rights Reserved.

Abel Banda

Return to the .NET DevCenter.