How to Write a COM+ Component

Introduction

COM+ is a great framework for Enterprise Development. I now want to introduce to how you can write a component to be use by the COM+ runtime.

let’s go!

Implementation

Open VS2008 (VS2003 and VS2005 are also okay) and select IDE File | New | Project…. The Project Wizard will pop up. Choose Visual C++ | ATL and then name it component. Click OK. Click next on the first “Welcome to the ATL Project Wizard” and select Support COM+ 1.0,

Click Finish so that the IDE generate the code stuff for you.

Now you can add the interface that will be used in the COM+ runtime. Select Project | Add class… and then choose ATL | ATL COM+ 1.0 Component

Click Add and name name it Bird. Click next to the COM+ 1.0 page, and then check IObjectControl and IObjectConstruct, and I wanna support transaction and I choose Required.

Click Finish.

Now you will add a method Fly by adding the following:

[id(1), helpstring(“method Fly”)] HRESULT Fly([out,retval] LONG* lSpeed);

and implement it as follows:


STDMETHODIMP CBird::Fly(LONG* lSpeed)
{
// TODO: Add your implementation code here
*lSpeed = 0xbee;
return S_OK;
}

That’s all regarding the code. Now you will install it!

First, create an empty COM+ application

  1. In the console tree of the Component Services administrative tool, select the computer on which you want to create an application.
  2. Select the COM+ Applications folder for that computer.
  3. On the Action menu, point to New, and then click Application. You can also right-click the COM+ Applications folder, point to New, and then click Application.
  4. On the Welcome page of the COM+ Application Install Wizard, click Next, and then in the Install or Create a New Application dialog box, click Create an empty application.
  5. In the box provided, type a name for the new application. (Note that the following special characters cannot be used in an application name: \, /, ~, !, @, #, %, ^, &, *, (, ), |, }, {, ], [, ‘, “, >, <, ., ?, :, and ;.) Under Activation type, click Library application or Server application. Click Next.
    Note that a server application runs in its own process. Server applications can support all COM+ services. A library application runs in the process of the client that creates it. Library applications can use role-based security but do not support remote access or queued components.
  6. In the Set Application Identity dialog box, choose an identity under which the application will run. If you select This user, type the user name and password. You must also retype the password in the Confirm password box. Click Next. (The default selection for application identity is Interactive User. The interactive user is the user logged on to the server computer at any given time. You can select a different user by selecting This user and entering a specific user or group.)
    Note that the Set Application Identity dialog box appears only if you selected Server application for the new application’s activation type in the COM Application Install Wizard’s preceding dialog box. The identity property is not used for library applications.
  7. In the Add Application Roles dialog box, add any roles you want to associate with the application. By default, only the CreatorOwner role is defined.
  8. In the Add Users to Roles dialog box, populate each role you created in the last step with the users, groups, or built-in security principals to which you want to grant the privileges associated with that role. By default, the interactive user is placed in the CreatorOwner role.
  9. Click Finish.

Now you can add a component to a COM+ application

  1. In the console tree of the Component Services administrative tool, select the computer hosting the COM+ application.
  2. Open the COM+ Applications folder for that computer, and select the application in which you want to install the component(s).
  3. Open the application folder and select Components.
  4. On the Action menu, point to New, and then click Component. You can also right-click the Components folder, point to New, and then click Component.
  5. On the Welcome page of the COM+ Application Install Wizard, click Next, and then in the Import or Install a Component dialog box, click Install new components.
  6. In the Install new components dialog box, click Add to browse for the component you want to add.
  7. In the Select files to install dialog box, type the filename of the component to install or select a filename from the displayed list. Click Open.

    After you add the files, the Install new components dialog box displays the files you have added and their associated components. If you select the Details check box, you will see more information about file contents and the components that were found. Note that theunconfigured COM components must have a type library. If COM+ cannot find your component’s type library, your component will not appear in the list. You can also remove a file from the Files to install list by selecting it and clicking Remove.

  8. Click Next, and then click Finish to install the component.

Ok, you have installed it!

Next you will use it on a remote machine. Right click the COM+ Application and Export. Note that you should choose Application Proxy there, then choose a folder to store the proxy. Include a msi file and a cab file. Copy them to remote machine, and click the msi file. The application proxy information is installed into the COM+ catalog and is visible in the Component Services administrative tool. You can find the component in %Drier%\Program Files\ComPlus Applications folder. Now you can write a client application. For instance, if you use C#, you can reference this DLL from %Drier%\Program Files\ComPlus Applications
and use the Bird.Fly() method.

Other stuff

This is only a simple guide to COM+ programming! Please send your feedback!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read