动态加载DLL需要使用Windows API函数:LoadLibrary、GetProcAddress以及FreeLibrary。我们可以使用DllImport在C#中使用这三个函数。
  [DllImport("Kernel32")]
  public static extern int GetProcAddress(int handle, String funcname);
  [DllImport("Kernel32")]
  public static extern int LoadLibrary(String funcname);
  [DllImport("Kernel32")]
  public static extern int FreeLibrary(int handle);
  当我们在C++中动态调用Dll中的函数时,我们一般的方法是:
  假设DLL中有一个导出函数,函数原型如下:
  BOOL __stdcall foo(Object &object, LPVOID lpReserved);
  1、首先定义相应的函数指针
  typedef BOOL (__stdcall *PFOO)(Object &object, LPVOID lpReserved);
  2、调用LoadLibrary加载dll:
  HINSTANCE hInst = ::LoadLibraryW(dllFileName);
  3、调用GetProcAddress函数获取要调用函数的地址:
  PFOO foo = (PFOO)GetProcAddress(hInst,"foo");
  if(foo == NULL)
  {
  FreeLibrary(hInst);
  return false;
  }
  4、调用foo函数:
  BOOL bRet = foo(object,(LPVOID)NULL);
  5、使用完后应释放DLL:
  FreeLibrary(hInst);
  那么在C#中应该怎么做呢?方法基本上一样,我们使用委托来代替C++的函数指针,通过.NET Framework 2.0新增的函数GetDelegateForFunctionPointer来得到一个委托的实例:
  下面封装了一个类,通过该类我们可以在C#中动态调用Dll中的函数了:
public class DLLWrapper
{
///<summary>
/// API LoadLibrary
///</summary>
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname);
///<summary>
/// API GetProcAddress
///</summary>
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, String funcname);
///<summary>
/// API FreeLibrary
///</summary>
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
///<summary>
///通过非托管函数名转换为对应的委托, by jingzhongrong
///</summary>
///<param name="dllModule">通过LoadLibrary获得的DLL句柄</param>
///<param name="functionName">非托管函数名</param>
///<param name="t">对应的委托类型</param>
///<returns>委托实例,可强制转换为适当的委托类型</returns>
public static Delegate GetFunctionAddress(int dllModule, string functionName, Type t)
{
int address = GetProcAddress(dllModule, functionName);
if (address == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
}
///<summary>
///将表示函数地址的IntPtr实例转换成对应的委托, by jingzhongrong
///</summary>
public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t)
{
if (address == IntPtr.Zero)
return null;
else
return Marshal.GetDelegateForFunctionPointer(address, t);
}
///<summary>
///将表示函数地址的int转换成对应的委托,by jingzhongrong
///</summary>
public static Delegate GetDelegateFromIntPtr(int address, Type t)
{
if (address == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
}
}