Search code examples
c#comwindows-mobilecompact-frameworkactivex

How to load COM object in smart device project?


I want to create a .NET CF application for Windows Mobile 5. In this application, I want to load a COM object based on the ProgID (or CLSID).

  1. How do I load this COM object in such a way that I can access its methods as if it were just another .NET object?
  2. In addition: how can I configure the projects / solutions in Visual Studio in such a way, that when I debug the application, I am sure that the COM object that is loaded is the one that is installed on the device, not one that may be accessible through the debugger?

I have tried adding the .ocx file as a Reference, but I get an error, and I am not sure about 'question 2'. I have also tried loading the COM object using

Type type = Type.GetTypeFromProgID("my.prog.id")
MyObject myObject = (MyObject)Activator.CreateInstance(type)

...but this results in an InvalidCastException on the second line.


EDIT: For the record: I am working on a 64-bit Windows 7 machine, not sure if this complicates things...


Solution

  • You need a runtime callable wrapper (RCW). See this MSDN article for the basics. For more complex things like ActiveX controls, see this article.