Enjoying the C++ interop C# fun

A master thesis makes you do quite a lot of new things; and try new out. The goal for my thesis is to detect skin. I want to write the whole algorithmn in C++ (I need as much speed as possible and assembly is out of scope ;-)) and give it two kinds of “UI”: one is a Windows application and the second one is a web application. The whole concept should be extenable: this means that another UI could be written later on.

An interop between C# and C++ seems to be the solution to go. WPF and C# for the UI and C++ for the raw image processing and analyzing. But how to do that?

Nothing easier than that! First a solution with a C# project and a C++ DLL project (select Win32 in the C++ node in Visual Studio and then in the wizard DLL) needs to be created. That’s what needed to be ready to start with coding.

The most important thing is to make the functions in the DLL exportable, so that PInvoke (platform invoke) calls work on them. If methods are not exported VC++ gives them some random names (like, for example, ?MethodNameXDPADF) and it’s getting hard to access them. Luckily for us it’s easy to set a method as exported rather easy. The method needs only to be decorated with the __declspec(dllexport) attribute. I also added the extern “C” keywords to make it exportable as a C method.

Since the C++ DLL should do most of the work I thought of giving to the native method only the path to the video file (let the platform open the files) and make the method notify me about the progress. That means I want to register a method that is invoked when progress is made. And that method should be implemented in C# (for the Windows client); to allow me to show a progress bar or something similar; even displaying the current frame would be awesome.

Luckily specifying a callback method is an easy task. The whole thing (in C++) looks like this, where the first argument is the path of the video file and the second one is the notation for a function pointer:

struct ProcessingData
{
    int CurrentFrameIndex;
    int TotalFramesCount;
};

// Processes the given video file.
extern “C” __declspec(dllexport) void ProcessVideo(char* path,
    void (_stdcall *func)(ProcessingData))
{
    for(int i = 0; i < 1000; i++)
    {
        // debug output the path to the file.
        printf(“%s\n, path);

        // fake some processing data for testing purpose.
        ProcessingData data;
        data.CurrentFrameIndex = i;
        data.TotalFramesCount = 1000;

        // invoke the function pointer and pass the
        // data structure.
        func(data);
    }
}

You might also notice that the callback function takes a structure as argument. This structure can contain whatever is required to notify the client about the progress.

As mentioned I’m using PInvoke to invoke the C++ DLL. The C# code looks like this:

static class NativeMethods
{
    // structure that’s passed with the callback.
    [StructLayout(LayoutKind.Sequential)]
    public struct ProcessingData
    {
        public int CurrentFrameIndex;
        public int TotalFramesCount;
    }

    // delegate used for the call back.
    public delegate void CallBackMethodDelegate(ProcessingData data);

    // exported method of the C/C++ DLL.
    [DllImport(“TestDLL2″, ExactSpelling = true)]
    public static extern void ProcessVideo(string path,
        CallBackMethodDelegate del);
}

You might notice that the structure that is used to notify the client has been duplicated in the C# code. Also, I have created a delegate that looks exactly like the function pointer definition in C++. To invoke the ProcessVideo the typical PInvoke DllImportAttribute is used.

From there on invoking and getting the results is straight forward. It’s awesome to see how much speed improvements are gained by something as simple as this and how easy it is to interact with C/C++ code from C# :-)

Published on May 26th, 2008 — Tags: , ,
Comments (5)    digg it!    kick it   

BioMatch: the project winning the Austrian Imagine Cup finals

In the last few months I was involved in a small team (Alex Duggleby - University of Economy Vienna, Marlies Oeltze, Stefan Braito - both University of Natural Resources and Applied Life Sciences Vienna and Christian Liensberger - University of Technology, Vienna) that was creating an application to participate at the Austrian software design finals of Microsoft’s Imagine Cup.

And we won!

