2

I've created an application in Delphi that is capable of loading plugins from a dll. The dll exports one function which looks like this

function GetPlugin: IPluginInterface;

IPluginInterface is a ActiveX Type Library. I figured because I was using a type library I could then use C# or any other language to export IPluginInterface from a dll, though after a bit of googling I found out this is not the case as there is a lot of talk of managed and unmanaged code and reasons why this can't be done. So my question is, am I still able to create a plugin in C# that will export a function like above? If not, what languages are able to do that apart from C++?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Meh Nada
  • 219
  • 2
  • 6

1 Answers1

5

So my question is, am I still able to create a plugin in C# that will export a function like above? If not, what languages are able to do that apart from C++?

All of the mainstream programming environments on Windows can produce and consume COM interfaces. C, C++, C#, VB6, VB.net, Delphi and so on. So, yes, you can use COM interfaces for your task.

As a point of detail, what you can't do is pass an interface out of a Delphi DLL as the return value of a function. That's because Delphi uses non-standard semantics for function return values. Instead you need to return it via an out parameter.

So you'd need to write your GetPlugin like this:

procedure GetPlugin(out Plugin: IPluginInterface); stdcall;
Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Many thanks. I will do that. If I'm creating the plugin in C# how would I export "procedure GetPlugin(out Plugin: IPluginInterface); stdcall;" from the c# dll? – Meh Nada Feb 24 '13 at 14:04
  • There are a few of different ways. You can implement a COM server in C#. Or you can use Robert Giesecke's [Unmanaged Exports](https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports). – David Heffernan Feb 24 '13 at 14:07
  • +1 Never knew (or even considered) Delphi did that with return values. – WhozCraig Feb 24 '13 at 15:00
  • About function return values: I think that's wrong, you can return an interface pointer (or any other pointer) just fine. The problem described in the linked question about WideString is probably due to WideString reference counting; I think declaring it as PWideChar instead would have worked. – Ondrej Kelle Feb 24 '13 at 15:13
  • @TOndrej Delphi implements function return values by passing the return value as an extra implicit var parameter, passed after all other parameters. That's different from all other tools on the Windows platform that I know of. They implement function return values passed by value from function to caller. You mention reference counting. You realise that interfaces are automatically reference counted also? – David Heffernan Feb 24 '13 at 15:17
  • @DavidHeffernan Are you saying you cannot export functions from a DLL written in Delphi? – Ondrej Kelle Feb 24 '13 at 15:19
  • 1
    @TOndrej No, I am not saying that. That would be a ridiculous assertion. – David Heffernan Feb 24 '13 at 15:22
  • @TOndrej It's quite easy to write a Delphi DLL and a C# program that confirms what I am saying. – David Heffernan Feb 24 '13 at 15:43
  • The program is written in Delphi and I need to write a C# dll that exports an interface. And yes, I do use PWideChar instead of WideString. Yes I do realise that Interfaces are reference counted, and I set the pointer to IPluginInterface to Nil just before the program exits to avoid memory leaks or other errors... – Meh Nada Feb 24 '13 at 16:10
  • @Meh All those comments above relate to TOndrej's comment. They were not directed at you. – David Heffernan Feb 24 '13 at 16:45
  • @TOndrej Is there any chance that we could tidy up this comment trail. It causes confusion. I can give you my test projects if you still don't believe me. – David Heffernan Feb 24 '13 at 18:18