java - JNA:在 Spotify API 中找不到 C 方法

标签 java api jna spotify

我试图了解 JNA 的工作原理,因此我决定使用 spotify API (libspotify 0.0.7)。我设法正确加载我的 dll,但看起来我的代码没有找到 API 中定义的任何方法。

这是我的测试代码:

public class Test { 
    static{
        System.loadLibrary("libspotify");
    }

    public interface LibSpotify extends Library {
        public static class sp_artist extends Structure{
            public String name;
            public boolean isLoaded;
            public int refCount;
        };

        LibSpotify INSTANCE = (LibSpotify)Native.loadLibrary("libspotify", LibSpotify.class);

        public String sp_artist_name(sp_artist artist);
    }

    public static void main(String[] args){
        LibSpotify ls = LibSpotify.INSTANCE;

        LibSpotify.sp_artist artist = new LibSpotify.sp_artist(){
            String name = "thename";
            boolean isLoaded = false;
            int refCount = 0;
        };

        ls.sp_artist_name(artist);
    }
}

这是我尝试访问的方法的 C 声明,位于 libspotify 的 api.h 中:

/**
 * Return name of artist
 *
 * @param[in]   artist     Artist object
 *
 * @return                 Name of artist.
 *                         Returned string is valid as long as the artist object stays allocated
 *                         and no longer than the next call to sp_session_process_events()
 */
SP_LIBEXPORT(const char *) sp_artist_name(sp_artist *artist);

这是我的 StackTrace:

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

    at com.sun.jna.Function.<init>(Function.java:129)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:250)
    at com.sun.jna.Library$Handler.invoke(Library.java:191)
    at $Proxy0.sp_artist_name(Unknown Source)
    at com.nbarraille.jspotify.main.Test2.main(Test2.java:33)

我的 DLL 有问题吗,还是我做错了什么?

顺便说一下,我无法访问C语言中sp_artist结构体的定义,我只是根据API提供的方法重构了它,这可能是问题所在吗?:

/**
 * @defgroup artist Artist subsystem
 * @{
 */

/**
 * Return name of artist
 *
 * @param[in]   artist     Artist object
 *
 * @return                 Name of artist.
 *                         Returned string is valid as long as the artist object stays allocated
 *                         and no longer than the next call to sp_session_process_events()
 */
SP_LIBEXPORT(const char *) sp_artist_name(sp_artist *artist);

/**
 * Check if the artist object is populated with data
 *
 * @param[in]   artist     An artist object
 *
 * @return                 True if metadata is present, false if not
 *
 */
SP_LIBEXPORT(bool) sp_artist_is_loaded(sp_artist *artist);


/**
 * Increase the reference count of a artist
 *
 * @param[in]   artist       The artist object
 */
SP_LIBEXPORT(void) sp_artist_add_ref(sp_artist *artist);

/**
 * Decrease the reference count of a artist
 *
 * @param[in]   artist       The artist object
 */
SP_LIBEXPORT(void) sp_artist_release(sp_artist *artist);

/** @} */

谢谢!

最佳答案

终于通过使用 Dependency Walker 打开 libspotify.dll 找到了解决方案: 编译器向方法名称添加了一些额外信息(下划线前缀和@4或@8后缀)。

我必须:

  • 创建 FunctionMapper 的实现,根据真实姓名重命名我的所有方法(可在 Dependency Walker 中找到)
  • 在选项映射中使用此映射器的实例实例化我的库。

关于java - JNA:在 Spotify API 中找不到 C 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5144662/

相关文章:

java - 概念证明 : how I can create a generic comparator method with reflection?

Java ThreadPoolExecutor - 关闭特定线程?

c# - 为什么基于堆栈的 IL 字节码中有局部变量

api - Spring Security 中接收 token 的基本身份验证

java - JAVA如何在linux中获取java.lang.Process的PID

java - 如何在一个类图中显示所有类

javascript - 创建 "bank transaction"并应用特定规则

php - 谷歌地图地理定位不请求许可

java - 在 Java 中使用 VisualBasic DLL

java - JNA 二维数组