Using legacy plug-ins with .NET

转载 2005年02月28日 14:39:00
By Harkos




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.


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:

[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:

[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.

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() {

   public void Dispose() {
      if(lib == IntPtr.Zero) return;
      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:

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.



  • xiaguamanshu
  • xiaguamanshu
  • 2013年12月01日 19:33
  • 2551

MVN编译问题Maven2RepositoryLayoutFactory: Unsupported repository layout legacy

编译hbase 0.98的一个程序的,然后莫名奇妙的就开始报错。。。。 [INFO] Scanning for projects... [INFO] [INFO] -----------...
  • yunlong34574
  • yunlong34574
  • 2017年03月24日 14:36
  • 649


WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWit...
  • buptcxy
  • buptcxy
  • 2012年03月19日 14:02
  • 343

ValueError('You appear to be using a legacy multi-label data'

使用sklearn时,遇到该问题,可能是因为scikit-learn版本过旧或者太新 pip install -U scikit-learn==0.16.1 就能解决 当然在0.17之后,已经不允...
  • dongfangxiaozi_
  • dongfangxiaozi_
  • 2017年05月07日 12:20
  • 179

eclipse plug-ins导出

1,osg中的bundle在导出plug-ins时,出现这个错误,搞了一下午... 就是这个错误,打开logs.zip有一个log文件,打开有好多中文乱码问题. 2.原来打包的时候默认ecli...
  • hekaihaw
  • hekaihaw
  • 2016年04月27日 17:41
  • 646

classdump-dyld / iphoneheaders / iOS7.0.3 / System / Library / Internet Plug-Ins / QuickTime Plugin.
  • cyforce
  • cyforce
  • 2014年04月05日 00:16
  • 2042


工具:Firefox 迅雷  1.在浏览器中右键选择查看元素,转到控制台, 2.点击要下载的驱动,在控制台中会显示ftp信息,eg:
  • u012979478
  • u012979478
  • 2017年11月12日 09:50
  • 398

关于OSGI模块化编程中MENIFEST.MF文件中的Required Plug-ins和Imported Packages的区别

转载来自: 最近在做一个“把OSIG上的bundle结构的工程打成标准的...
  • zhx1019
  • zhx1019
  • 2015年01月14日 10:46
  • 875


Eclipse最有魅力的地方就是它的插件体系结构。在这个体系中重要的概念是扩展点(extension points),也就是为插件提供的接口。每一个插件都是在现有的扩展点上开发,并可能还留有自己的扩展...
  • romans1981
  • romans1981
  • 2005年02月28日 09:05
  • 1679

.net中using的几种用途 (平时不容易注意到)

  • tongdoudpj
  • tongdoudpj
  • 2007年06月01日 11:03
  • 907
您举报文章:Using legacy plug-ins with .NET