Sunday, November 14, 2010

FreePascal, C# and COM

Hi there!

I've been skipping updates of FreePascal lately and since I've received the note that version 2.4.2 was released I've decided to do some investigations on how's the progress of this project.

This time I've decided to check how does it look like to implement a COM server in FP, then to write a client for that server in FP as well and at the end to create a client in C#!

First of all I need to emphasize that the FreePascal compiler as well as it's base library came a long long way since I've last checked them. Great work, guys! I'm really impressed! All I wanted to do was absolutely possible and the general impression was that I'm working with the good old Delphi compiler!

Having that out of my chest let's get back to the task.

We'll need some GUIDs. You can create them online here.

Define an interface in FP:
IHello = interface
procedure DisplayMessage1(S: PChar); safecall;
procedure DisplayMessage2(S: WideString); safecall;

That was easy, right? Just 2 methods that take a string in various form and judging by the name of those they'll display a message somewhere. Let's implement those:
// I've not found where the "ShowMessage" is declared...
procedure ShowMessage(Msg: AnsiString);
MessageBox(0, PChar(Msg), 'Information', MB_OK or MB_ICONINFORMATION);

{ THelloImpl }

procedure THelloImpl.DisplayMessage1(Msg: PChar); safecall;
ShowMessage('MESSAGE USING PCHAR [' + Msg + ']');

procedure THelloImpl.DisplayMessage2(Msg: WideString); safecall;
ShowMessage('MESSAGE USING WIDESTRING [' + Msg + ']');

Again, no magic here...

Let's see how we can do the registration in the COM-subsystem:
CLASS_Hello: TGUID = '{8576CE02-E24A-11D4-BDE0-00A024BAF736}';

'An example COM Object!',

Here you see the call to TComObjectFactory.Create. If you want to know what the specific parameters mean look at the Delphi's documentation online.

Now the final thing left to implement are the Co-classes used to instantiate this object:
  CoHello = class
class function Create: IHello;
class function CreateRemote(const MachineName: String): IHello;


{ CoHello }

class function CoHello.Create: IHello;
Result := CreateComObject(CLASS_Hello) as IHello;

class function CoHello.CreateRemote(const MachineName: String): IHello;
Result := CreateRemoteComObject(MachineName, CLASS_Hello) as IHello;

This way we'll have it easier to deal with the actual instances :)

Let's have some client code, shall we?
Hello: IHello;


Hello := CoHello.Create;
Hello.DisplayMessage1('Hello, world! using COM from FreePascal or Delphi');
Hello.DisplayMessage2('Hello, world! using COM from FreePascal or Delphi');

Important: if it's a console application the CoInitialize(0) does not happen automatically! If you'd have it in a VCL (as in GUI) application the call to Application.Initialize does this for you so you don't need to do that twice.

And there's the whole thing in FreePascal. Let's see now how we can reuse this COM object from C#!
using System;
using System.Runtime.InteropServices;

namespace Client.Net {
interface IHello {
void DisplayMessage1(
[In, MarshalAs(UnmanagedType.LPStr)] string message
void DisplayMessage2(
[In, MarshalAs(UnmanagedType.BStr)] string message

class Hello { }

class Program {
public static void Main(String[] args) {
var hello = new Hello() as IHello;
hello.DisplayMessage1("Hello, world! using COM from C#");
hello.DisplayMessage2("Hello, world! using COM from C#");

This might require a few words of explanation.

First of all the way you create instances of COM objects in C# is by using the "new" keyword, just like any other instances you might have. To sort of "map" the actual COM class to a C# class you define an empty class with 2 attributes: ComImport and Guid. This allows the compiler to do some magic behind the scenes and do the actual instantiation of COM objects instead.
Next is the funky-looking declaration of an interface. It also has 2 attributes on it: the Guid and an information that this interface is just a descendant of IUnknown, so it comes with no type library. This means that the late binding is out of question.
Last but not least - the threading model. Since we've registered the COM object in FP's COM subsystem to use the tmApartment we need to have it compatible in C# code as well. That's what the "STAThread" attribute is for.

Well, this was a wild ride, let me tell you :) But it was well worth it. Now I have the managed and not managed worlds at my fingertips for free!

As usual here's a ready to run example. It's been compiled for .NET 4.0 so if you don't have it you can build it for 3.5 as well.

Have fun!

No comments: