New generation

Ok, so now it’s time for a boring work post. I wish I could show pictures of where I work but no recording devices of any kind are allowed in the building. Security is reasonably tight. I can’t even get back from the toilet without a swipe card.

Yeah, Cecil guys & gals, I have been there before, but this time knocking on the door is not allowed. I once forgot my card when I went to the toilet. I had to get the security person (her name is Barbra) to ring into the office so that someone could walk out with my swipecard. No swipecard, no entry. Our swipecards have our pictures on them too.

Anyway, So I am writing code (no specifics). The design is finished and one of our key problems is interfacing with a particular API. This API is all done in unmanaged C++ libraries and we’re programming in C#. Non-programmers, continue reading at your peril…

You geeks can immediately see the problem of course: native C++ libraries don’t interoperate easily with .Net. You need to recompile the C++ in “common language runtime” mode so that “it just works” or do a bunch of old fashioned dllimports.

Now, we don’t have the source code and the nature of the headers is such that dllimport is out of the question. So, the only solution is a managed C++ wrapper. Do you think there’s a way to generate a managed C++ wrapper to an unmanaged C++ library? No. A tool called Swig can get part-way there but because its implementation of the C++ parsing rules is incomplete it basically choked on 90% of the headers I threw at it.

My team lead was in favour of going ahead and writing the wrapper by hand. So, we started doing that. By the second day I was just getting into the swing of things when my Intellisense stopped working. Well, rather it went into overdrive and killed the CPU on my computer. Why? A bug in the implementation of the intellisense in VC++ 8 (.Net 2005) causes it to go into an infinite loop.

So, that was the last straw. There’s no way you can write a wrapper to a foreign API (particularly not this one) without heaps of code completion. So, I rolled up my metaphorical sleeves and wrote my own parser to parse the headers and then code to generate the wrappers myself. The headers follow a pattern so it was easier than writing a C++ compiler, it was still quite a challenge.

I generated over 60,000 lines of code with my generator today and I am only about half way through (the API is somewhat symmetrical around getters and setters). Imagine having to hand-code 120,000 lines worth of wrappers? I am glad I saved myself (and my team) from that particular nightmare.