using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace POSV.Utils { public class SharedLibrary { [System.Runtime.InteropServices.DllImport("kernel32" , SetLastError = true , CharSet = System.Runtime.InteropServices.CharSet.Unicode)] protected static extern IntPtr LoadLibrary(string lpFileName); [System.Runtime.InteropServices.DllImport("kernel32" , CharSet = System.Runtime.InteropServices.CharSet.Ansi , ExactSpelling = true , SetLastError = true)] protected static extern IntPtr GetProcAddress(IntPtr hModule , string procName); [System.Runtime.InteropServices.DllImport("kernel32" , SetLastError = true)] [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] protected static extern bool FreeLibrary(IntPtr hModule); private IntPtr _IntPtr; public SharedLibrary(String path) { _IntPtr = LoadLibrary(path); } ~SharedLibrary() { FreeLibrary(_IntPtr); } //将要执行的函数转换为委托 public Delegate Invoke(string procName , Type t) { IntPtr ptr = GetProcAddress(_IntPtr , procName); return (Delegate)Marshal.GetDelegateForFunctionPointer(ptr , t); } } }