从C ++/CLI内部创建本机C ++对象时出现问题 [英] Problem creating Native C++ object from within C++/CLI

查看:76
本文介绍了从C ++/CLI内部创建本机C ++对象时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这使我无所适从,所以我希望有人可以帮助您:-D

我有一个在Visual Studio 2008中创建的现有本机C ++ DLL.

我想使用C ++/CLI项目(包装DLL)中提供的某些类及其提供的功能,这些项目又可以从其他.NET应用程序中使用,因此实际上,我将拥有一个.NET应用程序引用一个C ++/CLI dll,而后者又引用一个本机C ++ dll

我可以在单个VS2008解决方案文件中创建每个组件作为单独的保护,并且所有内容都可以正常构建.

.NET应用程序可以正常运行,并且可以实例化我没有错误的C ++/CLI对象.当我在C ++/CLI对象上调用一个方法又实例化一个本机类时,就会出现问题.我可以将代码调试到以下位置:

This is driving me up the wall so I hope someone can help :-D

I have an existing Native C++ DLL created in visual studio 2008.

I want to use some of the class''s and the functionality that they provide from within a C++/CLI project (wrapper DLL) which in turn can be used from other .NET applications so in affect I will have a .NET application which references a C++/CLI dll which in turn references a native C++ dll

I can create each of the components as separate protects within a single VS2008 solution file and everything builds without error.

The .NET application runs without error and is able to instantiate the C++/CLI object that I have without error. The problem occurs when I call a method on the C++/CLI object which in turn instantiates a native class. I can debug the code right to the point where:

NativeObject no = new NativeObject();



调用,此时我在.dll中出现了 System.Runtime.InteropServices.SEHException" '':confused :: confused:

本机代码可以在其他本机项目中正常使用,这些问题仅在此.NET环境中才会发生.

作为参考,我使用.h文件顶部的代码从本机DLL中导出类定义.



is called at which point I get a ''System.Runtime.InteropServices.SEHException'' occurred in .dll'' :confused::confused:

The native code works fine used within other native projects the problems only occur in this .NET environment.

For reference I''m exporting class definitions from the native DLL using this at the top of the .h file

#ifdef EXPORT_IMS_CORE_CLASS
    #define IMS_CORE_CLASS_API __declspec( dllexport )
#else
    #define IMS_CORE_CLASS_API __declspec( dllimport )
    #pragma message( "Automatic link to IMSCore.lib (IMS::DataTypes::DataObject)" )
    #pragma comment( lib, "IMSCore.lib" )
#endif




我可以提供非常精简的解决方案版本,如果任何人都可以看一下,就会发生错误

在此先感谢您的帮助,Simon




I can supply a very cut down version of the solution where the error occurs if anyone can have a look

Thanks in advance for any help, Simon

推荐答案

很有可能您没有使用正确的签名来称呼它. SEHException是所有在非托管代码中发生且无法由框架映射的内容的全部..您应在非托管类的函数入口处放置一个断点,并仔细检查所有输入参数并检查它们是否具有正确的值.我的赌注是基于引用传递的值,并按值解释,反之亦然.
There is a strong chance that you are not calling this with the right signatures. The SEHException is a catch-all for anything that occurs in unmanaged code and cannot be mapped by the framework.. You should put a breakpoint at function entry in your unmanaged class, and carefully examine any input parameters and check if they have the right values. My bets are on an value passed by reference and interpreted as by value or vice versa.


您是否尝试查看异常是什么?您可以将本机对象的实例放置在try块中,然后捕获SEHException并查看错误的确切原因.
出于多种原因,可能会发生SEHException.细微的原因可能是找不到natice DLL.特别是对于Asp.Net应用程序,DLL必须位于bin文件夹中.

-Saurabh
Did you tried to see what the exception is? You can place the instantiation of native object in a try block then catch the SEHException and see what exactly is the error.

SEHException can happen for variety of reasons. Subtle reason might be that natice DLL cannot be found. Especially for Asp.Net applications the DLL must be in bin folder.

-Saurabh


:)
我发现了使用.NET应用程序引用C ++/CLI dll并进而引用本机C ++ dll的问题.

发生问题是因为本机dll无法像.NET组件那样构建到BIN文件夹中,因此在运行程序时,它无法找到本机dll来创建对象.
我发现它是纯粹的fl幸,因为错误中没有信息指出找不到dll.

反正.既然我已经为.NET应用程序工作了,那么任何人都知道在使用.NET Web应用程序时如何解决相同的问题.

我已经尝试了杰里·奥曼(Jerry Orman)的这篇很好的帖子中的以下内容,但我仍然遇到问题
http://blogs.msdn.com/jorman/archive/2007/08/31/loading-c-assemblies-in-asp-net.aspx [
:)
I have a found the issue where using a .NET application which references a C++/CLI dll which in turn references a native C++ dll.

The problem was occurring because the native dll does not get built to the BIN folder as with .NET components so when the program was run it was unable to locate the native dll to create the object.
It as a pure fluke that I found this because there is no info in the error stating that the dll could not be found.

Anyway. Now that I have this working for .NET applications does any one know how to resolve the same problem when using a .NET web application.

I have tried the following from this very good post by Jerry Orman but I''m still having the problem
http://blogs.msdn.com/jorman/archive/2007/08/31/loading-c-assemblies-in-asp-net.aspx[^]

Thanks again, Simon


这篇关于从C ++/CLI内部创建本机C ++对象时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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