COM+ in .NET

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: ATL/COM/COM+

Introduction

In this article, I will explain the step-by-step procedure to develop and use COM+ components in the .NET environment and about “Component Services Explorer.”

Before going to the coding part, let’s analyze how COM+ components and applications are managed. By using the “Component Services Explorer,” we can manage COM+ components and applications in the local machine or in the remote machine (of course with administrative privileges). We can see the Component Services Explorer in every Windows 2000 machine; it’s a Microsoft Console snap-in. To open this, click Start, Settings, Control Panel from the control panel dialog; open Administrative Tools; and then Component Services.

We will use this Component Services Explorer very often to create and configure COM+ applications, import, and configure COM or .NET components, export, and deploy our applications and administer our local machine. We can even administer COM+ on other machines on the network if we have administrative privileges on those machines. A COM+ application is a logical group of COM+ components. You can see all the COM+ applications installed in your machine by opening the Component Services Explorer and expanding Component Services, Computers, My Computer, COM+ Applications. Every icon in the COM+ applications folder represents a COM+ application. Each COM+ application contains one COM+ application. Components must be explicitly imported into the component services explorer to take advantage of COM+ services. Figure 1 shows the Component Services Explorer.

Figure 1—The Component Services Explorer

The Component Services Explorer offers a hierarchical approach to managing COM+ services and configurations. The hierarchy is as follows:

+Computer
     +Applications
          +Components
               +Interface
                    +Methods

A computer contains applications, and an application contains components. A component has an interface, and an interface has methods. Each element in the hierarchy has its own configurable properties; select these properties from the pop-up menu by right-clicking the corresponding element.

Building the COM Component

Okay, we learned something about the Component Services Explorer; let’s use it by writing a component. Here we will develop the classical hello world type of COM+ component using ATL 7.0.

  1. Open Visual Studio .NET and click File, New, Project. You will get a New Project dialog box. Select Visual C++ project in project Types and ATL Project in the Templates pane. Name it MyCOM, browse to your favorite location, and click OK.
  2. This is our first component so we will keep things simple, at least for the time being. In the ATL project wizard application settings, deselect the Attributed check box; for simplicity, we won’t use an attributed project. Don’t check Support COM+ 1.0; this selection will add some interfaces that we don’t need for this simple hello world example, as shown in Figure 2. Click Finish.
  3. Figure 2—Application Settings for MyCOM Project

  4. Click Project, New Class from the Add Class dialog box. Select ATL Simple Object from the templates pane and click Open. You will get the ATL Simple Object dialog box. Type MyMessage in the Short Name filed and MyCOM in the CoClass field. The remaining fields will be filled automatically, as shown in Figure 3. Click the Finish button. We will keep the default options.
  5. Figure 3—ATL Simple Object Wizard Names settings

  6. Now we will add a method to our interface. Select Class View in the project explorer. Right-click the IMyMessage interface and select Add, Add method; you will get the Add Method Wizard. Enter ShowMyMesssage in the Method Name field; all other settings are default. Click the Finish button.
  7. Go to the MyMessage.cpp file and implement the ShowMyMesssage function of CMyMessage Class as shown below:
    STDMETHODIMP CMyMessage::ShowMyMessage(void)
    {
    MessageBox(GetActiveWindow(), "This is my first COM+",
                                  "MyCOM", MB_OK);
    
        return S_OK;
    }
    
  8. Now we can compile and build the DLL.

All COM+ components reside in the DLL, and that DLL must contain a type library embedded in it as a resource. ATL will build the DLL for you and add a reference to the type library in the resource file, MyCOM.rc. You do not need to register the COM+ components; ATL will register then for you. COM+ maintains its own components registration and configuration repository.

Creating the COM+ Application

Open Component Services Explorer and expand the My Computer, COM+ Applications folder. Right-click COM+ Applications and select New, Application from the pop-up context menu. This opens the application’s Install wizard. Click Next in the wizard, and then click the “Create an empty application” button. Enter a name for the new application as My First COM+ and select the Library application in the Activation option because we are going to create in the process server. A Server application indicates that the components run on their own process. Entries should look like Figure 4, shown below. Click the Next and then the Finish buttons.

Figure 4—A new COM+ application, naming and configuring it to be a library/server application

We have added a new COM+ by right-clicking My First COM+ and selecting the properties you will use on the properties page. On the the General tab, you have a name and description that you can change at any time. You also have property pages for all applications, components, interfaces, methods, roles, and subscriptions. Close the properties sheet. If you expand the components folder in My First COM+, it’s empty—as expected. So far, we’ve created a Component and an application; now, we need to add the component to the application.

Adding a Component to the COM+ Application

  1. To add a new component into the application, right-click the component folder and select New, Component from the pop-up context menu. We get the Component Install Wizard.
  2. From the wizard, click Next and select Install new component(s) from the three options; this will open the standard File Open dialog box. Browse to MyCOM.dll and select it. The Wizard will show you all the components it selected; in our case, there was only one, as shown in Figure 5. You can also Add/Remove DLLs by using the Add/Remove buttons. Click the Next and then the Finish buttons to complete the new component installation.
  3. Figure 5—Component Install Wizard displays selected Component(s)

  4. As we know, the type information is embedded in the DLL; COM+ knows about your components, interfaces, and methods. To verify that COM+ imported the component correctly, expand the interfaces and methods folder under the MyCOM.MyCOM.1 folder. You can see IMyMessage under the interface and ShowMyMessage under the methods folder, as shown in Figure 6.
  5. Figure 6—MyFirstCOM+ application and its component

Testing the Client

We’ve finished the component and application; now it’s time to develop a small client to test our COM+. Create a new dialog-based MFC Application project and name it COMTest. In OnButtonClick, add the following code. Copy the DLL to the client folder.

void CCOMTestDlg::OnBnClickedButton1()
{
    ::CoInitialize(NULL);

    HRESULT hRes = S_OK;
    IMyMessage* pMyMsg = NULL;

hRes = ::CoCreateInstance(CLSID_MyCOM, NULL, CLSCTX_ALL,
                          IID_IMyMessage, (LPVOID*)&pMyMsg);

    hRes = pMyMsg->ShowMyMessage();
    pMyMsg->Release();

    ::CoUninitialize();
}

Add the following line to the include section of the line to import the library:

#import "MyCOM.dll" no_namespace named_guids

When you run and click the button, you will get the following message box, as shown in Figure 7.

Figure 7—Message box from your first COM+ component

The main advantage of using COM+ is that you can configure a component or an application containing it without changing any code on the client side or object side. We will explore how to change the application activation. Normally, the application type is decided during design time. Changing the application type may have an impact on security. Okay, now we will change our Library Application (in process) to a server application out of process.

Right-click My First COM+ and select Properties. From the properties page, select the Activation tab and select Server Application, as shown in Figure 8.

Figure 8—Application activation tab

How COM+ differentiates between library and server applications is, in the case of library the client loads the DLL into the process address space. In the case of server application activation configuration, COM+ generates a special process called “Surrogate process (dllhost.exe)”; this loads the DLL. COM+ places the a proxy in the client process and a stub in the surrogate process. This connects the client to the object. To verify these points, configure the server application and run the application. In the Component Services Explorer, change the view to Status View. From the explorer, take the process ID (PID) and locate this PID in the Windows Task Manager. You will find the dllhost.exe image name, as shown in Figure 9.

Figure 9—COM+ Surrogate process for the Server application

I am a programmer, living in Hamburg. Drop me mail for comments and criticism at rishibala@hotmail.com.

Downloads

Download source – 107 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read