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

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 (12)

  1. Tor Bjornrud says:

    The links in the navbar to the left of your site for "Communities->Developer Newsgroups" and "Communities->Developer Sites" are dead.

  2. Wow! That is sooo cool!

  3. Daniel Moth says:


    >>(If you want to know how it works under the

    >> covers, I’m using message queues


  4. Alex Feinman says:

    Very clever.

  5. yangfeng says:

    Good job!

  6. jaybo says:

    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?

  7. Jason Fuller says:

    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).

  8. Craig A says:

    How about an implementation of a web app controlling the camera or GPS unit on a device?

  9. Jonathan Leonard says:

    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.]

  10. Jonathan Leonard says:

    I was referring to the collections of very symmetric read and write methods of your *MessageQueue* class.

  11. Jonathan Leonard says:

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

  12. Jonathan Leonard says:

    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).

Skip to main content