首先,我知道直接比较dllimport属性和getProcAddress函数是没有意义的。相反,我感兴趣的是比较两段代码,这两段代码通过导入带有dllimport属性的函数或使用getProcAddress函数来实现基本相同的功能-调用dll中的函数。具体地说,我正在编写一个C#应用程序,它使用我编写的动态链接库中的一些函数。首先,我使用以下代码访问我的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);
}
}这当然要整洁得多,正如前面提到的,它设置了lastError代码。显然,我的第一段代码为我提供了在运行时更改dll和函数调用的可能性,但目前这不是必需的。所以我的问题是:如果我确定我不会使用另一个dll或另一个函数,那么使用第一个公式有什么原因吗?
发布于 2010-07-14 07:32:39
使用GetProcAddress的唯一真正优势是,您可以手动卸载DLL以及调用函数,并且可以在运行时轻松更改命名。
然而,第二个选项为您提供了大量的好处。除了“更整洁”之外,它还为您处理大部分数据类型的封送处理-这在某些API中变得非常重要。
也就是说,如果您使用前面列出的方法,也应该确保卸载所有内容。现在,您基本上是在每次调用Bar()时泄漏地址……有关详细信息,请查看FreeLibrary。
发布于 2010-07-14 07:36:28
GetProcAddress的最大优点可能是它允许您控制DLL的搜索路径。例如,您可以自动加载32位或64位版本的本机DLL。使用DllImportAttribute,这是不可能的。
https://stackoverflow.com/questions/3242331
复制相似问题