Using legacy plug-ins with .NET

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

原文:http://www.thecodeproject.com/csharp/legacyplugins.asp

 

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:

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

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.

 

3ds Max插件开发(八)编写.Net Plug-ins

3ds Max 使用自己集成的.NET assembly 加载器在运行期间加载在3ds Max的安装目录\bin\assemblies 里全部有效并且实现了3ds Max plug-in的.Net a...

Using Legacy C APIs with Swift

Swift’s type system is designed to make our lives easier by enforcing strict rules around what we ca...

log4net 使用示例|using log4net with asp.net/winform

log4net, 是 apache.org 在 log4j的基础上推出的针对.NET程序的开源的日志组件。log4net目前的最新版本是 1.2.10,log4net支持的日志保存方式,可谓丰富之极,...

PDF插件VintaSoftPDF.NET Plug-in v5.5发布,新增PDF验证功能

VintaSoftPDF.NET Plug-in是一个完全的.NET组件,可用于查看,创建以及编辑PDF文档。该程序库还可用于显示,渲染,创建,转化,注释,打印,保存,修改和编辑PDF文档页面。 ...

.NET Reflector Ins CodeMetrics.zip

  • 2011年07月17日 00:06
  • 115KB
  • 下载

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

转载来自:http://blog.csdn.net/qimiguang/article/details/21371495 最近在做一个“把OSIG上的bundle结构的工程打成标准的...
  • zhx1019
  • zhx1019
  • 2015年01月14日 10:46
  • 817
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Using legacy plug-ins with .NET
举报原因:
原因补充:

(最多只允许输入30个字)