我试图使用DLL中的一个函数,但是我一直得到一个XamlParseException
,但是在一步一步运行我的程序时,我能够看到我得到了一个AccessViolationException
,我认为这是第一个异常的原因。
基本上,这个函数应该做的是让我登录到程序的一个目录,以便使用不同的类与程序进行交互。
我不能访问DLL的源代码,但我有一些文档可以使用它,尽管其中没有太多信息,下面是我应该如何使用它:
Logon(BSTR DirectoryPath, BSTR ErrorFile, int Lang)
下面是我的代码:
public partial class MainWindow : Window
{
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate int Logon([MarshalAs(UnmanagedType.BStr)]String DirectoryPath, [MarshalAs(UnmanagedType.BStr)]String ErrorFile, int Lang);
public MainWindow()
{
InitializeComponent();
try
{
string LnkPsbEppPath = "dll/LnkPsbEpp.dll";
IntPtr pAddressFctLogon = GetProcAddress(LnkPsbEppLink, "@Tdtm_Dossier@Logon$qv");
if (pAddressFctLogon == IntPtr.Zero)
Console.WriteLine("Logon function not found");
else
{
try
{
dynamic logon = Marshal.GetDelegateForFunctionPointer(pAddressFctLogon, typeof(Logon));
dynamic varLogon = logon(directoryPath, errorFilePath, 2); // <-- Here is where the AccessViolactionException happend
}
catch (Exception ex)
{
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR : " + ex.Message);
}
}
}
发布于 2014-02-17 19:06:52
检查该C++ dll是否为您提供了写入权限。
有关更多详细信息,请查看C++ dll文档
当我在我的C++应用程序中使用WPF dll时,我也遇到了同样的问题。
发布于 2014-02-17 21:46:37
如果在非托管代码中,有一些Null指针被访问,就可能发生这种情况。
https://stackoverflow.com/questions/21827292
复制相似问题