Our project is named BioMatch and the idea is to understand how plants (and animals) life together. For example, we want to understand what happens if you add a plant to a habitat (which is a region where plants life); how is that plant going to interact with the others? Is it going to survive, is it even going to spread and/or kill other plants?

To address that problem we have created a viewer that allows the scientist to see how the plants interact with each other and if a plant would survive when being added to a cretain location in the habitat:

This is a very important feature because until now the only way to understand the interaction between plants is to consult big databases with little or even no visualization. Therefore it is very difficult (even for the experts) to understand how plants or animals would life together if put into the same habitat. There are a lot examples where people added new animals or plants to a region (take for example Australia) and where that addition had a big impact; meaning that certain animals/plants completely disappeared from that region.

Another feature that our application provides is a visualization that allows the user to understand how similar plants are. We implemented a self-organizing map that analyzes the plants (we are currently at 2000+ plants, with 300 attributes for each), finds similarities, and displays the results:

The application is primarely aimed at scientists but can also be of great value for biology students (or interested people in general) who want to understand how small changes influence the whole region.

Also, the idea is to bring the extinction of species more into the main stream medias because with the current complex databases it’s quite impossible to understand how changing certain conditions will influence the fauna and flora of the whole world.

Edit: the Austrian Imagine Cup finals’ pictures are now online.

Published on May 24th, 2008 — Tags: ,
Comments (0)    digg it!    kick it   

Microsoft Source Analysis for C#

Microsoft has released a source analysis tool for C#. The tool analyzes the source code of your C# solution and validates it against a set of rules. A set of rules is perhaps a little bit an understatement: it’s a huuuuge mass of rules.

The tool integrates into Visual Studio 2008 or Visual Studio 2005 and can be executed from there. The official blog posting about the release sums it up as follows:

We are very excited to announce the release of a new developer tool from Microsoft, Source Analysis for C#. This tool is known internally within Microsoft as StyleCop, and has been used for many years now to help teams enforce a common set of best practices for layout, readability, maintainability, and documentation of C# source code.

Source Analysis is similar in many ways to Microsoft Code Analysis (specifically FxCop), but there are some important distinctions. FxCop performs its analysis on compiled binaries, while Source Analysis analyzes the source code directly. For this reason, Code Analysis focuses more on the design of the code, while Source Analysis focuses on layout, readability and documentation. Most of that information is stripped away during the compilation process, and thus cannot be analyzed by FxCop.

The ultimate goal of Source Analysis is to allow you to produce elegant, consistent code that your team members and others who view your code will find highly readable.

It’s interesting to note that the tool has been in use by Microsoft for several years. Still, it seems to have some issues; I don’t know if these issues are just in the public release or have also been in the private one. Another reason might also be that I’m just to dumb to understand how to use it…

For me the bugs are the following: It seems as if the tool doesn’t scan all the files of the solution. Also, it is impossible to make it scan each project independent. There is a menu entry to do that (right click on the project to get there) but the tool seems to ignore what has been clicked. It outputs the scan results of a random files scan.

Another thing that bothers me very much is that the rule that doesn’t allow you to use tabs as way to indent code (the rule fires for each indent that you did by using a tab and tells you to use spaces) is enabled by default.

What a pitty! How can somebody rely on spaces as way to indent code… I never understood that and that’s also the first thing that I always have to change after having installed Visual Studio. Tabs are way more customizable. Spaces suck because you can never customize the space they indent. With tabs you set the tab size and you are done. People who like (or even need; not all see like a hawk) a bigger indentation are lost with spaces (they are lost in spaces, hehe). Not to mention that with spaces everybody in the team needs to use the same amout of spaces for the indention! It’s a shame the more you think about it.

I hope these are issues that get fixed very soon; and since the project has been published in the MSDN code gallery we are all free to share the feedback with the team.

Published on May 23rd, 2008 — Tags: , , ,
Comments (2)    digg it!    kick it   

LOLCode and the Dynamic Language Runtime

