简体   繁体   English

NuiGetSensorCount Java JNA

[英]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. 我只是在学习如何使用Java JNA,并且试图从Microsoft Kinect SDK中调用一个简单的函数。 (NuiGetSensorCount) which just returns the number of connected kinects. (NuiGetSensorCount),它仅返回已连接的kinect的数量。

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) 并提供一些信息/参考,以便我自己尝试调试(以某种方式查看Java JNA正在寻找的功能,并将其与.dll包含的功能进行比较)

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. 首先,我使用了一个名为dependency walker http://dependencywalker.com/的程序来查看DLL中的所有符号,然后确定所使用的DLL(Microsoft.Kinect.dll)实际上不包含我尝试的功能打电话。 I found out that Kinect10.dll was the one I needed. 我发现Kinect10.dll是我需要的文件。 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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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