通过c ++/cli层从c ++ dll使用.net dll时出现问题 [英] Problem in using a .net dll from c++ dll via c++/cli layer

查看:111
本文介绍了通过c ++/cli层从c ++ dll使用.net dll时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c ++ dll,在某个时候它需要将任务委派给用.Net编写的dll.为此,我创建了一个c ++/cli静态库,该库使用/使用.net dll来完成一些任务.然后,我将c ++/cli静态库与c ++ dll静态链接.看起来像这样.

I have a c++ dll, which at a certain point, needs to delegate a task to a dll written in .Net. For this i Created a c++/cli static library which consumes/uses the .net dll to achive some TASK. Then i statically link the c++/cli static lib with c++ dll. It looks something like this.

        cli.aTask()         netdll.aTask()
c++ dll ----------> c++/cli ----------------> .net dll
    < static link  >    <normal assembly inclusion> 

现在,b/w c ++ dll和c ++/cli的连接很好.我可以在c ++/cli层中使用.net库执行任何操作,然后从c ++ dll调用它.但是,当我从.net dll创建类的对象,或访问.net dll类的静态方法时,我在c ++ dll中得到异常(实际上c ++ dll崩溃,没有异常).下面是我得到的味精.

Now, the connection b/w c++dll and c++/cli is fine. i can do anything using .net library in c++/cli layer and then call this from c++dll. but when i create an object of a class from .net dll, or access a static method of .net dll class, i get exception in c++dll (actually c++dll crashes, no exceptions). below is the msg i got.

foo.exe is using c++dll
Unhandled exception at 0x7c812aeb in "foo.exe": OxE0434f4d: 0xe0434f4d.

c ++ dll代码调用cli方法:

CLILibrary::CCLILib lib;
lib.GetUsersInput();

cli代码调用.net dll:

DotNetLibrary::DotNetClass::aMethod();   // static method`

.net dll类代码:

namespace DotNetLibrary
{
    public class DotNetClass
    {
        public DotNetClass(){}

       public static void aMethod(){}
    }
}


现在,如果我不以任何方式在c ++/cli lib中使用DotNetLibrary::DotNetClass,那么异常就会停止.


Now, if i dont use DotNetLibrary::DotNetClass in any way in c++/cli lib, then exception stops coming.

请帮助.我的目标是从c ++ dll调用.net lib,如果有更好的方法,那也很好.预先感谢.

Please help. my aim is to call .net lib from c++ dll, if there is a better way, then it will be nice too. thanks in advance.

推荐答案

如上所述,您需要在.Net或C ++/CLI模块中捕获(托管)异常. 我通常在C ++/CLI .Net包装器中执行的操作是捕获托管异常,然后将它们作为标准c ++异常重新抛出. 像

As already mentioned you need to catch the (managed) exception either in your .Net or in your C++/CLI module. What I usualy do in my C++/CLI .Net wrappers is to catch the managed exceptions and re-throw them as standard c++ ones. Something like

try {
   // ... managed call
}
catch (System::Exception^ e)
{
    throw std::exception(ConvertString(e->Message));
}

(其中ConvertString是将托管字符串转换为char *或std :: string的函数). 这样,您仍然必须在本机代码中捕获异常,但不会丢失有关源错误的信息. (不是很完全,因为生成异常和其他信息的原始代码中的调用栈将丢失,但是如果您愿意的话,也可以保存它.)

(where ConvertString is a function that converts a managed string to a char* or std::string). In this way you'll still have to catch the exception in your native code but you will not lose information about the source error (well not quite since the callstack from the original code that generated the exception and other information will be lost but you could save this too if you wanted to).

这篇关于通过c ++/cli层从c ++ dll使用.net dll时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