HoloLens Callbacks with Native Library - unity3d

My goal is to call methods, which are implemented in the Unity Code, from my UWP DLL. (So I can use them in my HoloLens Project)
I tried this with a bigger project but failed. Therefore I wrote a simple example to make it easier to find the mistake and exclude other influences.
But still, I get the same error.
My Working Environment:
64-bit Computer with OS Windows 10
Micsrosoft Visual Studio Community
2015 Version 14.0.25431.01 Update 3
HoloLens Emulator 10.0.14393.0
Unity 5.5.0f3 Personal (64 bit)
Creating the UWP DLL:
To approach this I created a C++ DLL(Windows Universal) in Visual Studio 2015 as followed:
New Project > Visual C++ > Windows > Universal > DLL(Universal Windows)
After the project was auto generated I added my code.
So the code looks like this:
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 prepearing the 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 did not make any changes to the auto generated files dllmain.cpp, pch.cpp, pch.h or targetver.h.
Finally I build the project for "Release" mode and architecture "x86" to generate the DLL-file.
Location of the DLL-file is now: project-root-folder/Release/SimpleProject/SimpleProjectDLL.dll.
---------------------
Next step I created a new Unity Project added the HoloLens-Toolkit and made sure that the new project is running fine on the emulator.
Unity Project Code:
After that I added the SimpleProjectDLL.dll in the Asset-Folder and implemented the following code:
First of all we need to create the connection between the delegates.
Cpp.cs prepears the 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 is calling the methodes:
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 a SLN, open the project in Visual Studio and start it with the "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 App just closes.
So my Question is, does anyone know what the problem could be?
Is this the right way to use callbacks in a HoloLens Project?
Or does someone know how to find an error description for the code "-1073740791 (0xc0000409)" ?
Additional Information:
I also tried it on a real HoloLens device, same issue, so the problem does not lays at the emulator.

The error is STATUS_STACK_BUFFER_OVERRUN. The call destroyed callstack.
You have different declarations of callbacks in SimpleProjectDLL.cpp and SimpleProjectDLL.h. Cpp file uses "CPP" call conversation, header uses "C" call conversation.
You should change SimpleProjectDLL.cpp by removing
typedef void(*CB_V)();
typedef void(*CB_V_VI)(const char * a, int b);
and adding
#include "SimpleProjectDLL.h"

Related

"DllAttribute could not be found": call a function in a .jslib file from unity

I tried to call a JavaScript Function from a .jslib file in Unity. I did it as described here
I have the .jslibe file in my assets/plugin folder. Now I tried to access it in the following code:
public class Prefab : MonoBehaviour
{
private string test = "test";
[DllImport("__Internal")]
private static extern void SendToJavscript(string test);
void Start()
{
SendToJavscript(this.test);
}
}
But it won´t compile and I get the error "The type or namespace name 'DllImportAttribute' could not be found (are you using directive or an assembly reference?)
Has anyone a idea what´s wrong here? I never got this error before..
At the top of your script add
using System.Runtime.InteropServices;

DllNotFoundException error when loading C++ DLL

I want to use C++ DLL in Unity, but I am prompted with “DllNotFoundException” error in Unity.I tried the method of others on the network, it didn't work for me.
I tried the method on Unity's official website, but it didn't work.
https://docs.unity3d.com/2017.3/Documentation/Manual/NativePlugins.html
https://docs.unity3d.com/2017.3/Documentation/Manual/PluginsForDesktop.html
Running the SimplestPluginExample Demo of Unity's official website is also prompted with “DllNotFoundException” error in Unity.
extern "C" int __declspec(dllexport) AddTwoIntegers(int a, int b)
{
return a + b;
}
put the MyDLL.dll in Assets/Plugins
[DllImport("MyDLL")]
private static extern int AddTwoIntegers(int a, int b);
void Start ()
{
Debug.Log(AddTwoIntegers(2, 3));
}
DllNotFoundException: MyDLL
Just try with extension. And put importing dll(MyDLL.dll) in output path of the function Caller project.
[DllImport("MyDLL.dll")]
private static extern int AddTwoIntegers(int a, int b);
void Start ()
{
Debug.Log(AddTwoIntegers(2, 3));
}
I solved my problem.My system is 64 bit.I used VS to create 64-bit DLLs.This dll in my Unity is correct,not error tip.

How to Turn off - VS CODE Opening all the internal classes it encounters during java debugging such as printstream class , String class

I am using VS CODE to learn java. While debugging , it opens all the internal classes it encounters in the code.
For example:-
class test{
public static void main(String[] args) {
int x= 7;
int y = 8;
System.out.println("Hello World"); // opens prints stream class and executes each line per click of this print stream class
int z = x+y;
}
}
What I want - when the debugger hits System.out.println("hello world"); it prints hello world and moves on rather than opening the printstream class.
Pictures below:
google stepFilter vscode and use it.

