Microsoft Kinect SDK Wrapper For Unity Crash Bug Fix

Posted by on May 10, 2012 in 3D, Kinect20 comments

There’s a great free Kinect SDK wrapper available for Unity. It’s free & open source but there are still a few problems getting it to run with the 1.0 SDK (as opposed to the beta).

The first problem is that it is pointing to the wrong dll file. When you get this exception:

DllNotFoundException: C:\Program Files (x86)\Microsoft Research KinectSDK\MSRKINECTNUI.DLL

You should open the file KinectInterop.cs and changes all dll paths to:

C:\Windows\System32\Kinect10.dll

This will fix all compiler errors and it should run without problems.

However, it will only run once. When you run it the second time Unity will freeze and you will have to kill the process. Not very convenient.

This is caused by a bug in the Microsoft SDK. According to this page the problem is:

If C++ code is executing NuiInitializa/NuiShutdown multiple times through
the application's lifetime, SetDeviceStatusCallback should be called once,
before invoking those calls.

So apparently a single call to SetDeviceStatusCallback() should fix the problem. To be able to call this method we need to add some code to the KinectInterop.cs file. First of all we need to add an empty struct:

public struct NuiStatusProc
{
}

Then we need to link the native method. In the NativeMethods class add:


[DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiSetDeviceStatusCallback")]
	    public static extern void NuiSetDeviceStatusCallback(NuiStatusProc callback);

Now open the file KinectSensor.cs and add this line to the void Awake() method (just before the line “catch (Exception e)”):


NativeMethods.NuiSetDeviceStatusCallback(new NuiStatusProc());

Now everything should run fine. If it doesn’t let me know :O



Tags: , ,


20 comments

» Comments RSS Feed
  1. Great thanks!

  2. Great! Many thanks!

  3. Hi, I’m trying to implement this solution, but I received a compilation error “CS0246: The type or namespace name ‘NuiStatusProc’ could not be found. Are you missing a using directive or an assembly reference?”

    The KinectInterop.cs file stay that way:

    public struct NuiStructProc
    {

    }

    public class NativeMethods
    {
    /*
    * kinect NUI (general) functions
    */

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiInitialize")]
    public static extern int NuiInitialize(NuiInitializeFlags dwFlags);

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiShutdown")]
    public static extern void NuiShutdown();

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiCameraElevationSetAngle")]
    public static extern int NuiCameraSetAngle(long angle);

    /*
    * kinect skeleton functions
    */

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiSkeletonTrackingEnable")]
    public static extern int NuiSkeletonTrackingEnable(IntPtr hNextFrameEvent, uint dwFlags);

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiSkeletonGetNextFrame")]
    public static extern int NuiSkeletonGetNextFrame(uint dwMillisecondsToWait, ref NuiSkeletonFrame pSkeletonFrame);

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiTransformSmooth")]
    public static extern int NuiTransformSmooth(ref NuiSkeletonFrame pSkeletonFrame,ref NuiTransformSmoothParameters pSmoothingParams);

    /*
    * kinect video functions
    */

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiImageStreamOpen")]
    public static extern int NuiImageStreamOpen(NuiImageType eImageType, NuiImageResolution eResolution, uint dwImageFrameFlags_NotUsed, uint dwFrameLimit, IntPtr hNextFrameEvent, ref IntPtr phStreamHandle);

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiImageStreamGetNextFrame")]
    public static extern int NuiImageStreamGetNextFrame(IntPtr phStreamHandle, uint dwMillisecondsToWait, ref IntPtr ppcImageFrame);

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiImageStreamReleaseFrame")]
    public static extern int NuiImageStreamReleaseFrame(IntPtr phStreamHandle, IntPtr ppcImageFrame);

    [DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiSetDeviceStatusCallback")]
    public static extern void NuiSetDeviceStatusCallback(NuiStatusProc callback);

    and my KinectSensor.cs file stay that way:


    NativeMethods.NuiSetDeviceStatusCallback(new NuiStatusProc());
    }
    catch (Exception e)
    {
    Debug.Log(e.Message);
    }

    I don’t knowhow to make it work, i’ve tred to implement attributes to the NuiStatusProc struct, but i don’t get the solution.

    Thanks a lot.

  4. Go Pumas! :)

    There’s a typo in your code:
    public struct NuiStructProc
    should be
    public struct NuiStatusProc

  5. Sorry, my mistake of programer. For me just Kinect.NuiStatusproc and worked fine. Thanks a lot for thisinformation that was looking for 3 weeks ago. Good job!

  6. Novice error, i’m shamed for this error. And yes, it works fine. Of course, let’s go Pumas!!! any e-mail if you want. arturin_ab@msn.com

  7. Has anyone been able to get DisplayColor or DisplayDepth working in SDK v1.5, I have the skeleton working without issues, but both colour and depth are a no show.

    Thanks.

  8. Hi, I already read the page and follow the same steps, but in my System32 I didn’t found the Kinect10.dll. What it’s supposed to I have do? thanks.

  9. I already read and follow your steps, but it doesn’t work for me.
    I’ve received a compilation error “UnityEngine.Debug:Log(Object)
    KinectSensor:Awake() (at Assets/Kinect/KinectSensor.cs:126)”

    What should i do? … help me please thanks :) ))

  10. @Shippo i got same error but then it suddenly worked without error. if i solve the problem i will inform you.

  11. I got this error : NuiInitialize Failed.
    UnityEngine.Debug:Log(Object)
    KinectSensor:Awake() (at Assets/Kinect/KinectSensor.cs:144)
    when trying to execute the example
    What can I do?

  12. Very useful article. Worked first time. Thanks so much!

  13. Brilliant. Many thanks.

  14. has anyone succeeded with accessing the color image? pls help…

  15. Hi,
    do you know how to import kinectaudio10.dll?
    i need to incorporate voice recognition with wrapper but dont know how :/

  16. Unfortunately, it doesn’t work. Followed your advice PRECISELY, but it keeps throwing NuiInitialize failed (blabla) line 143. Line 143 is merely a Debug.Log(e.Message);

    Changed DLL ref, did the struct thing, did the call thing.

    Any help?

  17. Does the wrapper works fine with “displaying depth-map”??

    I also develop an software using these wrapper, but it does not work with displaying a depthmap or color map, but can’t find how to fix it.

  18. Precise solution for a precise problem, It works really well!

  19. “Does the wrapper works fine with “displaying depth-map”??

    I also develop an software using these wrapper, but it does not work with displaying a depthmap or color map, but can’t find how to fix it.”

    Anyone with answer please???

  20. thanks…!!

Leave a comment