我的目标是从我的UWP DLL中调用Unity代码中实现的方法.(所以我可以在我的HoloLens项目中使用它们)
我尝试了一个更大的项目,但失败了.因此,我写了一个简单的例子,以便更容易找到错误并排除其他影响.但是,我仍然得到同样的错误.
我的工作环境:
具有OS Windows 10的64位计算机
Micsrosoft Visual Studio社区2015版本14.0.25431.01更新3
HoloLens仿真器10.0.14393.0
Unity 5.5.0f3个人(64位)
创建UWP DLL:
为了解决这个问题,我在Visual Studio 2015中创建了一个C++ DLL(Windows Universal),如下所示:
新建项目> Visual C++> Windows>通用> DLL(通用Windows)
项目自动生成后,我添加了我的代码.所以代码看起来像这样:
本地图书馆代码:
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正在预备代理:
SimpleProjectDLL.h: #pragma once #include#define DLL_EXPORT __declspec(dllexport) extern "C" { typedef void(*CB_V)(); typedef void(*CB_V_VI)(const char * a, int b); }
我没有对自动生成的文件dllmain.cpp,pch.cpp,pch.h或targetver.h进行任何更改.
最后,我为"Release"模式和体系结构"x86"构建项目以生成DLL文件.DLL文件的位置现在是:project-root-folder/Release/SimpleProject/SimpleProjectDLL.dll.
---------------------
下一步我创建了一个新的Unity项目添加了HoloLens-Toolkit,并确保新项目在模拟器上运行正常.
Unity项目代码:
之后,我在Asset-Folder中添加了SimpleProjectDLL.dll并实现了以下代码:
首先,我们需要在委托之间创建连接.Cpp.cs预备代表:
Cpp.cs using UnityEngine; using System; using System.Runtime.InteropServices; namespace Cpp { delegate void DelegateV(); delegate void DelegateVVi(IntPtr a, int b); }
SimpleInterfaceCpp.cs初始化连接:
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 ); }
主要:
MainController.cs using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class MainController : MonoBehaviour { void Start () { SimpleInterfaceCpp.Init(); SimpleInterface.TestCalls(); } }
SimpleInterface.cs正在调用方法:
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); } }
现在,如果我创建一个SLN,在Visual Studio中打开项目并使用"HoloLens Emulator"启动它,我得到以下输出:
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).
之后,应用程序才关闭.
所以我的问题是,有谁知道问题可能是什么?
这是在HoloLens项目中使用回调的正确方法吗?
或者有人知道如何找到代码"-1073740791(0xc0000409)"的错误描述?
附加信息: 我也在真正的HoloLens设备上尝试过,同样的问题,所以问题不在于模拟器.