Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:

C++
NativeObject no = new NativeObject();


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

C++
#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



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
Posted
Updated 5-May-10 2:58am
v2

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.
 
Share this answer
 
Comments
smea 5-May-10 9:00am    
Surly if the signatures were wrong you would get compile errors.

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

Thanks
Michel Godfroid 5-May-10 9:17am    
All right, consider the following:
You have a native dll, which works fine when called from native code.
You add a CLI wrapper, to call it from managed code, and this causes errors.
The error must therefore lie in the marshalling/processing in your CLI code. In there, there is probably a parameter that you pass by reference and should be passed by value, or vice versa, or you don't have the necessary levels of indirection.
That's why I said that it is probably a case of wrong signatures, but you are the one generating those signatures.
Note that the problem is probably not in the NativeObject constructor, but in some data that was initialized previously in your CLI code.
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
 
Share this answer
 
:)
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900