Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

Using legacy plug-ins with .NET

Rate me:
Please Sign up or sign in to vote.
3.29/5 (11 votes)
15 Jun 2004CPOL3 min read 69.3K   2   25   16
Anyone who ever worked with Windows development before .NET should know how to load DLLs dynamically. However, it's not possible to convert function pointers to something like a delegate. So, I created a small trick to use legacy plug-ins still using PInvoke.

Introduction

Anyone who ever worked with Windows development before .NET should know how to load DLLs dynamically using LoadLibrary, GetProcAddress and FreeLibrary from the Windows API. However, to my surprise, it's not possible to convert the pointer returned by GetProcAddress to something like a delegate. So, I created a small trick to use legacy plug-ins still using PInvoke. Notice that when I say "legacy", I'm referring to any code not written to .NET, but Win32.

Background

In the far far past, in the days I began to learn C#, I tried to call legacy DLLs within .NET with some code derived from what I used to do using Delphi. When I got tired of trying (even using unsafe) and posted the question on a MS newsgroup, they said that was really not possible. The problem is: I wanted a solution. Now that I have one, I decided to share it.

The trick is far more simple than it sounds, but requires a few more coding steps. First, we need some DLL to work as our plug-in. I will not write any code for that, we will just suppose it has only one method and we can import it with the following code:

C#
[DllImport ("plugin.dll")]
private extern static int SomeMethod(int a, int b);

This way we can imagine any DLL written in any non-.NET language. The problem with this is that we can access the method in only one specific DLL and we want to use an uncertain count of libraries this way.

Using the code

We want to access multiple legacy DLLs dynamically, and we can't convert and call the function pointer returned by GetProcAddress as, for example, a .NET delegate. Although, I still can use the pointer returned by LoadLibrary. So, we could create another legacy DLL imported with this code:

C#
[DllImport ("plugin.dll")]
private extern static int SomeMethod(IntPtr lib, int a, int b);

This is where the trick lies. We have a well known DLL but it isn't the plug-in call yet. Notice the first parameter in this method: this could be anything, but we'll use the return value of the LoadLibrary function. Once we can't call the dynamic function directly, we use another legacy library as a front-end just to call the plug-in. The front-end is exactly the same as the plug-in libraries, but every function has that IntPtr parameter as it's first parameter. It's very important to remember that every method of the plug-in libraries must have a correspondent in this front-end in order to be used.

Now that the trick is explained, it's very simple to create some class to be extended to allow us to load the legacy plug-ins in a more object-oriented fashion.

C#
using System;
using System.Runtime.InteropServices;

public abstract class LegacyPlugin: IDisposable {

   [DllImport ("kernel32.dll")]
   private extern static IntPtr LoadLibrary(string fileName);

   [DllImport ("kernel32.dll")]
   private exten static bool FreeLibrary(IntPtr lib);

   private IntPtr lib = IntPtr.Zero;

   protected LegacyPlugin(string fileName) {
      lib = LoadLibrary(fileName);
   }

   ~LegacyPlugin() {
      Dispose();
   }

   public void Dispose() {
      if(lib == IntPtr.Zero) return;
      FreeLibrary(lib);
      lib = IntPtr.Zero;
   }

   protected IntPtr Library {
      get {
         if(lib == IntPtr.Zero)
            throw new InvalidOperationException("Plug-in library is not loaded");
         return lib;
      }
   }
}

This class can be extended for each different type of legacy plug-in your application uses. All you need to do is declare the DllImports for the functions in the front-end DLL. So, our example plug-in could be accessed using the following class:

C#
using System;

public class SomePlugin: LegacyPlugin {

   // Notice we changed the name of the method because of the public version
   // DLL name search is case-insensitive but C# source code is sensitive
   [DllImport ("plugin.dll")]
   private extern static int someMethod(IntPtr lib, int a, int b);

   public int SomeMethod(int a, int b) {
      return someMethod(Library, a, b);
   }
}

Points of Interest

I hope to be clear that we're calling the front-end library (which must be created in a legacy language like Visual C++ or Delphi) and that this front-end still has to call GetProcAddress, convert its return value to a function, call it and return the return value of this function.

Another way to accomplish this is to create, static or dynamically, a class for each plug-in library we have to load. This implies in an obvious overhead for the application, not to mention it's more time-consuming.

Still, I'm not sure if it's possible to do the pointer conversion (from GetProcAddress) to a delegate using Delphi 8, once they tried to keep compatibility with previous versions. My first supposition is no. If anyone has tested, please let us know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Brazil Brazil
architecture student (5/10)​ · designer · developer · geek

Comments and Discussions

 
QuestionEmit? Pin
mscholz21-Jun-04 21:21
mscholz21-Jun-04 21:21 
AnswerRe: Emit? Pin
NeosMatrix8-Jul-04 2:58
NeosMatrix8-Jul-04 2:58 
GeneralRe: Emit? Pin
mscholz11-Jul-04 19:31
mscholz11-Jul-04 19:31 
GeneralRe: Emit? Pin
Leonardo Pessoa15-Jul-04 2:58
Leonardo Pessoa15-Jul-04 2:58 
GeneralRe: Emit? Pin
mscholz15-Jul-04 3:29
mscholz15-Jul-04 3:29 
GeneralRe: Emit? Pin
Leonardo Pessoa15-Jul-04 3:49
Leonardo Pessoa15-Jul-04 3:49 
GeneralRe: Emit? Pin
Fabrizio Carrai19-Jan-06 21:53
Fabrizio Carrai19-Jan-06 21:53 
GeneralRe: Emit? Pin
mscholz19-Jan-06 22:40
mscholz19-Jan-06 22:40 
GeneralRe: Emit? Pin
Fabrizio Carrai20-Jan-06 1:51
Fabrizio Carrai20-Jan-06 1:51 
GeneralRe: Emit? Pin
walker_network_ranger14-Nov-06 23:23
walker_network_ranger14-Nov-06 23:23 
GeneralRe: Emit? Pin
newportgm15-Nov-06 0:52
newportgm15-Nov-06 0:52 
GeneralRe: Emit? Pin
walker_network_ranger19-Nov-06 2:47
walker_network_ranger19-Nov-06 2:47 
AnswerPInvokeStackImbalance Pin
hahnl14-Apr-06 4:50
hahnl14-Apr-06 4:50 
AnswerRe: PInvokeStackImbalance Pin
Dieter Buecherl19-Jun-07 0:36
Dieter Buecherl19-Jun-07 0:36 
AnswerRe: Emit? Pin
bibiboule29-Jan-07 3:44
bibiboule29-Jan-07 3:44 
AnswerRe: Emit? Pin
bibiboule29-Jan-07 3:58
bibiboule29-Jan-07 3:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.