Share via


Calling managed code from native

A question that always pops up is "Can I call managed code from my native process?" And the answer was always "no".  Sure, you could create a window and pass window messages between a native process and a managed process, but that's a pretty limited solution, and not a rich programming model.  So I wrote a tool to let you do it.  It's called Mirror and the source and binaries are available on https://windowsmobile.members.winisp.net/Interop.zip.

Here's how it works.  You write a managed assembly that you want to be callable from native code.  You'll have to restrict yourself somewhat when it comes to what language features you can use, because Mirror doesn't support everything.  But it won't feel too constricting: you can use constructors, methods, properties and indexers.  And as for what types you can pass around, you can use all the primitive types (ints, bools, etc.), along with strings and enums.  You can even pass around instances of your own classes, as long as the classes themselves are Mirror-compatible.

Then run Mirror's NativeCodeGenerate.exe over your managed assembly, and it will generate C++ code for a class whose interface mirrors that of the managed class.  Your app can call that C++ class and it will take care of calling into the managed class.  More details are in the readme, but that's pretty much it.

(If you want to know how it works under the covers, I'm using message queues, which are a mechanism CE provides for cross-process communication.  They're basically pipes.  Mirror contains a reusable managed class for dealing with message queues, so if all you need is raw cross-process communication in your own app, you can steal that class.)

 - Jason Fuller

Comments

  • Anonymous
    September 07, 2005
    The links in the navbar to the left of your site for "Communities->Developer Newsgroups" and "Communities->Developer Sites" are dead.

  • Anonymous
    September 07, 2005
    Wow! That is sooo cool!

  • Anonymous
    September 07, 2005
    Nice!

    >>(If you want to know how it works under the
    >> covers, I'm using message queues

    FYI:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/point2point_message_queues.asp?frame=true

  • Anonymous
    September 07, 2005
    Very clever.

  • Anonymous
    September 08, 2005
    Good job!

  • Anonymous
    September 11, 2005
    Is this solution cross platform? That is, can it work on XP as well as Win CE?

    I'm curious since I have I have a single codebase supporting both platforms. If not, would it be hard to retrofit?

  • Anonymous
    September 13, 2005
    I didn't write it to be cross platform, since the desktop already has better COM/managed interop than CE. But if you did want to use Mirror on the desktop, it probably wouldn't be hard. The only difference I can think of is that XP doesn't have CE-style message queues, so you would need to replace my implementation of the MessageQueue class with your favorite inter-process communication mechanism on the desktop (probably named pipes).

  • Anonymous
    December 09, 2005
    How about an implementation of a web app controlling the camera or GPS unit on a device?

  • Anonymous
    September 11, 2008
    Why don't you factor the read and write method sets into only a few (possibly one) higher-order function for each?  [I will do this myself with your code when I get some free time.  It would certainly be much nicer for your readers if you would try to make tighter code.]

  • Anonymous
    September 11, 2008
    I was referring to the collections of very symmetric read and write methods of your MessageQueue class.

  • Anonymous
    September 11, 2008
    P.S.  I think it is really awesome that you MS guys share your code like this for the world to see and critique.

  • Anonymous
    September 12, 2008
    Your wParams and lParams should be IntPtrs instead of uints.  (If you look at a native WNDPROC, it uses UINT_PTR which will be 64-bit on a 64-bit OS).