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

时间:2011-02-28 16:28:33

标签: 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);

/** @} */

谢谢!

1 个答案:

答案 0 :(得分:0)

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

我必须:

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