Home > Delphi > Using .net binaries from Delphi

Using .net binaries from Delphi

Yesterday I had an interesting challenge at work, namely to figure out how to use .net binaries from Delphi. To make a long story short – the company I work for interfaces with several third-party systems, and one of them just happens to be written for the .net framework. The docs told us that the binaries are exposed as COM objects and can thus be used via scripting (powershell), visual basic script – and well, anything that can handle IDispatch more or less. This should be a breeze right?


Having installed the server and the client libraries on my test machine I started to look for a type library. I have never imported or invoked .net from Delphi (but I have used “native” dll’s from C#) so naturally I  was hoping we would find something in the binaries folder. But no such luck

In short: I could create the COM objects via createOleObject(), but that sort of leaves you blind. And the docs didn’t really explain the methods as much as it focused on the XML parameter syntax. But getting a response via com was a start. Even an error message is positive when creating a bridge. I pieced together a small wrapper class that simply created an instance of the COM object in the constructor, with X number of methods that just used IDispatch to invoke the methods we knew about. But what I really wanted was a typelibrary and better control over how Delphi deals with scenarios like this; scenarios that will no doubt become more and more frequent in the years to come.

Some work later and it turns out you can export a typelibrary manually providing the classes and interfaces you need have been signed as “visible” to COM. This is done by adding: using System.Runtime.InteropServices; to the .net uses section. You also have to mark the class you wish to export as such:

public class SomeClass : ISomeclass

The ISomeClass is just an interface that you define above the class, which just mirrors the methods you want to export. It is recommended that you mark each method you want to expose with the [comvisible(true)] signature. Hence the “classinterfacetype.none” in the header (as opposed to exposing the whole shabam).

Generating a typelibrary

The solution came in the form of the dark side of the… eh, I mean that wonderful utility that is Visual Studio. Once we knew the binaries did indeed export the methods/interfaces we wanted – we started up the visual studio command-prompt (remember to run as admin). This shell has a few extra commands, one of them being: tlbexp, which takes a .net binary and generates a Delphi compatible type library. You can read more about this utility here: http://msdn.microsoft.com/nb-no/library/hfzzah2c%28v=vs.80%29.aspx

With a typelibrary now in place I fired up Delphi Xp4, imported the typelibrary (the import wizard crashed 3 times before it worked, so be warned) and generated a unit.

Et voila! We finally had a clean and direct way of speaking to .net 🙂

XML mapping

A lot of .net libraries are based on object mapping. In short, all parameters are strings and you are expected to push in a huge wad of XML – which on the .net side is turned into an object. Well Delphi can do that to! Once I had a full overview of the XML saved to disk, I clicked on file -> new -> other -> XML. Then imported the XML files and generated the object mapping classes.

Delphi really have a lot of hidden gems. The XML mapping alone is a huge timesaver and it’s a shame so few people use it – and even worse that Embarcadero dont promote it more in the IDE (and perhaps give it a facelift and tlbexp support?).

Stuff to keep in mind when working with .net systems:

  1. Object mapping is a must, play around with the Delphi XML mapper, it really is a fantastic once you get the hang of it. A lot of .net systems (especially database services) use object mapping, so you better get used to it.
  2. Each build is unique (!) Even if the .net code has not changed, the interfaces can have new GUID signatures. So you must remember to export and re-generate the typelibrary whenever you update. Unless of course the coder have manually set the GUID, in which case it requires less maintainance.
  3. Install visual studio, either the free version or a trial (or buy it, it’s a bargain compared to Delphi). It will make your life so much easier.
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: