【问题标题】:JNA: Cannot find C methods in Spotify APIJNA:在 Spotify API 中找不到 C 方法
【发布时间】:2011-02-28 16:28:32
【问题描述】:

我试图了解 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);

/** @} */

谢谢!

【问题讨论】:

    标签: java api jna spotify


    【解决方案1】:

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

    我不得不:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多