I was a bit shamed about writing something about windows but then I thought there are so many ugly jobs which are respectable so the job of writing windows programs might fit somewhere too. So this is an article about how to write a small C# application which can calls code from a COM object. As a warning some of the informations here might be considered common knowledge to windows people so please don’t be too judgmental about.

Starting point

As a Linux and Unix guy I never use windows as a desktop environment but as a developer I sometime develop code for it. When I’ve heard about C# I thought Mono is mature enough now to develop in Linux everything and then just test in windows so the first try was to write a small C# application on linux using mono.

Installing mono

apt-get install mono monodevelop

The hello world mono application

So here is the “standard” mono example:

using System;

namespace Dela.Mono.Examples{   
public class HelloWorld   {      
public static void Main(string[] args)      
{         
Console.WriteLine("Hello World");      
}   
} 
}

Which can be compiled and run using the following commands:

$ mcs test.cs$ ./test.exeHello World

The “compiled” test.exe also runs on windows after installing the .NET redistributable 2.0 package from M$.

The winforms example

Well this is C# sharp example but it’s not really what I was expecting so the next step was to try a winforms example using the code from http://www.mono-project.com/WinForms_Getting_Started_Guide

Maybe because I am using the ubuntu mono version which is older this code would not compile:

error CS0006: cannot find metadata file `System.Windows.Forms.dll'Compilation failed: 1 error(s), 0 warnings

so I had to google for a while until finding the solution:

$ gmcs test_gui.cs /r:System.Windows.Forms.dll /r:System.Drawing.dll$ ./test_gui.exe

The test_gui.exe also works on windows and behaves in a similar fashion.

The windows part

Ok, so now the tricky part was to find a windows system. Fortunately my old dell laptop had an original windows installed which I almost never used so I decided to do the development here.

The .NET framework

First I had to install the .NET framework, a free download from M$ but which also required various updates such as the windows installer to be performed. In all I had to reboot 3 times.

The development environment

Last time I used Visual C++ it was at version 5 or 6 and it was quite expensive so I was relaxed with the idea of using TextPad as my editor but I found out with some joy that there is a light version (the visual express) which can be used for various research. It has some forms editor and can help you compile the code. Of course for me it seems like hiding the underlying compile commands but in this case I was not eager to know them as my only concern was to go back to my linux environment as fast as possible. Could not complain about the development environment too much as it seemed clean and simple.

Registering a COM DLL

The trouble began after trying to register the COM DLL from the cmd shell. The regsvr32 command was generating the error:

LoadLibrary "The specified module could not be found"

Nothing more detailed could be found. After some more research I found this very nice tool: dependency walker which showed me that the error was in fact generated by some missing dll files which my COM object was referencing. After installing everything I was able to successfully register my DLL which I could then see using the OLE/COM object viewer which I downloaded from M$ site also.

Binding to C#

Using the COM in C# was the simplest part yet as I just had to add a reference to the COM object in my project and the use it as any normal object. I even had auto-completion with the methods and classes inside. As for the language, which I also never used before it was absolutely simple as some sort of Java.

So, finally I completed my research and returned to linux.

Comments:

rik -

Len thanks for this, your research is appreciated. A couple questions, which I could probably work out, but if you answer, it will allow me to get off this M$ box and back to Linux a bit quicker. What did you register with the COM server? Was it test_gui.exe, or did you use the visual express to recompile your class as a DLL? How did you create the reference to the COM object in the other code? (With vis express?) Cheers Rik


len -

Actually rik I should say that I managed to remove all memories of that windows experience from my head so I don’t remember the details. I remember however that I did not managed to do the development I wanted in linux using mono and I had to spend about 1 week doing a prototype in windows using Visual Express :( Can only wish you good luck.


rik -

Oh well, looks like I’ll have to do some work myself. Thanks anyway. Microserf 486 213 579 reporting for duty …


eimer -

When you are a windows developer and want to try to look into a linux system, its the same thing: you have to learn a lot about your system ( better: understanding it). It shows us, that we have to wait many years for real system independent coding (with acceptable speed) (what is a runtime compiler that only runs on windows with all features, you better build an x86 and an x86-64 excecutable that runs on windows a native way, for win32 you have all features with visual studio and c/c++ like C#…, but full control like included asm-files) sorry, my english is terrible