August 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Mixing Managed and Unmanaged Code in Your Windows Mobile Applications

  • June 20, 2007
  • 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;

#endregion

namespace classCS
{
   [ComVisible(true)]
   public class MyException : ApplicationException
   {
      const int SecondLevelHResult = unchecked((int)0x81234567);

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

   [Guid("694C1820-04B6-4988-928F-FD858B95C880")]
   public interface csCOM_Interface
   {
      [DispId(1)]
      void Method1();
      [DispId(2)]
      void Method2();
      [DispId(3)]
      int Method3();
   }

   [Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E")]
   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.

Conclusion

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel