The Wayback Machine - https://web.archive.org/web/20121020054449/http://www.codeguru.com/cpp/com-tech/complus/general/article.php/c16639/How-to-Write-a-COM-Component.htm

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!

Downloads

IT Offers

Comments

  • How to add a variable to the class:

    Posted by codecheater on 03/19/2010 09:04am

    1. use the class manger and request "Add Proeprty" (right next to where, before you added the methid Fly) . 
    
    2. Call (and make this) a Double MyValue , check Put and Get Functions
    
    
    3. In Bird.h add a public section so it becomes 
    
    public: double MyValue; 
    
    4. in Bird.cpp complete the TODO sections for the Get_ and PUT_MyValue methods as follows: 
    
    STDMETHODIMP Bird::get_MyValue(short *pVal) 
    {
     *pVal=this->MyValue; return S_OK; 
    }
    
     STDMETHODIMP Bird::put_MyValue(short newVal) 
    {
     this->MyValue=newVal; 
    return S_OK; 
    }
    
    5.  please doublcheck my syntax is really correct. there is always risk that doiing copy/paste I am  forgetthing or losing  something) 
    
    
    6. All this is inspired by: http://www.codeproject.com/KB/atl/SimpleATLCom.aspx?msg=2763751 
    
    Download the sample application and watch the files. Search for appearences of put_ and get_ in the project files (could be using any text-editor) and it will teach you some details,

    • property add

      Posted by wshcdr on 03/26/2010 01:34pm

      I think the step is ok...

      Reply
    Reply
  • How to add a variable to the class:

    Posted by codecheater on 03/19/2010 08:59am

    use the class manger and request "Add Proeprty" (right next to where, before you added the methid Fly) . Call (and make this) a Double MyValue , check Put and Get Functions in Bird.h add a public secionso it becomes public: double MyValue; in Bird.cpp complete the TODO sections for the Get_ and PUT_MyValue methods as follows: STDMETHODIMP Bird::get_MyValue(short *pVal) { *pVal=this->MyValue; return S_OK; } STDMETHODIMP Bird::put_MyValue(short newVal) { this->MyValue=newVal; return S_OK; } ( please doublcheck my syntax is really correct. there is always risk that doiing copy/paste I may forget something) All this is inspired by: http://www.codeproject.com/KB/atl/SimpleATLCom.aspx?msg=2763751 download the sample application and watch the files. Search for appearences of put_ and get_ in the project files (could be using any text-editor) and it will teach you some details,

    Reply
  • very helpful. And: how to add a variable?

    Posted by codecheater on 03/18/2010 01:49pm

    Hi wshcdr, your article saved my day. Very clear and great guidance! Can you also explain how to add a property/attribute? Using the Class manager it looks simple (like the function you added) but I need help on the details. It just does not work to pass a value to the new attribute. Probably the point is in understanding how to complete the TODO sections and also the GET and PUT functions. An illustrated guide would be great! Best, codecheater

    Reply
  • very helpful. And: how to add a variable?

    Posted by codecheater on 03/18/2010 01:48pm

    Hi wshcdr,
    your article saved my day. Very clear and great guidance!
    Can you also explain how to add a property/attribute? Using the Class manager it looks simple (like the function you added) but I need help on the details. It just does not work to pass a value to the new attribute. Probably the point is in understanding how to complete the TODO sections and  also the GET and PUT functions.
    An illustrated guide would be great!
    
    Best,
    codecheater

    Reply

Whitepapers and More

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds