C# 呼叫C++;dll尝试读取或写入受保护内存

C# 呼叫C++;dll尝试读取或写入受保护内存,c#,c++,exception,visual-studio-2012,dll,C#,C++,Exception,Visual Studio 2012,Dll,我试图使用函数drom a DLL,但我不断得到一个XamlParseException,但我能够在一步一步运行我的程序时看到我得到一个AccessViolationException,我认为这是第一个异常的原因。 基本上,该函数要做的是将我记录到程序的目录中,以使用不同的类与程序交互。 我没有访问DLL的源代码的权限,但我有一些文档可以使用它,即使其中没有太多信息,以下是我应该如何使用它: Logon(BSTR DirectoryPath, BSTR ErrorFile, int La

我试图使用函数drom a 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);
        }

    }
}
公共部分类主窗口:窗口
{
[DllImport(“kernel32.dll”)]
静态外部IntPtr加载库(字符串dllToLoad);
[DllImport(“kernel32.dll”,CharSet=CharSet.Ansi,ExactSpelling=true,SetLastError=true)]
公共静态外部IntPtr GetProcAddress(IntPtr hModule,string procedureName);
[非托管函数指针(CallingConvention.Winapi)]
私有委托内部登录([Marshallas(UnmanagedType.BStr)]字符串目录路径[Marshallas(UnmanagedType.BStr)]字符串错误文件,int-Lang);
公共主窗口()
{
初始化组件();
尝试
{
字符串lnkpsbeppath=“dll/LnkPsbEpp.dll”;
IntPtr pAddressFctLogon=GetProcAddress(LNKPSbeplink,“@Tdtm_Dossier@Logon$qv”);
if(pAddressFctLogon==IntPtr.Zero)
Console.WriteLine(“未找到登录功能”);
其他的
{   
尝试
{
动态登录=Marshal.GetDelegateForFunctionPointer(pAddressFctLogon,typeof(logon));

动态ValLogon=登录(DealTryPATH,Error FrimePATH,2);//< P>检查C++ DLL是否提供了允许写入。

检查C++ + DLL文档以了解更多细节< /P>


即使我在WPF应用程序

< P>中使用C++ DLL时也会遇到同样的问题,如果在非托管代码中,有空指针被访问。

顺便说一下,在这个DLL附带的文档中,如果您使用的是代码>登录< <代码>,它将自动尝试连接到SQL Server,如果不是的话。完成了。这可能是
AccessViolationException
的原因吗?你成功了吗?你介意与全世界(或至少是我)分享你的知识吗?事实上,我和你有着完全相同的问题。我基本上正试图实现完全相同的目标……专家/m真是一件痛苦的事