Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Deploying a DLL to the HoloLens through Unity

bakkanekobakkaneko
edited July 2017 in Questions And Answers

I've been stuck on this for a few weeks now. I'm attempting to integrate OpenCV into my HoloLens project using this tutorial, and I do not believe that the Interop is working when it is deployed to the HoloLens.
When I run the game in Unity it correctly accessed the Init method and returns the correct value (-2, no camera stream to open). However, when deployed to the HoloLens it appears to get stuck in the Init function and does not move on to the next line.

  • I have tried just having one OpenCVHoloLens.dll (x64), I now have two, one each in the x64 and x86 folders in the Plugins folder. They are compiled for the selected architecture.
  • I have removed all OpenCV code from the method and rebuilt it to see if the OpenCV build was what was broken
  • I have completely redone the tutorial several times
  • Other projects (not using dlls) deploy and function in the HoloLens correctly

Function from the DLL source

extern "C" int __declspec(dllexport) __stdcall Init(int& outCameraWidth, int& outCameraHeight)
{
// Load LBP face cascade.
if (!_faceCascade.load("lbpcascade_frontalface.xml"))
return -1;
// Open the stream.
_capture.open(0);
if (!_capture.isOpened())
return -2;
outCameraWidth = _capture.get(CAP_PROP_FRAME_WIDTH);
outCameraHeight = _capture.get(CAP_PROP_FRAME_HEIGHT);
return 0;
}

Interop Function in Unity C# Script

internal static class OpenCVInterop
{[DllImport("OpenCVHoloLens")]
public static extern int Init(ref int outCameraWidth, ref int outCameraHeight);
[DllImport("OpenCVHoloLens")]
internal static extern int Close();
[DllImport("OpenCVHoloLens")]
internal static extern int SetScale(int downscale);
[DllImport("OpenCVHoloLens")]
internal unsafe static extern void Detect(CvCircle* outFaces, int maxOutFacesCount, ref int outDetectedFacesCount);
}

Call in **same **Unity script. Renderers used for visual debugging

int camWidth = 0, camHeight = 0;
rend2 = box2.GetComponent();
rend2.enabled = false;
//FOUND BUG: UNABLE TO ACCESS OpenCVInterop methods
int result = OpenCVInterop.Init(ref camWidth, ref camHeight);
rend3 = box3.GetComponent();
rend3.enabled = false;

using System.Runtime.InteropServices; is included in the Unity script.

Error when run from Start with Debugging:

Exception thrown: 'System.BadImageFormatException' in Assembly-CSharp.dll
'OpenCVTest.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\OpenCVTestVS.Release_x86.vr\System.Diagnostics.StackTrace.dll'. Cannot find or open the PDB file.
BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at OpenCVInterop.Init(Int32& outCameraWidth, Int32& outCameraHeight)
at OpenCVFaceDetection.Start()
at OpenCVFaceDetection.$Invoke3(Int64 instance, Int64* args)
at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
(Filename: Line: 0)
Setting up 1 worker threads for Enlighten.
Thread -> id: ea4 -> priority: 1

I apologize for formatting. The code indicator was not working correctly.

Tagged:

Answers

  • Any logs? Errors?

    Did you include your dll in for the WSAPlayer platform? You do this by selecting the dll in your project folder and then checking the platforms and cpu that you would like the dll to be included with when building.

    Here is the compositor from spectator view (though this one is set to only run in editor)

    Taqtile

  • I added the error above. I do not appear to have the WSA Plugin (lord knows why), and HoloToolkit doesn't have a Compositor.dll that I can find. Where can I find those?

  • Don't worry about the compositor.dll. That was just the only dll I had in my scene so I clicked on that to view its properties for the screenshot.

    What version of Unity are you using?

    Do your build settings look like this:

    Can you send a screenshot of what the inspector looks like when you click on your dll?

    Taqtile

  • All of my settings match except I have Debugging -> Unity C# Projects checked.
    I don't think it matters, but I am deploying via USB because I do not have accessible WiFi here.

  • hmm. Seems correct. Perhaps try with only the x86 version in the project as I think that is the only one used by Hololens.

    Taqtile

  • Any update on how to use a DLL on the Hololens via Unity?

  • There should be no issue using a dll on the hololens as long as the dll targets x86 and supports UWP.

    Taqtile

Sign In or Register to comment.