July 8, 2020
Hot Topics:

Mixing Managed and Unmanaged Code in Your Windows Mobile Applications

  • By Alex Gusev
  • Send Email »
  • More Articles »

CF.NET and COM Interop

As I said before, CF.NET 2.0 and later supports COM Interop. You can call to COM objects from managed code as well as create components visible to COM; for example, in C#:

#region Using directives
using System;
using System.Text;
using System.Runtime.InteropServices;


namespace classCS
   public class MyException : ApplicationException
      const int SecondLevelHResult = unchecked((int)0x81234567);

      public MyException(string message)
         : base(message)
         //this.`HResult = -10;

   public interface csCOM_Interface
      void Method1();
      void Method2();
      int Method3();

   public class csCOM_Class : csCOM_Interface
      public csCOM_Class()

      public void Method1()
         if (true)
            throw new ArithmeticException("Arithmetic");
      public void Method2()
         if (true)
            throw new DivideByZeroException("DivideByZero");
      public int Method3()
         if (true)
            throw new MyException("Our Custom Exception");

and then use them in C++:

#import "path_to_tlb\classCS.tlb"
   classCS::csCOM_InterfacePtr csObj;
   HRESULT hr = csObj.CreateInstance(__uuidof(classCS::_csCOM_Class));
   hr = csObj->Method1();

This is maybe a slightly weird way of tackling CF.NET, but in case you need COM-like behavior—here you go. More info about COM Interop under Windows Mobile can me found here.


In this article, you briefly explored a few methods of mixing managed and unmanaged code in a mobile environment. Although the Compact Framework is not yet as great as on the desktop, it does allow you to implement many things to reach pretty efficient code, and your 'old buddy' unmanaged C++ helps you there a lot.

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. After working almost a decade for an international retail software company as a team leader of the Windows Mobile R department, he has decided to dive into Symbian OS ™ Core development.

Page 2 of 2

This article was originally published on June 20, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date