Calling .net 4.5 functions through a native wrapper

I’ve got a .net 4.5 class library which is being wrapped by a C++/CLR DLL which is then wrapped by a pure native C DLL. In my Unity app, I load the native C DLL, when I try to call a function which uses a .net 4.5 function, Unity crashes.

The C DLL (and it’s dependencies) are not placed in the Assets folder. Instead I use the method described here http://runningdimensions.com/blog/?p=5. This method calls LoadLibrary to load the DLL and then calls GetProcAddress to ultimately create a delegate to a function in the C DLL.

This method works with simple functions (i.e. simple ‘calculator’ functions), but the editor crashes when I try to invoke a more complicated functions, which I believe is due to those functions being dependent on .net 4.5.

The C DLL works, I’ve created a simple native C++ console app which is able to successfully call all functions.

Do I need to call LoadLibrary for the DLLs the C DLL are dependent on? Windows is the only platform I’m building for.

Any tips would be appreciated, thanks.

Here are some relevant lines from the log file:
0x00007FFE45FA1F28 (KERNELBASE) RaiseException
0x00007FFE211C1E89 (MSVCR120_CLR0400) _BuildCatchObjectHelper
0x00007FFE498690E3 (ntdll) RtlCaptureContext
0x00007FFE21279D49 (clr) LogHelp_LogAssert
0x00007FFE213E68B2 (clr) StrongNameSignatureVerification
0x00007FFE2127FE9B (clr) LogHelp_LogAssert
ERROR: SymGetSymFromAddr64, GetLastError: ‘Attempt to access invalid address.’ (Address: 00007FFE21262C55)
0x00007FFE21262C55 (clr)
ERROR: SymGetSymFromAddr64, GetLastError: ‘The specified module could not be found.’ (Address: 00007FFDC1C02A1B)
ERROR: SymGetModuleInfo64, GetLastError: ‘A dynamic link library (DLL) initialization routine failed.’ (Address: 00007FFDC1C02A1B)
0x00007FFDC1C02A1B (())
0x00007FFE212D7D1E (clr) StrongNameTokenFromPublicKey
0x00007FFE3167167F (pfw-c-dll)

:eyes:

Is there a reason you need to do this?

Like… there are SO MANY places this could break. You have .Net code being, called by C++ code, called by C code, called by mono code, ran in unity.

… it sounds weird just writing that out.

I don’t even know what .net 4.5 code you’re attempting to run, let alone how you start up the .net CLR to run it (I hope you’re not trying to run it in the mono clr with unity, because it won’t work that way), I don’t know what your C++ code does, nor your C code. All I know is that you’re calling it from unity.

All I can discern from is the log you posted… which basically just says you accessed invalid memory. I couldn’t imagine how that happened… it’s not like you’ve called jit code from native code from native code from jit code from native co… oh…

So before even trying to solve this enigma wrapped in a puzzle served over a plate of wtf… how about we figure out what you’re attempting to do, and maybe come up with a better solution that isn’t so complicated to debug.

1 Like

lol.

Okay, so as you probably figured there’s a bunch of different libraries being used here. This is being done for a project I’m working on which involves 3D reconstruction using cameras, such as Kinect(s).

I do the most of the work in native C++, where I use libraries such as OpenCV and Cuda (among others) to do various image processing stuff.

The C DLL (exported as C to avoid name mangling) I made has functions which will deal with interfacing with the various libraries. The idea was that by having a single DLL which just took care of linking and wrapping everything, implementation on any engine would be simplified.

All was fine and dandy until I needed to use a C# library to calculate some camera calibration stuff for me. All I wanted to do was add some functions to my C DLL which simply wrapped up this library as I have done before.

I found a bunch of different ways to achieve C++ / C# interop but decided on using an intermediate CLR wrapper (Using C# from native C++ with the help of C++/CLI (fixed and enhanced) | Pragmateek), it seemed like a good choice (at the time). I mean it works in C++ but I don’t know enough about the internals of Mono / C# to know what I’m missing or why what I’m trying to do would make a Mono / C# expert cry.

In my native C++ application all I need to do is call CoInitialize() prior to calling any functions in the C# library and everything works fine.

I figured all I would need to do in a Unity C# script was call CoInitialize() and everything would be good to go. Unfortunately that is not the case at all.

I created a simple C# console application which uses .net 2.0. I was able to successfully load and invoke all of the functions in my DLL, even the ones which cause Unity to crash.

Is there a good resource which explains Unity’s Mono/C# setup in detail?