C回调与JNA使JRE崩溃 [英] C callback with JNA makes JRE crash

查看:2031
本文介绍了C回调与JNA使JRE崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理JNA中的回调时遇到问题。



我试图使用一个使用回调来处理多个会话事件out,连接问题...)。

会话对象(称为 sp_session )是一个不透明结构。所有回调都注册在 sp_session_callbacks 结构中。根据API,我应该声明callbacks对象,并将其放入创建 sp_session 对象时将提供的Config对象中。如果我不想使用某些回调,我应该用 null 初始化它们。 API使用 __ stdcall 调用约定。



这是一个与我的问题相关的C头文件:

  #define SP_CALLCONV __stdcall 

typedef struct sp_session sp_session; ///<表示会话

typedef枚举sp_error {
SP_ERROR_OK = 0,
SP_ERROR_BAD_API_VERSION = 1,
/ *更多错误* /
} sp_error;

typedef struct sp_session_callbacks {
/ **
*当登录已处理并成功时调用
* /
void(SP_CALLCONV * logged_in) sp_session * session,sp_error error);

/ **
*处理注销后调用。如果你初始化一个注销操作,或者如果
*是一个永久的连接错误
*
* @param [in] session session
* /
void(SP_CALLCONV * logged_out)(sp_session * session);

/ **
*在出现连接错误时调用,库在重新连接到Spotify服务时出现问题
*。可以多次调用(作为
* long,因为存在问题)
* /
void(SP_CALLCONV * connection_error)(sp_session * session,sp_error error);

/ *更多回调* /
} sp_session_callbacks;

/ **
*初始化会话。返回的会话将被初始化,但您需要
*才能执行任何其他操作
* /
SP_LIBEXPORT(sp_error)sp_session_create(const sp_session_config * config,sp_session ** sess);

这是我的等效JNA代码:



sp_session对象

  public class sp_session extends PointerType {

public sp_session(Pointer address){
super(address);
}
public sp_session(){
super();
}
}

sp_session_callbacks对象包含所有回调

  public class sp_session_callbacks extends Structure {
public LoggedIn logged_in;
public LoggedOut logged_out;
public ConnectionError connection_error;
}

回调对象(这里是LoggedIn,每个回调一个)

  public interface LoggedIn extends StdCallCallback {
public void logged_in(sp_session session, int error);
}

本地库,声明所有方法

  public interface JLibspotify extends StdCallLibrary {
int sessionCreate(sp_session_config config,PointerByReference sess);
int sessionLogin(sp_session session,String username,String password);
//由API定义的所有其他方法
}

我的主类,绑定所有在一起

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

public static void main(String [] args){
JLibspotify lib =(JLibspotify)Native.loadLibrary(libspotify,JLibspotify.class);
sp_session_config cfg = new sp_session_config();
}
sp_session_callbacks sessCallbacks = new sp_session_callbacks();
LoggedIn loggedInCallback = new LoggedIn(){
public void logged_in(sp_session session,int error){
System.out.println(logged_in()called);
}

};
sessCallbacks.logged_in = loggedInCallback;

cfg.session_callbacks = sessCallbacks;

PointerByReference sessionPbr = new PointerByReference();

int error_id = sessionCreate(cfg,sessionPbr); // CRASHES HERE

sp_session mySession = new sp_session(sessionPbr.getValue());
}
}



因此,sessionCreate函数调用会使JRE崩溃 EXCEPTION_ACCESS_VIOLATION(0xc0000005)有问题的框架:C [jna3666290841889849729.dll + 0xa3f4]



看起来像logged_in回调是导致这个,因为当我设置为 null 它运行确定。此外,如果我初始化 connection_error 回调,它具有完全相同的签名,它也不会崩溃。



我运行的版本3.2.7的JNA。我试图与前版本(3.0.9),它也失败了。
我运行的JDK 1.7测试版本,但我尝试与1.6和它也失败。



谢谢!

 
#Java运行时环境检测到致命错误:

#EXCEPTION_ACCESS_VIOLATION(0xc0000005)at pc = 0x0499a3f4,pid = 1368,tid = 1344

#JRE版本:7.0-b129
#Java VM:Java HotSpot TM客户端VM(21.0-b01混合模式,共享windows-x86)
#有问题的框架:
#C [jna3666290841889849729.dll + 0xa3f4]

#如果您想提交错误报告,请访问:
#http://java.sun.com/webapps/bugreport/crash.jsp
#崩溃发生在Java虚拟机之外的本机代码中。
#见有问题的框架,在哪里报告错误。


--------------- THREAD ---------------

当前线程(0x01b79400):JavaThreadmain[_thread_in_native,id = 1344,stack(0x00340000,0x00390000)]

siginfo:ExceptionCode = 0xc0000005,读取地址0x1993053a
$ b b寄存器:
EAX = 0x19930522,EBX = 0x100dc77c,ECX = 0x00010004,EDX = 0x1008d3e0
ESP = 0x0038f58c,EBP = 0x0038f5b4,ESI = 0x0038f5a4,EDI = 0x100b79b0
EIP = 0x0499a3f4,EFLAGS = 0x00210212

堆栈顶部:(sp = 0x0038f58c)
0x0038f58c:100dc77c 0038f5a4 00010004 0038f688
0x0038f59c:05983de0 05981330 0598289c 05983de0
0x0038f5ac:05983de0 00000014 0038f688 1008d3ea
0x0038f5bc:05983de0 10030d77 0038fc44 100b79b0
0x0038f5cc:ffffffff 1008d334 00000000 05983de0
0x0038f5dc:1008d3e0 05983fc4 1008d9fd 0038f770
0x0038f5ec:00000000 00000000 7275016a 055310b0
0x0038f5fc:00000000 00010001 00000000 00000000

说明:(pc = 0x0499a3f4)
0x0499a3d4:01 00 89 e5 57 56 8d 75 f0 53 83 ec 1c 8b 7d 14
0x0499a3e4:8b 5f 4c 8b 03 89 4c 24 08 89 74 24 04 89 1c 24
0x0499a3f4:ff 50 18 83 ec 0c 85 c0 0f 94 c0 0f b6 c0 85 c0
0x0499a404:89 45 ec 75 19 8b 03 31 d2 89 54 24 08 89 74 24


注册到内存映射:

EAX = 0x19930522是未知值
EBX = 0x100dc77c是未知值
ECX = 0x00010004是未知值
EDX = 0x1008d3e0是未知值
ESP = 0x0038f58c指向线程堆栈:0x01b79400
EBP = 0x0038f5b4指向堆栈线程:0x01b79400
ESI = 0x0038f5a4指向线程的堆栈:0x01b79400
EDI = 0x100b79b0是未知值


