DllImportを使わずにアンマネージドDLL関数を呼び出してみる
まったく使わないわけではないけど。参考にしたのはこのあたり。
- http://dobon.net/vb/dotnet/links/extractarchive.html
- http://momotchi.net/forums/711/ShowPost.aspx
- http://msdn2.microsoft.com/ja-jp/library/system.runtime.interopservices.marshal.getdelegateforfunctionpointer.aspx
同じようなインターフェイスを持つ、複数のアンマネージドDLL関数を呼び出す必要があったのでいろいろ調べていたら、 .NET 2.0 から用意された System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer を使えば関数ポインタをデリゲートに変換できることがわかった。
やり方としてはこんな感じ。
- アンマネージドDLLの関数定義を調べる。
- 関数定義にあわせてデリゲートを定義する。
- LoadLibrary でアンマネージドDLLをロードする。
- GetProcAddress で関数ポインタを取得する。
- GetDelegateForFunctionPointer で関数ポインタをデリゲートに変換する。
- デリゲートをコールする。
- FreeLibrary でアンマネージドDLLを解放する。
で、毎回同じようなコードになる部分をクラスにしてみたのでメモ。
サンプルでは MessageBoxW を呼び出してるけど、統合アーカイバとかにも応用できるはず。
using System; using System.Runtime.InteropServices; namespace LateBindingApplication { static class Program { /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() { using (LateBinding b = new LateBinding("user32.dll")) { MessageBox m = (MessageBox)b.GetDelegate("MessageBoxW", typeof(MessageBox)); m(IntPtr.Zero, "call by c#", "LateBindingApplication", 0); } } } public delegate int MessageBox(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]string text, [MarshalAs(UnmanagedType.LPWStr)]string Caption, int type); public class LateBinding : IDisposable { [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPWStr)] string lpFileName); [DllImport("kernel32", SetLastError = true)] private static extern bool FreeLibrary(IntPtr hModule); [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = false)] private static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName); private IntPtr _module; /// <summary> /// DLL名を指定して遅延バインディングを行うオブジェクトを生成します。 /// </summary> /// <param name="filename">バインドするDLL名</param> public LateBinding(string filename) { _module = LateBinding.LoadLibrary(filename); if (_module != IntPtr.Zero) { return; } int result = Marshal.GetHRForLastWin32Error(); throw Marshal.GetExceptionForHR(result); } /// <summary> /// 指定した名前を持つアンマネージ関数ポインタをデリゲートに変換します。 /// </summary> /// <param name="procName">アンマネージ関数名</param> /// <param name="delegateType">変換するデリゲートのType</param> /// <returns>変換したデリゲート</returns> public Delegate GetDelegate(string procName, Type delegateType) { IntPtr ptr = LateBinding.GetProcAddress(_module, procName); if (ptr != IntPtr.Zero) { Delegate d = Marshal.GetDelegateForFunctionPointer(ptr, delegateType); return d; } int result = Marshal.GetHRForLastWin32Error(); throw Marshal.GetExceptionForHR(result); } #region IDisposable メンバ public void Dispose() { if (_module != IntPtr.Zero) { LateBinding.FreeLibrary(_module); } } #endregion } }