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 } }