HoloLens callbacks with native library

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() { // this method makes problems ! cb_native_log("Call for callback", 1); } extern "C" { DLL_EXPORT void initInterfaceCallbacks( CB_V_VI native_log, CB_V call ) { cb_native_log = native_log; cb_call = call; } DLL_EXPORT void callSmth() { cb_call(); } DLL_EXPORT int getSomeInt() { return 42; } DLL_EXPORT void initCallback() { 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() { // This works fine int number = getSomeInt(); Debug.Log("getSomeInt: " + number); // This also works fine and outputs "--- A callback ---" callSmth(); // This call gives the output "call_log: native log" but crashes afterwards ! initCallback(); } [MonoPInvokeCallback(typeof(DelegateVVi))] public static void NativeLog(IntPtr logMessage, int logLevel) { string result = StringFromCReturn(logMessage); UnityEngine.Debug.Log(result); // outputs "call_log: native log" } [MonoPInvokeCallback(typeof(DelegateV))] public static void Call() { UnityEngine.Debug.Log("--- A callback---"); } [DllImport(DLL)] private static extern void initCallback(); [DllImport(DLL)] private static extern void callSmth(); [DllImport(DLL)] private static extern int getSomeInt(); public static string StringFromCReturn(StringReturn someReturnVal) { return Marshal.PtrToStringAnsi(someReturnVal); } } 

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.

+6
source share
1 answer

Error STATUS_STACK_BUFFER_OVERRUN. The call destroyed the call stack.

You have different callback declarations in SimpleProjectDLL.cpp and SimpleProjectDLL.h. The CPP call dialog is used in the Cpp file, the call C dialog is used in the header.

You must modify SimpleProjectDLL.cpp by deleting

 typedef void(*CB_V)(); typedef void(*CB_V_VI)(const char * a, int b); 

and adding

 #include "SimpleProjectDLL.h" 
0
source

Source: https://habr.com/ru/post/1014416/


All Articles