简体   繁体   中英

NuiGetSensorCount Java JNA

I am just learning how to use Java JNA, and I am trying to call a simple function from the Microsoft Kinect SDK. (NuiGetSensorCount) which just returns the number of connected kinects.

Here is my attempt:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;

public class Driver {
    public interface KinectLibrary extends Library {
    KinectLibrary INSTANCE = (KinectLibrary)Native.loadLibrary(("Microsoft.Kinect"),KinectLibrary.class);

        //_Check_return_ HRESULT NUIAPI NuiGetSensorCount( _In_ int * pCount );
        NativeLong NuiGetSensorCount(Pointer pCount);
    }

    public static void main(String[] args) {
        Pointer devCount = new Pointer(0);
        KinectLibrary.INSTANCE.NuiGetSensorCount(devCount);
        System.out.println("Devices:"+devCount.getInt(0));
    }
}

But I get:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'NuiGetSensorCount': The specified procedure could not be found.

at com.sun.jna.Function.<init>(Function.java:208)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
at com.sun.jna.Library$Handler.invoke(Library.java:199)
at $Proxy0.NuiGetSensorCount(Unknown Source)
at Driver.main(Driver.java:30)

Can anybody provide help of how to change my code so it finds the correct native function? And also provide some information/reference so that I could try to debug this myself (some way to see what function Java JNA is looking for, and compare it to what the .dll contains)

I figured out my problem. First, I used a program called dependency walker http://dependencywalker.com/ to view all the symbols in the DLL and I determined that the DLL I was using (Microsoft.Kinect.dll) did not actually contain the function I was trying to call. I found out that Kinect10.dll was the one I needed. After changing that, I had to make a small change to my pointer and it works perfectly!

Here is the fixed code.

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;

    public class Driver{
    public interface KinectLibrary extends StdCallLibrary {
        KinectLibrary INSTANCE = (KinectLibrary)Native.loadLibrary(("Kinect10"),KinectLibrary.class);

        //_Check_return_ HRESULT NUIAPI NuiGetSensorCount( _In_ int * pCount );
        NativeLong NuiGetSensorCount(IntByReference pCount);
    }
    public static void main(String[] args) {
        IntByReference a = new IntByReference();
        KinectLibrary.INSTANCE.NuiGetSensorCount(a);
        System.out.println("Devices:"+a.getValue());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM