Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/CLI wrapper for native C/C++ code, unable to load in Unity3D

I've been looking and looking all over the net about how to load an assembly in Unity3D, and I'm having a real hard time. I'm sorry if this post will be long, but I'll be putting everything I've learned and how far I've gotten since this is my first time working in .net and dlls.

I have a native dll, it has a whole bunch of extern "C" so I can load everything at runtime. This works in unity if I use the [DLLImport] attribute and such. However, it's cumbersome, and not really reusable code. It would become even more cumbersome later when I will have to abstract my system between more than 1 native library.

So I figured I'd do a C+++/CLI wrapper and then load it in Unity3d like any other dll and just link the namespace: "using MyWrapper;"

I created the simplest C++/CLI lib I could think of. all my lib does is have a class (Class1) and it has a function int getnum() {return 5;}. I'm using VC++ 2010 express, and I'm building with V90, and modified the vcxproj file to target 2.0. I know unity only supports 2.0. I'm building in /clr in order to be able to have native and .net code.

It completely crashes unity3d. This is my Error log at GameManager.Awake () [0x0001d] in Manager\GameManager.cs:116 at GameManager.Awake () [0x00000] in Manager\GameManager.cs:107 at (wrapper runtime-invoke) GUIRadioButton.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff> Receiving unhandled NULL exception

if I build it in /clr:safe instead it works fine. /clr:pure also doesn't work.

So I decided to do an external c# command line project to test everything. I load my lib, and compile in 2.0 and /clr (mixed mode) works without a problem.

I'm using Unity 2.6 Pro.

It's possibly what I'm looking to do is impossible, I don't know.. I mean I figured that was what .NET was all about. I just want to have a system which can be reused in other projects (C++).

Thanks for any insight.

Here's my test c++/cli project.

#pragma once

using namespace System;

namespace CLRTest {

    public ref class Class1
    {
        // TODO: Add your methods for this class here.
    public:

        Class1(){}

        int getnum (){return 5;}
    };
}

When I create a C# command line project like this. it works.

using System;
using System.Collections.Generic;
using System.Text;
using CLRTest;

namespace CLRTestLoad
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1();

            Console.WriteLine ("num is = " + c.getnum());
        }
    }
}

Executing this line in Unity3d crashes the whole editor. (Yes I did put in the using statement)

Class1 c = new Class1();
like image 726
Edgar Parente Avatar asked Mar 15 '11 14:03

Edgar Parente


People also ask

What is CLI wrapper?

Zigbee CLI wrapper (zb_cli_wrapper) is a Python package for the nRF5 SDK for Zigbee that includes a wrapper for automating communication with the Zigbee CLI Agent example and improving the control of the Zigbee network. It is a standalone package and can be used on both Windows and Linux machines.

Can you use C with Unity?

Unity supports the C# programming language natively. C# (pronounced C-sharp) is an industry-standard language similar to Java or C++. In addition to this, many other . NET languages can be used with Unity if they can compile a compatible DLL - see here for further details.

How to create C# wrapper for C++ DLL?

Solution 1 You can do it by using C++/CLI and mixed-mode (managed+unmanaged) project. You can really use "regular" C++ classes with C++/CLI "ref" classes. You can really wrap a "regular" C++ class or struct around some "ref" types used in the implementation of the wrapper class. Everything else depends on your problem.

What is C++ CLI wrapper?

C++/CLI Wrapper As previously mentioned, this is the link between the C# class and the main C++ application. This project was created using the empty CLR Class project in Visual Studio. Once the project is created, a header and source file were added. A reference to C# class library dll was also added to the project.


1 Answers

AFAIK, C++/CLI assemblies are not working with Unity, as Unity is relying on Mono that doesn't support mixed assemblies. The only workaround is to call functions from a dll with classic p/invoke

like image 126
Alexandre Mutel Avatar answered Oct 19 '22 08:10

Alexandre Mutel