Will and I spoke with Martin Maly about the DLR (Dynamic Language Runtime) and how to write own language compilers in a few hours.

He showed us his LOLCode compiler that he has written in a few hours (a day in Redmond and a few hours while had was waiting for this flight in Heathrow).

It’s awesome how easily you can write languages on base of the DLR and how expressive and fast they are!

Click here to watch the video on Channel 8.

Published on May 19th, 2008 — Tags: , ,
Comments (0)    digg it!    kick it   

WPF 3.5 SP1 Graphics with David Teitlebaum

Adam (ex Channel 9 dude) is doing a WPF Week on Channel 9. Today he published a video interview that he did with David Teitlebaum.

There is just one word for this video interview: AWESOME!

If you haven’t had the chance to watch it, you definitively should. These new effects and features that are coming with SP1 are really really cool. The new pixel shaders kick ass and also the fast writable bitmaps are awesome.

I wonder if I should use this stuff in my master thesis… could get me a lot of work done and help me a lot…

Awesome work! WPF rocks!

Greg Schechter has also started an awesome series of blog posts on how to use and write own shaders. He first starts with introducting effects, then going into the details and in the end he is showing how to create own shaders with HSLS and the new WPF classes. Check it out.

Published on May 13th, 2008 — Tags: ,
Comments (0)    digg it!    kick it   

Visual Studio 2008 and .NET 3.5 SP1 Beta

Finally, after it has been postponed because of mother’s day (and we all understand that) the SP1 Beta for Visual Studio 2008 and .NET 3.5 has gone public.

The service pack is still beta but holds a few nice improvements and, as we are geeks, need to test it because it is new and everything that’s new needs to be tested. :P

It’s so cool that we get, again, speed improvements with this upcoming service pack. I like this improvement a lot:

Improvements in the WPF designer including a (10-20%) improvement in startup time.

The WPF designer in Visual Studio 2008 behaves sometimes really like a version 1.0 release. Perhaps these improvements remove some of the pain. Sad that we don’t get much speed improvements in the CLR itself; but perhaps they give us something in the next versions.

If you need a guided tour on the SP1 improvements check this website out.

You only want the links to download the new bits (never cared about guided tours yadda-yadda-yadda)? Here you go:

Published on May 12th, 2008 — Tags: , , ,
Comments (0)    digg it!    kick it   

What’s the Micro Framework?

Will and I had the chance to speak with Rob Miles and Dave Baker. They talked us into the .NET Micro Framework and showed us what you can do with C# on small devices (really really small devices) that could be embedded everywhere. Some of these devices are so small that their only reason to live is to work for us as a sensor somewhere.

It was really amazing to see how easy it is to use the Micro Framework and C# to create a solution that runs on a device that’s just as big as my old Nintendo(r) GameBoy (and with a cover would also look exactly the same).

Tune in if you like to program for embedded devices and ever wanted to see your code running in a toaster!

Published on May 11th, 2008 — Tags: , ,
Comments (2)    digg it!    kick it   

Getting started with Silverlight

There are a lot of open questions around Silverlight: What’s Silverlight? What’s better in Silverlight than in its competitors? What’s really making the difference to other technologies? What technology is running Silverlight? Why should a student even care about Silverlight?…

That’s why I met with Jaime Rodriguez (after I have heard that he’s going to come to Vienna) and asked him a huge load of questions on Silverlight. Jaime is a Technical Evangelist for Silverlight and does therefore a lot with Silverlight; and that’s not only demo applications!

I tried to cover a lot of the open questions to give you a great overview on why to choose Silverlight. Since I’m a developer I went also a little bit deeper to understand what exactly is making the “Silverlight experience” run.<

Tune in if you enjoy programming and/or designing for the web and you wondered if there is a way to create a richer experience than the one that is available through plain HTML, CSS and JavaScript.

Published on May 9th, 2008 — Tags:
Comments (0)    digg it!    kick it