调用C ++ / CLI包装器时发生外部异常E0434352

问题描述 投票:-2回答:1

以下解决方案创建了库'YahooAPI.dll'和'YahooAPIWrapper.dll'。(在此处找到原始项目:http://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/

从第三方应用程序中,我将其称为“ YahooAPIWrapper.dll”,结果如下:

  • 函数'Foo'返回期望的数据而不会出现问题(它不会调用YahooAPI.dll)。
  • 函数'GetRevision'通过函数'GetRevisionUtil',两次调用'YahooAPI.dll';每次调用均显示msgbox,且无错误。但是,在显示第二个msgbox之后,将显示一个弹出窗口,显示:外部异常E0434352

为了进行故障排除,我添加了c ++控制台应用程序。

请参见下面的代码中的注释。

YahooAPI.dll:

// YahooAPI.cs

using System;
using System.Windows.Forms;

public class YahooAPI
{
    public string GetRevisionUtil(string rev)
    {
        MessageBox.Show("CurrentRev: " + rev);
        return "rev_" + rev ;
    }
}

YahooAPIWrapper.dll标头:

// YahooAPIWrapper.h

#pragma once

#define DLL_EXP extern "C" __declspec(dllexport)
DLL_EXP void GetRevision(char* data_in, char *data_out);
DLL_EXP void Foo(char* data_in, char *data_out);

class YahooAPIWrapperPrivate;
class __declspec(dllexport) YahooAPIWrapper
{
private:  YahooAPIWrapperPrivate* _private;

public:
    YahooAPIWrapper();
    ~YahooAPIWrapper();

    const char* GetRevisionFunc(const char* rev);
};

请参阅函数中的注释:GetRevision

// YahooAPIWrapper.cpp

#include "stdafx.h"
#include <msclr\auto_gcroot.h>
#include <string>
#using "YahooAPI.dll"

#include <msclr\auto_gcroot.h>
#include "YahooAPIWrapper.h"

using namespace System::Runtime::InteropServices; // Marshal

DLL_EXP void GetRevision(char* data_in, char *data_out)
{
    YahooAPIWrapper wrp;
    const char* c = wrp.GetRevisionFunc(data_in);  // 1st messagebox shows
    const char* d = wrp.GetRevisionFunc(c);   // 2nd messagebox shows

    strcpy_s(data_out,100, d);  // << HERE <<  3rd party app throws 'external exception E0434352 '
                          // the console app throws the error 'Unhandled exception...'
}

const char* YahooAPIWrapper::GetRevisionFunc(const char* rev)
{
    System::String^ managedCapi = _private->yahooAPI->GetRevisionUtil(gcnew System::String(rev));
    return (const char*)Marshal::StringToHGlobalAnsi(managedCapi).ToPointer();
}

DLL_EXP void Foo(char* data_in, char *data_out)
{
    int a_size = int(strlen(data_in)); 
    std::string s_a = convertToString(data_in, a_size);
    strcpy_s(data_out,100, s_a.c_str());
}

std::string convertToString(char* a, int size)
{
    int i;
    std::string s = "";
    for (i = 0; i < size; i++) {
        s = s + a[i];
    }
    return s;
}

class YahooAPIWrapperPrivate
{
    public: msclr::auto_gcroot<YahooAPI^> yahooAPI;
};

YahooAPIWrapper::YahooAPIWrapper()
{
    _private = new YahooAPIWrapperPrivate();
    _private->yahooAPI = gcnew YahooAPI();
}

YahooAPIWrapper::~YahooAPIWrapper()
{
    delete _private;
}

控制台测试应用程序:

// Test.cpp 

#include <iostream>
#include <stdio.h>

#include "YahooAPIWrapper.h"

int main()
{
    YahooAPIWrapper yahoo;

    char* a = "aaaa";
    char* b = "bbbb";

    const char* c = yahoo.GetRevisionFunc(b); // messagebox shows
    std::cout << c << std::endl;

    GetRevision(a, b); // 1st & 2nd msgbox shows - then error: "unhandled exception..."
    std::cout << a << std::endl;
    std::cout << b << std::endl;

    return 0;
}

编辑:“控制台应用程序”错误已解决。(以上错误的全文:Test.exe中0x0f3c2fdd的未处理异常:0xC0000005:访问冲突写入位置0x00da7830。)


编辑:上面的项目已被缩减为以下内容(请参见评论)-但仍然看到“外部异常E0434352”:

namespace Publics {
    public class YahooAPI{
        public static void GetRevisionUtil() {
            string rev = "test";
            MessageBox.Show("CurrentRev: " + rev);
            //return "rev_" + rev;
        }
    }
}
// YahooAPIWrapper.h
#pragma once
#define DLL_EXP extern "C" __declspec(dllexport)
DLL_EXP void GetRevision(char* data_in, char *data_out);
DLL_EXP void Foo(char* data_in, char *data_out);
#include "stdafx.h"
#include <windows.h>
#include <msclr\auto_gcroot.h>
#include <string>

#using YahooAPI.dll"
#include "YahooAPIWrapper.h"

using namespace System::Runtime::InteropServices; // Marshal

DLL_EXP void GetRevision(char* data_in, char *data_out) 
// this function builds & works fine with console 
// however, immediately throws error (External Exception E0434352) when called by 3rd party app 
// - does not even make it to MessageBox.
{
    MessageBox(NULL, TEXT("msg1"), TEXT("Test"), MB_OK);

    Publics::YahooAPI::GetRevisionUtil();

    // Publics::YahooAPI^ obj = gcnew Publics::YahooAPI;
    // obj->GetRevisionUtil();

    strcpy(data_out,  data_in);
}

编辑:通过使用WinDbg,我为解决此错误所做的不懈努力-每个:https://stackoverflow.com/a/6245146/3818364

在这里找到了实际使用WinDbg的帮助:https://netmatze.wordpress.com/2012/08/24/using-windbg-exe-and-sos-dll-to-debug-a-net-4-0-application/

下面是WinDbg命令窗口中的代码段。请注意以下命令的使用:。load C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ sos.dll!线程

ntdll!DbgBreakPoint:
772c2790 cc              int     3
0:014> .load C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll
0:014> g
(9b3c.a350): C++ EH exception - code e06d7363 (first chance)
(9b3c.a350): C++ EH exception - code e06d7363 (first chance)
(9b3c.a350): C++ EH exception - code e06d7363 (first chance)
(9b3c.a350): CLR exception - code e0434352 (first chance)
(9b3c.a350): C++ EH exception - code e06d7363 (first chance)
(9b3c.87d8): Break instruction exception - code 80000003 (first chance)
eax=00353000 ebx=00000000 ecx=772fb3b0 edx=772fb3b0 esi=772fb3b0 edi=772fb3b0
eip=772c2790 esp=0e9eff44 ebp=0e9eff70 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
ntdll!DbgBreakPoint:
772c2790 cc              int     3
0:014> !threads
ThreadCount:      2
UnstartedThread:  0
BackgroundThread: 2
PendingThread:    0
DeadThread:       0
Hosted Runtime:   no
                                                                         Lock  
       ID OSID ThreadOBJ    State GC Mode     GC Alloc Context  Domain   Count Apt Exception
   0    1 a350 0299d1b8     20220 Preemptive  0BFC41D4:00000000 04f22fd8 0     STA System.IO.FileNotFoundException 0bfc2768
  13    2 9b08 04f369c8     21220 Preemptive  00000000:00000000 04f22fd8 0     Ukn (Finalizer) 
0:014> !PrintException /d 0bfc2768
Exception object: 0bfc2768
Exception type:   System.IO.FileNotFoundException
Message:          Could not load file or assembly 'YahooAPI.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
InnerException:   <none>

您可以在上面的窗口中看到,我能够将原始错误'External Exception E0434352'分解为'C ++ EH exception-code e06d7363'。最终能够解决此错误,使之变得平凡:“ System.IO.FileNotFoundException”。我的YahooAPIWrapper.dll找不到我的(托管)YahooAPI.dll,即使它们都位于同一文件夹中。

底线:我的第三方应用程序具有配置功能,供用户浏览并选择其Win32 dll的文件夹。

我的错误是假定托管DLL(YahooAPI.dll)的正确位置与非托管(YahooAPIWrapper.dll)位于同一文件夹中。

通过简单地将我的托管DLL(YahooAPI.dll)复制到第三方.Exe所在的文件夹,即可解决该错误。中提琴!

c# c++ c++-cli windbg
1个回答
0
投票
使用WinDbg,我可以将通用的'External Exception E0434352'错误更进一步地解决:“ System.IO.FileNotFoundException”。请查看我对链接的最后修改。
© www.soinside.com 2019 - 2024. All rights reserved.