My goal is to call methods that are implemented in Unity code from my UWP DLL . (Therefore, I can use them in my HoloLens project)
I tried this with a big project, but could not. Therefore, I wrote a simple example to facilitate the search for errors and exclude other influences. But still I get the same error.
My work environment:
- Windows 10 64-bit computer
- Micsrosoft Visual Studio 2015 Community Version 14.0.25431.01 Update 3
- HoloLens Emulator 10.0.14393.0
- Unity 5.5.0f3 Personal (64 bit)
Creating a UWP DLL:
To approach this, I created the C ++ DLL (Windows Universal) in Visual Studio 2015 as follows:
New Project> Visual C ++> Windows> Universal> DLL (Universal Windows)
After the project was created automatically, I added my code. So the code is as follows:
Native library code:
SimpleProjectDLL.cpp: #include "pch.h" #define DLL_EXPORT __declspec(dllexport) typedef void(*CB_V)(); typedef void(*CB_V_VI)(const char * a, int b); CB_V_VI cb_native_log; CB_V cb_call; void log() {
SimpleProjectDLL.h is preceded by delegates:
SimpleProjectDLL.h: #pragma once #include <cstdint> #define DLL_EXPORT __declspec(dllexport) extern "C" { typedef void(*CB_V)(); typedef void(*CB_V_VI)(const char * a, int b); }
I have not made any changes to the automatically generated dllmain.cpp, pch.cpp, pch.h or targetver.h files.
Finally, I create a project for Release mode and an x86 architecture to generate the DLL file. The location of the DLL file now: project-root folder /Release/SimpleProject/SimpleProjectDLL.dll .
---------------------
In the next step, I created a new Unity project , added by HoloLens-Toolkit, and made sure that the new project works fine on the emulator.
Unity project code:
After that, I added SimpleProjectDLL.dll to the properties folder and implemented the following code:
First of all, we need to create a link between the delegates. Cpp.cs precedes delegates:
Cpp.cs using UnityEngine; using System; using System.Runtime.InteropServices; namespace Cpp { delegate void DelegateV(); delegate void DelegateVVi(IntPtr a, int b); }
SimpleInterfaceCpp.cs initializes the connection:
SimpleInterfaceCpp.cs using Cpp; using System.Runtime.InteropServices; using UnityEngine; public static class SimpleInterfaceCpp { public static void Init() { initInterfaceCallbacks( SimpleInterface.NativeLog, SimpleInterface.Call ); } [DllImport(SimpleInterface.DLL)] private static extern void initInterfaceCallbacks( DelegateVVi native_log, DelegateV call ); }
Main:
MainController.cs using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class MainController : MonoBehaviour { void Start () { SimpleInterfaceCpp.Init(); SimpleInterface.TestCalls(); } }
SimpleInterface.cs calls the methods:
SimpleInterface.cs using System; using UnityEngine; using System.Runtime.InteropServices; using AOT; using IntPtr = System.IntPtr; using Cpp; using StringReturn = System.IntPtr; public class SimpleInterface { public const string DLL = "SimpleProjectDLL"; public static void TestCalls() {
Now, if I create an SLN, open the project in Visual Studio and run it using "HoloLens Emulator", I get the following output:
getSomeInt: 42 (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51) --- A callback--- (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51) call_log: native log (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51) The program '[1932] SimpleProject.exe' has exited with code -1073740791 (0xc0000409).
After that, the application simply closes.
So my Question is , does anyone know what might be the problem?
Is it correct to use callbacks in a HoloLens project?
Or does someone know how to find the error description for the code "-1073740791 (0xc0000409)"?
Additional information: I also tried this on a real HoloLens device, the same problem, so the problem does not lie on the emulator.