首先,我知道直接比较dllimport属性和getProcAddress函数是没有意义的。相反,我感兴趣的是比较两段代码,它们实现了基本相同的功能—在dll中调用函数—通过使用dllimport属性或getProcAddress函数导入函数。具体来说,我正在编写一个C应用程序,它在我编写的dll中使用一些函数。首先,我使用以下代码访问了我的dll函数:
class DllAccess
{
[DllImport("kernel32.dll", SetLastError = true)]
private extern IntPtr LoadLibrary(String DllName);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate Bool BarType(Byte arg); // return value indicates whether function call went well or not.
Bool Bar(Byte arg)
{
Bool ok = false;
IntPtr pDll= LoadLibrary("foo.dll");
if (pDll != IntPtr.Zero)
{
IntPtr pfunc = GetProcAddress(pDll, "bar");
if (pFunc != IntPtr.Zero)
{
BarType bar = (BarType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(BarType));
ok = bar(arg);
}
FreeLibrary(pDll);
}
return ok;
}
}
但是,如果lastError值是在dll调用期间设置的,我稍后需要获取它,因此我将代码更改为:
class DllAccess
{
[DllImport("foo.dll", EntryPoint = "bar", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private extern Bool DllBar(Byte arg); // return value indicates whether function call went well or not.
Bool Bar(Byte arg)
{
return DllBar(arg);
}
}
这当然要整洁得多,正如前面提到的,它设置了最后一个错误代码。显然,我的第一段代码提供了在运行时更改dll和函数调用的可能性,但目前这不是必需的。所以我的问题是:如果我确信我不会使用另一个dll或另一个函数,那么使用第一个公式有什么理由吗?