I was trying to learn how JNA works, so I decided to use the spotify API (libspotify 0.0.7). I managed to load my dll correctly, but then it looks like my code is not finding any of the method defined in the API.
Here is my test code:
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);
}
}
Here is the C declaration of the method I try to access, in the api.h of libspotify:
/**
* 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);
And here is my 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)
Is there a problem with my DLL, or am I doing something wrong?
By the way, I don't have access to the definition of the sp_artist structure in C, I just reconstructed it based on the methods offered by the API, could it be the problem?:
/**
* @defgroup artist Artist subsystem
* @{
*/
/**
* Return name of artist
*
* @param[in] artist Artist object
*
* @return Name of artist.
* Re开发者_如何转开发turned 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);
/** @} */
Thanks!
I finally found the solution by opening the libspotify.dll with Dependency Walker: The compiler added some extra information to the method name (a underscore prefix and a @4 or @8 suffix).
I had to:
- Create an implementation of FunctionMapper that renamed all my methods according to the real names (available in Dependency Walker)
- Instantiate my Library with an instance of this mapper in the options map.
精彩评论