BY
Harkos
Original: 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 THEN 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 used to do sale delphi. By Tired of Trying (Even Unsafe) and posted The Question on a MS NewsGroup, 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 folowing code:
[DLLIMPORT ("Plugin.dll")]]]
Private Extern Static Int SomeMethod (Int A, INT B);
THIS WRITTEN IMAGINE ANY DLL WRITTEN IN ANY NON-.NET LANGUAGE. The Problem with this is this............... ..
USING THE CODE
We want to access multiple legacy DLLs dynamically, and we can not 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 is not 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 not 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 Must Have A Correspondent In this Front-End in Order To BE.
Now That 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 = loadingLibrary (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 CHENGED THE Name of the Method Because of the public version
// DLL Name Search Is Case-Innsitive 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.