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.
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
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:
For this quick sample, we will be creating a simple public function.
Save the project (with new names).
Double-click on the
functions class from the project panel to get started.
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
Save the project.
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.
Save The Project.
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
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.
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:
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.
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.).
Enter the following code and save it to your
wwwroot directory as
<% 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 %>
Save the file.
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.
Return to the .NET DevCenter.
Copyright © 2009 O'Reilly Media, Inc.