堆栈:[0x00340000,0x00390000],sp = 0x0038f58c,自由空间= 317k
本机框架(J =编译的Java代码,j =解释,Vv = VM代码,C =本地代码)
C [jna3666290841889849729.dll + 0xa3f4] Java_com_sun_jna_Native_initialize_1ffi_1type + 0x1054
C [libspotify.dll + 0x8d3ea] sp_error_message + 0x35a
C [jna3666290841889849729.dll + 0xcb77] Java_com_sun_jna_Native_initialize_1ffi_1type + 0x37d7
C [jna3666290841889849729.dll + 0xc7c2] Java_com_sun_jna_Native_initialize_1ffi_1type + 0x3422
C [jna3666290841889849729。 dll + 0x4561] Java_com_sun_jna_Pointer__1getString + 0xa31
C [jna3666290841889849729.dll + 0x4d2e] Java_com_sun_jna_Function_invokeInt + 0x2e
j com.sun.jna.Function.invokeInt(I [Ljava / lang / Object;)I + 0
j com.sun.jna.Function.invoke([Ljava / lang / Object; Ljava / lang / Class; Z)Ljava / lang / Object; +315
j com.sun.jna.Function.invoke / lang / Class; [Ljava / lang / Object; Ljava / util / Map;)Ljava / lang / Object; +214
j com.sun.jna.Library $ Handler.invoke(Ljava / lang / Object; Ljava / lang / reflect / Method; [Ljava / lang / Object;)Ljava / lang / Object; +341
j $ Proxy0.sp_session_create(Lcom / nbarraille / jspotify / model / sp_session_config; Lcom / sun / jna / ptr / PointerByReference;)I + 20
j com.nbarraille.jspotify.main.Test.main([Ljava / lang / String;)V + 273
v〜StubRoutines :: call_stub
V [jvm。 dll + 0x115f6d]
V [jvm.dll + 0x1b788e]
V [jvm.dll + 0x115fed]
V [jvm.dll + 0xa2507]
V [jvm.dll + 0xac867]
C [javaw.exe + 0x209e]
C [javaw.exe + 0xa23b]
C [javaw.exe + 0xa2c5]
C [kernel32.dll + 0x51194] BaseThreadInitThunk + 0x12
C [ntdll.dll + 0x5b429] RtlInitializeExceptionChain + 0x63
C [ntdll.dll + 0x5b3fc] RtlInitializeExceptionChain + 0x36

Java框架: ,j = interpret,Vv = VM代码)
j com.sun.jna.Function.invokeInt(I [Ljava / lang / Object;)I + 0
j com.sun.jna.Function.invoke [Ljava / lang / Object; Ljava / lang / Class; Z] Ljava / lang / Object; +315
j com.sun.jna.Function.invoke(Ljava / lang / Ljava / lang / Object; Ljava / lang / Object; +214
j com.sun.jna.Library $ Handler.invoke(Ljava / lang / Object; Ljava / lang / reflect / Object;)Ljava / lang / Object; +341
j $ Proxy0.sp_session_create(Lcom / nbarraille / jspotify / model / sp_session_config; Lcom / sun / jna / ptr / PointerByReference;)I + 20
j com。 nbarraille.jspotify.main.Test.main([Ljava / lang / String;)V + 273
v〜StubRoutines :: call_stub

------------ --- PROCESS ---------------

Java Threads:(=>当前线程)
0x0182fc00 JavaThread低内存检测器守护程序[_thread_blocked,id = 4664,stack(0x04170000,0x041c0000)]
0x0182ac00 JavaThreadC1 CompilerThread0守护程序[_thread_blocked,id = 1728, ,0x01b70000)]
0x01829800 JavaThreadAttach Listener守护进程[_thread_blocked,id = 112,stack(0x04020000,0x04070000)]
0x01826400 JavaThreadSignal Dispatcher守护进程[_thread_blocked,id = 5836,stack ,0x03ef0000)]
0x01819800 JavaThreadFinalizer守护程序[_thread_blocked,id = 4724,stack(0x03f80000,0x03fd0000)]
0x01817800 JavaThreadReference Handler守护程序[_thread_blocked,id = 3940, 0x01b20000)]
=> 0x01b79400 JavaThreadmain[_thread_in_native,id = 1344,stack(0x00340000,0x00390000)]

其他主题:
0x01816400 VMThread [stack:0x01a30000 ,0x01a80000] [id = 3876]
0x01843000 WatcherThread [stack:0x040d0000,0x04120000] [id = 4636]

虚拟机状态:未处于正常执行状态

VM Mutex / Monitor当前由一个线程拥有:None

Heap
def新一代总计4928K,使用3630K [0x23450000,0x239a0000,0x289a0000)
eden空间4416K,82 %used [0x23450000,0x237db858,0x238a0000)
从空间512K,0%使用[0x238a0000,0x238a0000,0x23920000)
到空间512K,使用0%[0x23920000,0x23920000,0x239a0000]
生成总共10944K,使用0K [0x289a0000,0x29450000,0x33450000)
空间10944K,0%使用[0x289a0000,0x289a0000,0x289a0200,0x29450000)
压缩永久总共12288K,使用860K [0x33450000,0x34050000, 0x37450000)
空间12288K,7%已使用[0x33450000,0x33527190,0x33527200,0x34050000)
ro空间10240K,43%已使用[0x37450000,0x3789ce40,0x3789d000,0x37e50000]
rw空间12288K, 53%used [0x37e50000,0x384c2710,0x384c2800,0x38a50000)

代码缓存[0x01e90000,0x01f20000,0x03e90000)
total_blobs = 234 nmethods = 82 adapters = 88 free_code_cache = 32972224 largest_free_block = 0

动态库:
0x00880000 - 0x008b0000 C:\Program Files\Java\jdk1.7.0\bin\javaw.exe
0x778f0000 - 0x77a2d000 C:\Windows \SYSTEM32\\\
tdll.dll
0x77070000 - 0x77144000 C:\Windows\system32\kernel32.dll
0x75cf0000 - 0x75d3a000 C:\Windows \system32\KERNELBASE.dll
0x60000000 - 0x60041000 C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\ Midas_00078_002\midas32.dll
0x61000000 - 0x61028000 C:\Program Files\BitDefender\BitDefender 2011 \Active Virus Control\Midas_00078_002\plugin_base.m32
0x67000000 - 0x67048000 C:\Program Files\BitDefender\BitDefender 2011 \Active Virus Control\ Midas_00078_002\plugin_nt.m32
0x64000000 - 0x64021000 C:\Program Files\BitDefender\BitDefender 2011 \Active Virus Control\ Midas_00078_002\plugin_registry.m32
0x62000000 - 0x6202d000 C:\Program Files\BitDefender\BitDefender 2011 \Active Virus Control\Midas_00078_002\plugin_extra.m32
0x65000000 - 0x6501a000 C:\Program Files\BitDefender\BitDefender 2011 \Active Virus Control\ Midas_00078_002\plugin_net.m32
0x63000000 - 0x630a6000 C:\Program Files\BitDefender\BitDefender 2011 \Active Virus Control\ Midas_00078_002\plugin_fragments.m32
0x75e10000 - 0x75eb0000 C:\Windows \system32\ADVAPI32.dll
0x775c0000 - 0x7766c000 C:\Windows \system32 \msvcrt.dll
0x75d50000 - 0x75d69000 C:\Windows \SYSTEM32\sechost.dll
0x77670000 - 0x77711000 C:\ Windows \system32\RPCRT4.dll
0x76f00000 - 0x76fc9000 C:\Windows \system32\USER32.dll
0x77a50000 - 0x77a9e000 C:\Windows \system32\GDI32.dll
0x77a30000 - 0x77a3a000 C:\Windows\system32\LPK.dll
0x75d70000 - 0x75e0d000 C:\Windows \system32\USP10.dll
0x74830000 - 0x749ce000 C:\ Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7600.16661_none_420fe3fa2b8113bd\COMCTL32.dll
0x772f0000 - 0x77347000 C:\Windows\system32\SHLWAPI.dll
0x775a0000 - 0x775bf000 C :\Windows \system32\IMM32.DLL
0x75eb0000 - 0x75f7c000 C:\Windows \system32\MSCTF.dll
0x72740000 - 0x727fe000 C:\Program Files\Java\ jdk1.7.0\jre\bin\msvcr100.dll
0x01b80000 - 0x01e89000 C:\Program Files\Java\jdk1.7.0\jre\bin\client\jvm.dll
0x746c0000 - 0x746f2000 C:\Windows \system32\WINMM.dll
0x73920000 - 0x7392c000 C:\Program Files \Java\jdk1.7.0\\\\\\\\\\ dll
0x73330000 - 0x73350000 C:\Program Files\Java\jdk1.7.0\jre\bin\java.dll
0x75d40000 - 0x75d45000 C:\Windows \system32 \ PSAPI.DLL
0x733a0000 - 0x733b3000 C:\Program Files\Java\jdk1.7.0\jre\bin\zip.dll
0x10000000 - 0x10259000 C:\Windows \System32 \libspotify.dll
0x77150000 - 0x77185000 C:\Windows\system32\WS2_32.dll
0x77a40000 - 0x77a46000 C:\Windows \system32\NSI.dll
0x75ba0000 - 0x75cbc000 C:\Windows\system32\CRYPT32.dll
0x75ab0000 - 0x75abc000 C:\Windows \system32\MSASN1.dll
0x74ee0000 - 0x74f38000 C:\Windows \system32 \WINHTTP.dll
0x74e90000 - 0x74edf000 C:\Windows\system32\webio.dll
0x754c0000 - 0x754d6000 C:\Windows \system32\CRYPTSP.dll
0x75260000 - 0x7529b000 C:\Windows \system32\rsaenh.dll
0x750a0000 - 0x750b7000 C:\Windows \system32\USERENV.dll
0x75a40000 - 0x75a4b000 C:\Windows \system32 \profapi.dll
0x75a30000 - 0x75a3c000 C:\Windows\system32\CRYPTBASE.dll
0x728a0000 - 0x728b6000 C:\Program Files\Java\jdk1.7.0\jre\\ \\bin \\\
et.dll
0x75480000 - 0x754bc000 C:\Windows\system32\mswsock.dll
0x75470000 - 0x75476000 C:\Windows \System32\wship6.dll
0x73950000 - 0x73960000 C:\Windows\system32\NLAapi.dll
0x75340000 - 0x75384000 C:\Windows \system32\DNSAPI.dll
0x71030000 - 0x71038000 C:\Windows \\ \\ System32 \winrnr.dll
0x71020000 - 0x71030000 C:\Windows \system32\\\
apinsp.dll
0x71000000 - 0x71012000 C:\Windows \system32\pnrpnsp.dll
0x74fd0000 - 0x74fd5000 C:\Windows\System32\wshtcpip.dll
0x74d30000 - 0x74d4c000 C:\Windows \system32\IPHLPAPI.DLL
0x74d20000 - 0x74d27000 C:\Windows \\ \\system32\WINNSI.DLL
0x70c60000 - 0x70c66000 C:\Windows \system32\rasadhlp.dll
0x71ba0000 - 0x71bd8000 C:\Windows \System32\fwpuclnt.dll
0x73930000 - 0x7393f000 C:\Program Files\Java\jdk1.7.0\jre\bin\\\
io.dll
0x04990000 - 0x049e5000 C:\Users\\\
barraille\AppData\Local \Temp\jna3666290841889849729.dll

VM参数:
jvm_args:-Djava.library.path = C:\Windows \System32 -Dfile.encoding = Cp1252
java_command:com.nbarraille.jspotify.main.Test
启动程序类型:SUN_STANDARD

环境变量:
PATH = C:/ Program Files / Java / jdk1.7.0 / bin / ../jre/bin/client;C:/Program Files / Java / jdk1.7.0 / bin /../ jre / bin; C:/ Program Files / Java / jdk1.7.0 / bin /../ jre / lib / i386; C:\Windows \System32
USERNAME = nbarraille
OS = Windows_NT
PROCESSOR_IDENTIFIER = x86 Family 6 Model 23 Stepping 10,GenuineIntel



---------------系统---------------

操作系统:Windows 7 Build 7600

CPU:total 2(每个CPU 2个核心,每个核心1个线程)系列6型号23步进10,cmov,cx8,fxsr,mmx,sse,sse2,sse3,ssse3,sse4.1

内存:4k页面,物理3624108k(885020k免费),交换7246452k(3637664k免费)

vm_info:用于Windows的Java HotSpot(TM)客户端VM(21.0-b01) -x86 JRE(1.7.0-ea-b129),建于2011年2月10日07:43:39由java_re与未知的MS VC ++:1600

时间:Mon Mar 07 14:25: 08 2011
已用时间:0秒

EDIT:
* 在C中测试API的代码

*

  #includeapi.h 


/ * ---数据--- * /
const uint8_t g_appkey [] = {/ * My appkey * /};


/ * ---------------------------会议呼叫------- ------------------ * /
static void logged_in(sp_session * sess,sp_error error){
printf(logged_in()called:\ n);
}

static void log_message(sp_session * sess,const char * data){
printf(log_message()called%s:\\\
,data);
}

static void notify_main_thread(sp_session * sess){
printf(notify_main_thread()called \\\

}

static sp_session_callbacks session_callbacks = {
//.logged_in =(void *)& logged_in,
//.notify_main_thread =(void *)& notify_main_thread,
//.log_message =(void *)& log_message,
};

static sp_session_config spconfig = {
.api_version = 7,
.cache_location =tmp,
.settings_location =tmp,
.application_key = g_appkey,
.application_key_size = sizeof(g_appkey),
.user_agent =jspotify,
.callbacks =& session_callbacks,
.userdata = NULL
} ;

int main(int argc,char ** argv)
{
sp_session * sp;

const char * username =foo;
const char * password =bar;

spconfig.application_key_size = sizeof(g_appkey);


printf(Creating session \\\
);

sp_error err = sp_session_create(& spconfig,& sp);

if(err!= 0){
printf(错误发生:%d \\\
,err);
return 0;
}

printf(Login \\\
);
sp_session_login(sp,username,password);

Sleep(10000);
return 0;
}

当我像这样(没有回调)运行时,输出是: / p>

 创建会话
登录

当我注册回调(取消注释行),它甚至不打印任何东西!



当调用回调时打印任何东西,因为如果我注释 sp_session_login 行,只有 notify_main_thread 是唯一一个打印的)将阻止程序打印...

解决方案

p>

  struct Foo {
Bar * bar
}
pre>

ie一个包含指向另一个结构的指针,你的JNA实现Bar( class Bar extends Structure )也必须实现 Structure.ByReference interface - 否则JNA会认为 struct Foo 包含 struct Bar 的实例,而不是指向a struct Bar ,并且当C代码将Bar实例中的值解释为指针时,将导致非法的内存访问。



您需要向 sp_session_callbacks 类添加实现Structure.ByReference

$ b $感谢您启发我查看JNA - 非常酷!



这段代码的输出是:

  sp_session_create返回0 
sp_session_login返回0
log_message()调用:14:16:53.825 I [ap:1388] ap.spotify.com:4070

log_message()调用:14:16:54.061 I [ap:938]连接到AP:193.182.8.11:4070

log_message )调用:14:16:54.765 E [ap:3396]连接错误:401

退出代码0

  import com.sun.jna。*; 
import com.sun.jna.ptr.PointerByReference;

import java.sql.Connection;

public class JNATest {
// static {
// System.loadLibrary(libspotify);
//}

public interface JLibspotify extends Library {
int sp_session_create(sp_session_config config,PointerByReference sess);

int sp_session_login(sp_session session,String username,String password);
//由API定义的所有其他方法
}

public static class sp_session extends PointerType {

public sp_session(Pointer address){
super(address);
}

public sp_session(){
super();
}
}

public static class sp_session_config extends Structure {
public int api_version = 7; //应用程序编译的Spotify API的版本。
public String cache_location =。;
public String settings_location =。;
public Pointer application_key; //您的应用程序键。
public int application_key_size; //应用程序键的大小(以字节为单位)
public String user_agent =jspotify;
public sp_session_callbacks callbacks; //交付会话事件的回调。 NULL如果不感兴趣任何回调
public Pointer userdata; //用户提供的应用程序数据
public boolean compress_playlists;
public boolean dont_save_metadata_for_playlists;
public boolean initial_unload_playlists;
}

public interface LoggedIn extends Callback {
public void logged_in(sp_session session,int error);
}

public interface LoggedOut extends Callback {
public void logged_out(sp_session session,int error);
}

public interface ConnectionError extends Callback {
public void connection_error(sp_session session,int error);
}

public static class sp_session_callbacks extends结构实现Structure.ByReference {
public LoggedIn logged_in; //在处理登录并成功时调用
public LoggedOut logged_out; //当注销被处理时调用。如果初始化注销操作,则显式调用;如果存在永久连接错误,则隐式调用。
public Callback metadata_updated; //每当元数据更新时调用。如果您的元数据在libspotify之外缓存,您应该清除缓存并获取新版本。
public ConnectionError connection_error; //在出现连接错误时调用,并且库在重新连接到Spotify服务时出现问题。可以多次调用(只要存在问题)
public Callback message_to_user; //当访问点要向用户显示消息时调用。在桌面客户端中,这些都显示在搜索框下方的蓝色工具栏中。
public Callback notify_main_thread; //当需要在主线程上进行处理时调用。你需要在主线程中调用sp_session_process_events()来获得libspotify做更多的工作。否则可能会导致请求超时或丢失连接。
public Callback music_delivery; //当有解压缩的音频数据可用时调用。
public Callback play_token_lost; //音乐已暂停,因为只有一个帐户可能会同时播放音乐。
public Callback log_message; //记录回调
public Callback end_of_track; //轨道结束。当当前播放的曲目到达结束时调用。
public Callback streaming_error; //流式传输错误。在流式传输时无法启动或继续时调用。
public Callback userinfo_updated; //在用户信息(与sp_user对象相关的任何内容)之后调用已更新。
public Callback start_playback; //在音频播放开始时调用。为了正常工作,应用程序还必须实现get_audio_buffer_stats()。此函数从内部会话线程调用 - 您需要有正确的同步。此函数不能阻止。
public Callback stop_playback; //在音频播放停止时调用。为了正常工作,应用程序还必须实现get_audio_buffer_stats()。此函数从内部会话线程调用 - 您需要有正确的同步。此函数不能阻止。
public Callback get_audio_buffer_stats; //调用查询应用程序有关其音频缓冲区。此函数从内部会话线程调用 - 您需要正确的同步!此函数不能阻止。
}

private static final char [] APP_KEY = {/ * Appkey here ** /;

public static void main(String [] args)throws InterruptedException {
JLibspotify lib =(JLibspotify)Native.loadLibrary(spotify,JLibspotify.class)
sp_session_config cfg = new sp_session_config();
Pointer ptr = new Memory(APP_KEY.length);
ptr.write(0,toBytes(APP_KEY),0,APP_KEY.length);
cfg.application_key = ptr;
cfg.application_key_size = APP_KEY.length;
sp_session_callbacks sessCallbacks = new sp_session_callbacks();
LoggedIn loggedInCallback = new LoggedIn(){
public void logged_in(sp_session session,int error){
System.out.println(logged_in()called);
}

};
ConnectionError connectionErrorCallback = new ConnectionError(){

public void connection_error(sp_session session,int error){
System.out.println(connection_error()called);
}
};
LoggedOut loggedOutCallback = new LoggedOut(){

public void logged_out(sp_session session,int error){
System.out.println(logged_out()called);
}
};

sessCallbacks.logged_in = loggedInCallback;
sessCallbacks.connection_error = connectionErrorCallback;
sessCallbacks.logged_out = loggedOutCallback;
sessCallbacks.log_message = new Callback(){
public void callback(sp_session session,String message){
System.out.println(log_message()called:+ message);
}
};
cfg.callbacks = sessCallbacks;
PointerByReference sessionPbr = new PointerByReference();

int error_id = lib.sp_session_create(cfg,sessionPbr); // CRASHES HERE
System.out.println(sp_session_create returned+ error_id);
//
sp_session mySession = new sp_session(sessionPbr.getValue());
error_id = lib.sp_session_login(mySession,foo,bar);
System.out.println(sp_session_login returned+ error_id);
Thread.sleep(1000);
}

public static byte [] toBytes(char [] key){
byte [] b = new byte [key.length];
for(int i = 0; i if(key [i]> 127){
b [i] =(byte [ i] -256);
} else {
b [i] =(byte)key [i];
}
}
return b;
}
}

这里是一个C程序,更少的回调 - 你需要添加更多)。它是为OS X所以你可能需要改变 #include 。由于我在澳大利亚无法获得spotify,我无法测试它是否一个成功的登录命中一个回调,但日志回调工作。

  #include< stdio.h> 
#include< libspotify / api.h>

void SP_CALLCONV log_message(sp_session *session, const char *data) {
fprintf(stderr,\"log_message: %s\n\", data);
fflush(stderr);
}

void SP_CALLCONV connection_error(sp_session *session, sp_error error) {
fprintf(stderr,\"connection_error: %d\n\", error);
fflush(stderr);
}

int main(int argc, char** argv) {

static byte APP_KEY[] = {
/*the API key */};
static sp_session_callbacks callbacks;
callbacks.log_message = log_message;
callbacks.connection_error = connection_error;
static sp_session_config cfg;
cfg.callbacks = &callbacks;
cfg.api_version = 7;
cfg.cache_location = \".\";
cfg.settings_location = \".\";
cfg.user_agent = \"jspotify\";
cfg.application_key = APP_KEY;
cfg.application_key_size = sizeof(APP_KEY);

sp_session* mySession;
int code = sp_session_create(&cfg, &mySession);
printf(\"sp_session_create returned %d\n\", code);
sp_session_login(mySession, \"foo\", \"bar\");
printf(\"sp_session_login returned %d\n\", code);
sleep(10);
}

If the C program calls callbacks which the Java version doesn’t, then perhaps there’s something wrong with the use of jna -- if not, perhaps there’s something more to understand about spotify and when it calls callbacks. I agree that the docs seem to say that a successful login will call a callback, but perhaps they’re out of date?


I'm having problems handling callbacks in JNA.

I'm trying to use a C API that uses callbacks to handle several session events (logged in, logged out, connection problem...).
The session object (called sp_session) is an opaque struct. All the callbacks are registered in a sp_session_callbacks structure. According to the API, I am supposed to declare the callbacks object, and put it into a Config object that I will provide when creating the sp_session object. If I don't want to use certain callbacks, I am supposed to initialize them with null. The API is using the __stdcall calling convention.

Here is a snippet of the C header that's relevant to my problem:

#define SP_CALLCONV __stdcall

typedef struct sp_session sp_session; ///< Representation of a session

typedef enum sp_error {
    SP_ERROR_OK                        = 0,  
    SP_ERROR_BAD_API_VERSION           = 1,
        /* More errors */
} sp_error;

typedef struct sp_session_callbacks {
    /**
     * Called when login has been processed and was successful
     */
    void (SP_CALLCONV *logged_in)(sp_session *session, sp_error error);

    /**
     * Called when logout has been processed. Either called explicitly
     * if you initialize a logout operation, or implicitly if there
     * is a permanent connection error
     *
     * @param[in]  session    Session
     */
    void (SP_CALLCONV *logged_out)(sp_session *session);

    /**
     * Called when there is a connection error, and the library has problems
     * reconnecting to the Spotify service. Could be called multiple times (as
     * long as the problem is present) 
     */
    void (SP_CALLCONV *connection_error)(sp_session *session, sp_error error);

    /* More callbacks */
} sp_session_callbacks;

/**
 * Initialize a session. The session returned will be initialized, but you will need
 * to log in before you can perform any other operation
 */
SP_LIBEXPORT(sp_error) sp_session_create(const sp_session_config *config, sp_session **sess);

Here is my equivalent JNA code:

The sp_session object

public class sp_session extends PointerType{

    public sp_session(Pointer address) {
        super(address);
    }
    public sp_session() {
        super();
    }
}

The sp_session_callbacks object, containing all the callbacks

public class sp_session_callbacks extends Structure{
    public LoggedIn logged_in;
    public LoggedOut logged_out;
    public ConnectionError connection_error;
}

The callbacks object (here is LoggedIn, but of course I have one for each callback)

public interface LoggedIn extends StdCallCallback {
    public void logged_in(sp_session session, int error);
}

The native library, with the declaration of all the methods

public interface JLibspotify extends StdCallLibrary{
    int sessionCreate(sp_session_config config, PointerByReference sess);
    int sessionLogin(sp_session session, String username, String password);
    // All the other methods defined by the API
}

And my main class, binding it all together

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

    public static void main(String[] args){
        JLibspotify lib = (JLibspotify)Native.loadLibrary("libspotify", JLibspotify.class);
        sp_session_config cfg = new sp_session_config();
        }
        sp_session_callbacks sessCallbacks = new sp_session_callbacks();
        LoggedIn loggedInCallback = new LoggedIn(){
        public void logged_in(sp_session session, int error) {
             System.out.println("logged_in() called");
            }

        };
        sessCallbacks.logged_in = loggedInCallback;

        cfg.session_callbacks = sessCallbacks;

        PointerByReference sessionPbr = new PointerByReference();

        int error_id = sessionCreate(cfg, sessionPbr); // CRASHES HERE

        sp_session mySession = new sp_session(sessionPbr.getValue());
    }
}

So, the sessionCreate function call makes the JRE crash with the trace at the end of the post EXCEPTION_ACCESS_VIOLATION (0xc0000005) problematic frame: C [jna3666290841889849729.dll+0xa3f4].

It looks like the logged_in callback is causing this, because when I set it to null it runs ok. Plus, if I initialize the connection_error callback, that has the exact same signature, it doesn't crash either.

I'm running version 3.2.7 of JNA. I tried with an anterior version (3.0.9) and it also failed. I'm running the JDK 1.7 beta version, but I tried with the 1.6 and it also failed.

Thank you!

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0499a3f4, pid=1368, tid=1344
#
# JRE version: 7.0-b129
# Java VM: Java HotSpot(TM) Client VM (21.0-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [jna3666290841889849729.dll+0xa3f4]
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

---------------  T H R E A D  ---------------

Current thread (0x01b79400):  JavaThread "main" [_thread_in_native, id=1344, stack(0x00340000,0x00390000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x1993053a

Registers:
EAX=0x19930522, EBX=0x100dc77c, ECX=0x00010004, EDX=0x1008d3e0
ESP=0x0038f58c, EBP=0x0038f5b4, ESI=0x0038f5a4, EDI=0x100b79b0
EIP=0x0499a3f4, EFLAGS=0x00210212

Top of Stack: (sp=0x0038f58c)
0x0038f58c:   100dc77c 0038f5a4 00010004 0038f688
0x0038f59c:   05983de0 05981330 0598289c 05983de0
0x0038f5ac:   05983de0 00000014 0038f688 1008d3ea
0x0038f5bc:   05983de0 10030d77 0038fc44 100b79b0
0x0038f5cc:   ffffffff 1008d334 00000000 05983de0
0x0038f5dc:   1008d3e0 05983fc4 1008d9fd 0038f770
0x0038f5ec:   00000000 00000000 7275016a 055310b0
0x0038f5fc:   00000000 00010001 00000000 00000000 

Instructions: (pc=0x0499a3f4)
0x0499a3d4:   01 00 89 e5 57 56 8d 75 f0 53 83 ec 1c 8b 7d 14
0x0499a3e4:   8b 5f 4c 8b 03 89 4c 24 08 89 74 24 04 89 1c 24
0x0499a3f4:   ff 50 18 83 ec 0c 85 c0 0f 94 c0 0f b6 c0 85 c0
0x0499a404:   89 45 ec 75 19 8b 03 31 d2 89 54 24 08 89 74 24 


Register to memory mapping:

EAX=0x19930522 is an unknown value
EBX=0x100dc77c is an unknown value
ECX=0x00010004 is an unknown value
EDX=0x1008d3e0 is an unknown value
ESP=0x0038f58c is pointing into the stack for thread: 0x01b79400
EBP=0x0038f5b4 is pointing into the stack for thread: 0x01b79400
ESI=0x0038f5a4 is pointing into the stack for thread: 0x01b79400
EDI=0x100b79b0 is an unknown value


Stack: [0x00340000,0x00390000],  sp=0x0038f58c,  free space=317k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [jna3666290841889849729.dll+0xa3f4]  Java_com_sun_jna_Native_initialize_1ffi_1type+0x1054
C  [libspotify.dll+0x8d3ea]  sp_error_message+0x35a
C  [jna3666290841889849729.dll+0xcb77]  Java_com_sun_jna_Native_initialize_1ffi_1type+0x37d7
C  [jna3666290841889849729.dll+0xc7c2]  Java_com_sun_jna_Native_initialize_1ffi_1type+0x3422
C  [jna3666290841889849729.dll+0x4561]  Java_com_sun_jna_Pointer__1getString+0xa31
C  [jna3666290841889849729.dll+0x4d2e]  Java_com_sun_jna_Function_invokeInt+0x2e
j  com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341
j  $Proxy0.sp_session_create(Lcom/nbarraille/jspotify/model/sp_session_config;Lcom/sun/jna/ptr/PointerByReference;)I+20
j  com.nbarraille.jspotify.main.Test.main([Ljava/lang/String;)V+273
v  ~StubRoutines::call_stub
V  [jvm.dll+0x115f6d]
V  [jvm.dll+0x1b788e]
V  [jvm.dll+0x115fed]
V  [jvm.dll+0xa2507]
V  [jvm.dll+0xac867]
C  [javaw.exe+0x209e]
C  [javaw.exe+0xa23b]
C  [javaw.exe+0xa2c5]
C  [kernel32.dll+0x51194]  BaseThreadInitThunk+0x12
C  [ntdll.dll+0x5b429]  RtlInitializeExceptionChain+0x63
C  [ntdll.dll+0x5b3fc]  RtlInitializeExceptionChain+0x36

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+315
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341
j  $Proxy0.sp_session_create(Lcom/nbarraille/jspotify/model/sp_session_config;Lcom/sun/jna/ptr/PointerByReference;)I+20
j  com.nbarraille.jspotify.main.Test.main([Ljava/lang/String;)V+273
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x0182fc00 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=4664, stack(0x04170000,0x041c0000)]
  0x0182ac00 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=1728, stack(0x01b20000,0x01b70000)]
  0x01829800 JavaThread "Attach Listener" daemon [_thread_blocked, id=112, stack(0x04020000,0x04070000)]
  0x01826400 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=5836, stack(0x03ea0000,0x03ef0000)]
  0x01819800 JavaThread "Finalizer" daemon [_thread_blocked, id=4724, stack(0x03f80000,0x03fd0000)]
  0x01817800 JavaThread "Reference Handler" daemon [_thread_blocked, id=3940, stack(0x01ad0000,0x01b20000)]
=>0x01b79400 JavaThread "main" [_thread_in_native, id=1344, stack(0x00340000,0x00390000)]

Other Threads:
  0x01816400 VMThread [stack: 0x01a30000,0x01a80000] [id=3876]
  0x01843000 WatcherThread [stack: 0x040d0000,0x04120000] [id=4636]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 4928K, used 3630K [0x23450000, 0x239a0000, 0x289a0000)
  eden space 4416K,  82% used [0x23450000, 0x237db858, 0x238a0000)
  from space 512K,   0% used [0x238a0000, 0x238a0000, 0x23920000)
  to   space 512K,   0% used [0x23920000, 0x23920000, 0x239a0000)
 tenured generation   total 10944K, used 0K [0x289a0000, 0x29450000, 0x33450000)
   the space 10944K,   0% used [0x289a0000, 0x289a0000, 0x289a0200, 0x29450000)
 compacting perm gen  total 12288K, used 860K [0x33450000, 0x34050000, 0x37450000)
   the space 12288K,   7% used [0x33450000, 0x33527190, 0x33527200, 0x34050000)
    ro space 10240K,  43% used [0x37450000, 0x3789ce40, 0x3789d000, 0x37e50000)
    rw space 12288K,  53% used [0x37e50000, 0x384c2710, 0x384c2800, 0x38a50000)

Code Cache  [0x01e90000, 0x01f20000, 0x03e90000)
 total_blobs=234 nmethods=82 adapters=88 free_code_cache=32972224 largest_free_block=0

Dynamic libraries:
0x00880000 - 0x008b0000     C:\Program Files\Java\jdk1.7.0\bin\javaw.exe
0x778f0000 - 0x77a2d000     C:\Windows\SYSTEM32\ntdll.dll
0x77070000 - 0x77144000     C:\Windows\system32\kernel32.dll
0x75cf0000 - 0x75d3a000     C:\Windows\system32\KERNELBASE.dll
0x60000000 - 0x60041000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\midas32.dll
0x61000000 - 0x61028000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_base.m32
0x67000000 - 0x67048000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_nt.m32
0x64000000 - 0x64021000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_registry.m32
0x62000000 - 0x6202d000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_extra.m32
0x65000000 - 0x6501a000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_net.m32
0x63000000 - 0x630a6000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_fragments.m32
0x75e10000 - 0x75eb0000     C:\Windows\system32\ADVAPI32.dll
0x775c0000 - 0x7766c000     C:\Windows\system32\msvcrt.dll
0x75d50000 - 0x75d69000     C:\Windows\SYSTEM32\sechost.dll
0x77670000 - 0x77711000     C:\Windows\system32\RPCRT4.dll
0x76f00000 - 0x76fc9000     C:\Windows\system32\USER32.dll
0x77a50000 - 0x77a9e000     C:\Windows\system32\GDI32.dll
0x77a30000 - 0x77a3a000     C:\Windows\system32\LPK.dll
0x75d70000 - 0x75e0d000     C:\Windows\system32\USP10.dll
0x74830000 - 0x749ce000     C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7600.16661_none_420fe3fa2b8113bd\COMCTL32.dll
0x772f0000 - 0x77347000     C:\Windows\system32\SHLWAPI.dll
0x775a0000 - 0x775bf000     C:\Windows\system32\IMM32.DLL
0x75eb0000 - 0x75f7c000     C:\Windows\system32\MSCTF.dll
0x72740000 - 0x727fe000     C:\Program Files\Java\jdk1.7.0\jre\bin\msvcr100.dll
0x01b80000 - 0x01e89000     C:\Program Files\Java\jdk1.7.0\jre\bin\client\jvm.dll
0x746c0000 - 0x746f2000     C:\Windows\system32\WINMM.dll
0x73920000 - 0x7392c000     C:\Program Files\Java\jdk1.7.0\jre\bin\verify.dll
0x73330000 - 0x73350000     C:\Program Files\Java\jdk1.7.0\jre\bin\java.dll
0x75d40000 - 0x75d45000     C:\Windows\system32\PSAPI.DLL
0x733a0000 - 0x733b3000     C:\Program Files\Java\jdk1.7.0\jre\bin\zip.dll
0x10000000 - 0x10259000     C:\Windows\System32\libspotify.dll
0x77150000 - 0x77185000     C:\Windows\system32\WS2_32.dll
0x77a40000 - 0x77a46000     C:\Windows\system32\NSI.dll
0x75ba0000 - 0x75cbc000     C:\Windows\system32\CRYPT32.dll
0x75ab0000 - 0x75abc000     C:\Windows\system32\MSASN1.dll
0x74ee0000 - 0x74f38000     C:\Windows\system32\WINHTTP.dll
0x74e90000 - 0x74edf000     C:\Windows\system32\webio.dll
0x754c0000 - 0x754d6000     C:\Windows\system32\CRYPTSP.dll
0x75260000 - 0x7529b000     C:\Windows\system32\rsaenh.dll
0x750a0000 - 0x750b7000     C:\Windows\system32\USERENV.dll
0x75a40000 - 0x75a4b000     C:\Windows\system32\profapi.dll
0x75a30000 - 0x75a3c000     C:\Windows\system32\CRYPTBASE.dll
0x728a0000 - 0x728b6000     C:\Program Files\Java\jdk1.7.0\jre\bin\net.dll
0x75480000 - 0x754bc000     C:\Windows\system32\mswsock.dll
0x75470000 - 0x75476000     C:\Windows\System32\wship6.dll
0x73950000 - 0x73960000     C:\Windows\system32\NLAapi.dll
0x75340000 - 0x75384000     C:\Windows\system32\DNSAPI.dll
0x71030000 - 0x71038000     C:\Windows\System32\winrnr.dll
0x71020000 - 0x71030000     C:\Windows\system32\napinsp.dll
0x71000000 - 0x71012000     C:\Windows\system32\pnrpnsp.dll
0x74fd0000 - 0x74fd5000     C:\Windows\System32\wshtcpip.dll
0x74d30000 - 0x74d4c000     C:\Windows\system32\IPHLPAPI.DLL
0x74d20000 - 0x74d27000     C:\Windows\system32\WINNSI.DLL
0x70c60000 - 0x70c66000     C:\Windows\system32\rasadhlp.dll
0x71ba0000 - 0x71bd8000     C:\Windows\System32\fwpuclnt.dll
0x73930000 - 0x7393f000     C:\Program Files\Java\jdk1.7.0\jre\bin\nio.dll
0x04990000 - 0x049e5000     C:\Users\nbarraille\AppData\Local\Temp\jna3666290841889849729.dll

VM Arguments:
jvm_args: -Djava.library.path=C:\Windows\System32 -Dfile.encoding=Cp1252 
java_command: com.nbarraille.jspotify.main.Test
Launcher Type: SUN_STANDARD

Environment Variables:
PATH=C:/Program Files/Java/jdk1.7.0/bin/../jre/bin/client;C:/Program Files/Java/jdk1.7.0/bin/../jre/bin;C:/Program Files/Java/jdk1.7.0/bin/../jre/lib/i386;C:\Windows\System32
USERNAME=nbarraille
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 23 Stepping 10, GenuineIntel



---------------  S Y S T E M  ---------------

OS: Windows 7 Build 7600 

CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 23 stepping 10, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1

Memory: 4k page, physical 3624108k(885020k free), swap 7246452k(3637664k free)

vm_info: Java HotSpot(TM) Client VM (21.0-b01) for windows-x86 JRE (1.7.0-ea-b129), built on Feb 10 2011 07:43:39 by "java_re" with unknown MS VC++:1600

time: Mon Mar 07 14:25:08 2011
elapsed time: 0 seconds

EDIT: *Code to test the API in C*

#include "api.h"


/* --- Data --- */
const uint8_t g_appkey[] = {/*My appkey*/};


/* ---------------------------  SESSION CALLBACKS  ------------------------- */
static void logged_in(sp_session *sess, sp_error error){
    printf("logged_in() called: \n");
}

static void log_message(sp_session *sess, const char *data){
    printf("log_message() called %s : \n", data);
}

static void notify_main_thread(sp_session *sess){
    printf("notify_main_thread() called \n");
}

static sp_session_callbacks session_callbacks = {
    //.logged_in = (void*)&logged_in,
    //.notify_main_thread = (void*)&notify_main_thread,
    //.log_message = (void*)&log_message,
};

static sp_session_config spconfig = {
    .api_version = 7,
    .cache_location = "tmp",
    .settings_location = "tmp",
    .application_key = g_appkey,
    .application_key_size = sizeof(g_appkey),
    .user_agent = "jspotify",
    .callbacks = &session_callbacks,
    .userdata = NULL
};

int main(int argc, char **argv)
{
    sp_session *sp;

    const char *username = "foo";
    const char *password = "bar";

    spconfig.application_key_size = sizeof(g_appkey);


    printf("Creating session \n");

    sp_error err = sp_session_create(&spconfig, &sp);

    if(err != 0){
        printf("Error occured: %d \n", err);
        return 0;
    }

    printf("Login\n");
    sp_session_login(sp, username, password);

    Sleep(10000);
    return 0;
}

When I run it like this (without callbacks), the output is:

Creating session 
Login

And when I register the callbacks (uncomment the lines), it doesn't even print anything!

It looks like it doesn't print anything when a callback is called, because if I comment the sp_session_login line, only the declaration of notify_main_thread (which is the only one printed) will prevent the program from printing...

解决方案

When you have a C structure like:

struct Foo {
   Bar* bar
}

i.e. one which contains a pointer to another structure, your JNA implementation of Bar (class Bar extends Structure) must also implement the Structure.ByReference interface -- otherwise JNA will think that struct Foo contains an instance of struct Bar instead of a pointer to a struct Bar, and an illegal memory access will result as the C code interprets a value in the Bar instance as a pointer.

You need to add implements Structure.ByReference to the sp_session_callbacks class.

Thanks for inspiring me to look at JNA -- it's pretty cool!

Output of this code is:

sp_session_create returned 0
sp_session_login returned 0
log_message() called:14:16:53.825 I [ap:1388] Connecting to AP ap.spotify.com:4070

log_message() called:14:16:54.061 I [ap:938] Connected to AP: 193.182.8.11:4070

log_message() called:14:16:54.765 E [ap:3396] Connection error:  401

Process finished with exit code 0

import com.sun.jna.*;
import com.sun.jna.ptr.PointerByReference;

import java.sql.Connection;

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

    public interface JLibspotify extends Library {
        int sp_session_create(sp_session_config config, PointerByReference sess);

        int sp_session_login(sp_session session, String username, String password);
        // All the other methods defined by the API
    }

    public static class sp_session extends PointerType {

        public sp_session(Pointer address) {
            super(address);
        }

        public sp_session() {
            super();
        }
    }

    public static class sp_session_config extends Structure {
        public int api_version = 7; // The version of the Spotify API your application is compiled with.
        public String cache_location = ".";
        public String settings_location = ".";
        public Pointer application_key; // Your application key.
        public int application_key_size; // The size of the application key in bytes
        public String user_agent = "jspotify";
        public sp_session_callbacks callbacks; // Delivery callbacks for session events. NULL if not interested in any callbacks
        public Pointer userdata; // User supplied data for your application
        public boolean compress_playlists;
        public boolean dont_save_metadata_for_playlists;
        public boolean initially_unload_playlists;
    }

    public interface LoggedIn extends Callback {
        public void logged_in(sp_session session, int error);
    }

    public interface LoggedOut extends Callback {
        public void logged_out(sp_session session, int error);
    }

    public interface ConnectionError extends Callback {
        public void connection_error(sp_session session, int error);
    }

    public static class sp_session_callbacks extends Structure implements Structure.ByReference{
    public LoggedIn logged_in; // Called when login has been processed and was successful
    public LoggedOut logged_out; // Called when logout has been processded. Either called explicitly if you initialize a logout operation, or implicitly if there is a permanent connection error.
    public Callback metadata_updated; // Called whenever metadata has been updated. If you have metadata cached outside of libspotify, you should purge your caches and fetch new versions.
    public ConnectionError connection_error; // Called when there is a connection error, and the library has problems reconnecting to the Spotify service. Could be called multiple times (as long as the problem is present)
    public Callback message_to_user; // Called when the acces point wants to display a message to the user. In the desktop client, these are shown in a blueish toolbar just below the search box.
    public Callback notify_main_thread; // Called when processing needs to take place on the main thread. You need to call sp_session_process_events() in the main thread to get libspotify to do more work. Failure to do so may cause request timeouts, or a lost connections.
    public Callback music_delivery; // Called when there is decompressed audio data available.
    public Callback play_token_lost; // Music has been paused because only one account may play music at the same time.
    public Callback log_message; // Logging callback
    public Callback end_of_track; // End of track. Called when the currently played track has reached its end.
    public Callback streaming_error; // Streaming error. Called when streaming cannot start or continue.
    public Callback userinfo_updated; // Called after user info (anything related to sp_user objects) have been updated.
    public Callback start_playback; // Called when audio playback should start. For this to work correctly the application must also implement get_audio_buffer_stats(). This function is called from an internal session thread - you need to have proper synchronization. This function must never block.
    public Callback stop_playback; // Called when audio playback should stop. For this to work correctly the application must also implement get_audio_buffer_stats(). This function is called from an internal session thread - you need to have proper synchronization. This function must never block.
    public Callback get_audio_buffer_stats; // Called to query application about its audio buffer. This function is called from an internal session thread - you need to have proper synchronization! This function must never block.
}

    private static final char[] APP_KEY ={/* Appkey here**/;

    public static void main(String[] args) throws InterruptedException {
        JLibspotify lib = (JLibspotify) Native.loadLibrary("spotify", JLibspotify.class);
        sp_session_config cfg = new sp_session_config();
        Pointer ptr = new Memory(APP_KEY.length);
        ptr.write(0, toBytes(APP_KEY), 0, APP_KEY.length);
        cfg.application_key = ptr;
        cfg.application_key_size = APP_KEY.length;
        sp_session_callbacks sessCallbacks = new sp_session_callbacks();
        LoggedIn loggedInCallback = new LoggedIn() {
            public void logged_in(sp_session session, int error) {
                System.out.println("logged_in() called");
            }

        };
        ConnectionError connectionErrorCallback = new ConnectionError() {

            public void connection_error(sp_session session, int error) {
                System.out.println("connection_error() called");
            }
        };
        LoggedOut loggedOutCallback = new LoggedOut() {

            public void logged_out(sp_session session, int error) {
                System.out.println("logged_out() called");
            }
        };

        sessCallbacks.logged_in = loggedInCallback;
        sessCallbacks.connection_error = connectionErrorCallback;
        sessCallbacks.logged_out = loggedOutCallback;
        sessCallbacks.log_message = new Callback() {
            public void callback(sp_session session, String message) {
                System.out.println("log_message() called:" + message);
            }
        };
        cfg.callbacks = sessCallbacks;
        PointerByReference sessionPbr = new PointerByReference();

        int error_id = lib.sp_session_create(cfg, sessionPbr); // CRASHES HERE
        System.out.println("sp_session_create returned " + error_id);
//
        sp_session mySession = new sp_session(sessionPbr.getValue());
        error_id = lib.sp_session_login(mySession, "foo", "bar");
        System.out.println("sp_session_login returned " + error_id);
        Thread.sleep(1000);
    }

    public static byte[] toBytes(char[] key){
        byte[] b = new byte[key.length];
        for(int i =0; i < key.length; i++){
            if(key[i] > 127){
                b[i] = (byte)(key[i] - 256);
            }else{
                b[i] = (byte)key[i];
            }
        }
        return b;
    }
}

Here's a C program which does the same thing (with fewer callbacks -- you'll need to add more). It's for OS X so you may need to change the #include. As I can't get spotify in Australia I can't test it to see whether a successful login hits a callback, but the logging callback works.

#include <stdio.h>
#include <libspotify/api.h>

void SP_CALLCONV log_message(sp_session *session, const char *data) {
        fprintf(stderr,"log_message: %s\n", data);
        fflush(stderr);
}

void SP_CALLCONV connection_error(sp_session *session, sp_error error) {
        fprintf(stderr,"connection_error: %d\n", error);
        fflush(stderr);
}

int main(int argc, char** argv) {

        static byte APP_KEY[] = {
                /*the API key */};
        static sp_session_callbacks callbacks;
        callbacks.log_message = log_message;
        callbacks.connection_error = connection_error;
        static sp_session_config cfg;
        cfg.callbacks = &callbacks;
        cfg.api_version = 7;
        cfg.cache_location = ".";
        cfg.settings_location = ".";
        cfg.user_agent = "jspotify";
        cfg.application_key = APP_KEY;
        cfg.application_key_size = sizeof(APP_KEY);

        sp_session* mySession;
        int code = sp_session_create(&cfg, &mySession);
        printf("sp_session_create returned %d\n", code);
        sp_session_login(mySession, "foo", "bar");
        printf("sp_session_login returned %d\n", code);
        sleep(10);
}

If the C program calls callbacks which the Java version doesn't, then perhaps there's something wrong with the use of jna -- if not, perhaps there's something more to understand about spotify and when it calls callbacks. I agree that the docs seem to say that a successful login will call a callback, but perhaps they're out of date?

这篇关于C回调与JNA使JRE崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