Building a C# Wrapper for ViiM SDK
This is my first attempt at writing a more personal post for the GuineaPig. I hope people like it and maybe even relate with.
This is the story behind the C# Wrapper creation for the ViiM SDK v1.0.0 (well actually ViiM SDK v1.0.1, because we had to make small changes for it to work). But for everything in this post to make sense, I ought to tell you a bit about my background history. I took the Master in Electronics and Programming Engineering and after some time looking for a job, I found CoVii and they accepted me in the team. I feel very fortunate for this, because to be able to integrate such a young, energetic and smart team in a start-up theme is the best (and maybe hardest?) first job one could ever wish. I have learnt a lot since the first day, I’ve made my mistakes and I have also had my “little” victories. Although my official role is a developer in the ViiM SDK project, I have done a bit of everything in our headquarters: from messing with Adobe Illustrator and writing for the blog, main webpage and the ViiM wiki to help moving furniture around It’s really pleasing when our opinions matter, help moving the company forward and we’re not just another number.
I hope that at this point you have understood my place here. Now on to the good stuff.
After taking part in the ViiM SDK v2.0.0 beta development I’ve been responsible from day one for the C# wrapper for ViiM SDK v1.0.1.
Getting my hands dirty!
As you may know, ViiM SDK was developed from C++ and is, obviously, meant to be used in a C++ environment. But one of the following improvements after the release of the V1.0.0 was the creation of wrappers for other languages. So I started messing around with C#, which I had never studied back at the university. I gradually got to know Microsoft’s language and understanding the interface/graphical/events oriented language.
Then it was time to try and connect the ViiM SDK C++ dll with C# in a hello world style sample. Understanding the concept and getting the settings right between the unmanaged C++ and the managed C# were probably the most difficult issues I’ve been through during this time. I ended up coming with the solution of having a C++ dll (the Bridge) with calls to ViiM’s most basic methods. On the other hand the C# application would call the methods on the bridge through DLLImport/DLLExport.
Complicated stuff, he? Look at Canotilho’s and Bárbara’s faces
Once I got ViiM’s start() working (which was reason for a mini celebration:), getting integers or float numbers proved to be quite straightforward. But then came the color and depth images. Since these images are given as a pointer to char from ViiM SDK, after a bit of struggle I was able to transform them into Bitmap images. The problem was that in order to use pointers (exception made to the IntPtr) the application had to be in unsafe context, and this was something we really didn’t want. Plus, enums inside classes in C++ are used like Class::enumVariable and in C# Class.EnumName.enumVariable, which are obviously incompatible. Moreover, I wanted programmers to have a similar experience when coding with the wrapper as with ViiM SDK in C++. These are a few examples as why I decided to have a C# dll between the bridge and the application. This way all the necessary transformations are done inside this dll and then passed to the user’s application in a safe context. This file was called the Wrapper.
After the working proof of concept of this structure, the experiments were over and the ViiM SDK’s C# Wrapper (bridge.dll + wrapper.dll) development started for real.
In the end, ViiM SDK’s samples were replicated in C# and the final bug’s pursuit took place.
But then I found that it really wasn’t the end of the road just yet! When running the samples, I noticed that the wrapper dll was copied to the sample’s binary folder and this was something we didn’t want. So after some research I found out about the Global Assembly Cache (GAC). Registering the wrapper DLLs in the GAC, allowed the integration of their references on the C# project without them being copied when run.
Finally an installer was made and the wrapper was ready to be uploaded to ViiM’s webpage.
This was only a summary of what’s been going in my desk for the last 2 months give or take (and still had time to work on two more small projects and missing a few days of work due to pharyngitis and otitis!). I learnt a lot about C# and even more about crossing different languages.
I believe the C# wrapper turned out to be a great accessory for ViiM SDK v1.0.1 and, as similar and easy to use as the C++ SDK, everybody who knows ViiM SDK and wants to develop in C# will find it a rewarding experience.
Computer Vision Inside Technology ViiM SDK 08/03/2013
Till next time!