【发布时间】:2014-08-28 13:18:03
【问题描述】:
我遇到了一个问题。我在 delphi 2009 中编写了 win32 DLL。现在我想从 C# 桌面应用程序动态加载该 DLL,但 LoadLibray 函数返回 0。这是 CSharp 代码,请任何人帮助我为什么 DLL 没有加载?
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad, IntPtr hFile, uint dwFlag);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("Kernel32.dll")]
private extern static Boolean CloseHandle(IntPtr handle);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IntPtr ptr = IntPtr.Zero;
ptr = LoadLibrary("MyDLL.dll", IntPtr.Zero, 0);
if (ptr == IntPtr.Zero)
{
MessageBox.Show("DLL not laded");
}
}
}
}
【问题讨论】:
-
确保您的 C# 应用程序在 x86 模式下运行。
-
请检查如何通过使用GetLastError 使这些函数报告错误。在这里,您可能只需将其添加到您的 if 语句中:
throw new Win32Exception();看看会发生什么。 -
你读过LoadLibrary的文档吗?它描述了如何处理错误。
-
@DavidHeffernan 难道不是因为 user2724058 正在调用 LoadLibrary() API,但使用了 LoadLibraryEx() 参数占用空间吗? (我从来没有做过这样的事情,所以我不确定。)
-
我多次将 VC++ DLL 加载到 .NET 中,并且一直有效。但我遇到了 delphi win32 DLL 的问题