SharpDX.Direct Input in Unity

I've been trying to implement DirectInput into unity using SharpDX.DirectInput dlls, but when i create a joystick, it gives me an error:
SharpDXException: HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.
and here is my code:
using System;
using UnityEngine;
using SharpDX.DirectInput;
namespace KurdifyEngine.Input
{
static class Direct
{
public static DirectInput directInput;
public static Guid directGuid;
public static Joystick directGamepad ;
internal static void Initialize()
{
// Initialize DirectInput
directInput = new DirectInput();
directGuid = Guid.Empty;
DirectUpdate();
}
internal static void DirectUpdate()
{
// search for Gamepad
foreach (var deviceInstance in directInput.GetDevices(SharpDX.DirectInput.DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly))
directGuid = deviceInstance.InstanceGuid;
}
if (directGuid != Guid.Empty)
{
directGamepad = new Joystick(directInput, directGuid);
}
}
}
}
the error happens when i create a joystick:
directGamepad = new Joystick(directInput, directGuid);
We have been developing exotic controller support for a Unity product and encountered the same problem (SharpDXException: HRESULT: [0x80070057]) on the exact same line.
We tried recompiling the SharpDX by fixing it like it is explained in this github post: https://github.com/sharpdx/SharpDX/issues/406 but encountered other problems with the DirectX (June 2010) SDK.
We just found a way to compile our Unity project using the libraries included in this project (which is an add-on for exotic controllers support in Kerbal Space Program): https://github.com/pbatard/AltInput just pick up the .dll under Libraries/, replace your old .dll by these and you're good to go !

Eclipse Java JNI, LoadLibrary linking error

This my second time coding Java and never referencing any external library before. I follow the JNI examples online and I get UnsatisfiedLinkError when trying to load the dll. I thought that I have to create DLL first before trying to load, but all the examples I've looked they don't mention about creating DLL. Most of them stating that I should create Java code first then native code.
public class ClassSample1
{
public native void displayHelloWorld();
static
{
System.loadLibrary("MyLittleJNI");
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
ClassSample1 classSample1;
classSample1 = new ClassSample1();
classSample1.displayHelloWorld();
System.out.println("Hello");
}
}
How can I get ride of the error?
The sample code you provide assumes that there is a DLL in the search path called MyLittleJNI.dll, containing a method displayHelloWorld. The actual C function name in the DLL is decorated using a well defined syntax.
If you get an UnsatisfiedLinkError in loadLibrary(), it is because the JVM cannot find the DLL. You can duck the issue temporarily by specifying the full pathname to the DLL using the System.load(filename) method instead.
Once load or loadLibrary succeeds, you need to make sure that the native function is named correctly. To aid in this, you can use javah to generate a header file containing prototypes for all the native functions in a class.
More information about how to use JNI can be found in here and here.
EDIT: Also, the "Related" column to the right of this questions seems to contain several useful related question.
I try to create new project again.
So here is JNISample2.java file
public class JNISample2
{
static
{
System.loadLibrary("JNISample2Dll");
}
public native void displayHelloWorld();
public static void main(String[] args)
{
System.out.println("from java Hello");
JNISample2 JNIsample2;
JNIsample2 = new JNISample2();
JNIsample2.displayHelloWorld();
}
}
And here .h file that's generated by the javah -classpath . JNISample2
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNISample2 */
#ifndef _Included_JNISample2
#define _Included_JNISample2
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNISample2
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Here is my .h file dll that I create VS2005 with MFC app.
// JNISample2Dll.h : main header file for the JNISample2Dll DLL
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
#include "JNISample2.h"
// CJNISample2DllApp
// See JNISample2Dll.cpp for the implementation of this class
//
class CJNISample2DllApp : public CWinApp
{
public:
CJNISample2DllApp();
// Overrides
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld(JNIEnv *, jobject);
And here is my .cpp file
// JNISample2Dll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "JNISample2Dll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
//TODO: If this DLL is dynamically linked against the MFC DLLs,
// any functions exported from this DLL which call into
// MFC must have the AFX_MANAGE_STATE macro added at the
// very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
// CJNISample2DllApp
BEGIN_MESSAGE_MAP(CJNISample2DllApp, CWinApp)
END_MESSAGE_MAP()
// CJNISample2DllApp construction
CJNISample2DllApp::CJNISample2DllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CJNISample2DllApp object
CJNISample2DllApp theApp;
// CJNISample2DllApp initialization
BOOL CJNISample2DllApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld(JNIEnv *, jobject)
{
MessageBox(NULL, TEXT("In JNISample2Dll"), TEXT("DLL"), 1);
}
After I use command prompt to run: java JNISample2, it display the string "from java Hello", but how come it does not display messagebox that I put inside the .cpp DLL file?