<分区>
Possible Duplicate:
Specify the search path for DllImport in .NET
我有一个非托管 DLL。为了清除,它是我想在我的 c# 代码中使用的 C++ dll。 问题是它是一个 Windows 应用程序,用户可以将它安装在他们选择的任何目录中。所以我不能用静态路径引用它,也找不到给出相对路径的方法。 这是我试过的代码:
[DllImport("mydll.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
public void SetDllDirectory(string lpPathName)
{
try
{
bool r = SetDllDirectory(lpPathName);
}
catch (Exception ex)
{
throw ex;
}
}
public void SetDirectoryPath()
{
try
{
DirectoryInfo directory = new DirectoryInfo(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath));
if (directory.Exists)
SetDllDirectory(directory.ToString() + "\\mydll.dll");
}
catch (Exception ex)
{
throw ex;
}
}
下面是我收到的错误。
Unable to find an entry point named 'SetDllDirectory' in DLL 'mydll.dll'.
这个问题可以复制
"Relative Path to DLL in Platform Invoke Statement "
很抱歉,但我没有在这些引用资料中找到任何解决方案。