John Lam 1997 — 2026

I've gotten the MC++ Religion

I had earlier vowed to never write a line of MC++ code.

I would now like to publicly recant that vow after I spent the better part of this evening (and early morning hours) working with the managed C++ extensions.

I was writing some code that needed to be unmanaged. But I wanted to pass the results of my computations (an array of structs) back to managed code. I started off by writing an unmanaged C++ DLL, and I used P/Invoke to marshal the array of structs back to my managed code. Then, just for fun, I decided to dive into MC++ and do the same thing by writing a managed wrapper class around my unmanaged class. After fighting with the syntax of MC++ a bit, I managed to get both pieces of code working.

Then I decided to run a benchmark: initial results show that the MC++ wrapper case was significantly faster than the P/Invoke case.[1] The MC++ case definitely seemed a lot more “natural”, and I was amazed at the ease with which I could combine my managed and unmanaged C++ code inside of the same assembly. The P/Invoke case, on the other hand, was full of truly grotesque calls to CoTaskMemAlloc() and Marshal.DestroyStructure() and redundant memcpy’s (at least 1 layer’s worth). A thousand apologies to the VC++ team. This technology totally rocks!

[1] Benchmark numbers to follow, but I don’t have absolute confidence in code that I wrote at 4am. Both versions of the code produced correct results, but I want to make sure that I didn’t do something stupid. Besides, I want to know what I’m really measuring as well. Stay tuned.