From 58cc09165730728dd0cb70e265c1678dc36abb2e Mon Sep 17 00:00:00 2001 From: Andrei Mikhalev Date: Tue, 9 Jul 2024 15:49:09 +0300 Subject: [PATCH] Explicitly cast the callback and userData pointers to jlong to safely pass them to the Java code: - on win32 pointers are 4 bytes - reinterpret_cast win32_ptr to jlong type allows us to safely pass it from native -> Java -> to native --- src/main/c/jni-lib/src/listeners/DxEventListener.cpp | 4 +++- src/main/c/jni-lib/src/listeners/DxStateChangeListener.cpp | 4 +++- .../jni-lib/src/listeners/DxSubscriptionChangeListener.cpp | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/c/jni-lib/src/listeners/DxEventListener.cpp b/src/main/c/jni-lib/src/listeners/DxEventListener.cpp index 52dc56bf..1f12c6eb 100644 --- a/src/main/c/jni-lib/src/listeners/DxEventListener.cpp +++ b/src/main/c/jni-lib/src/listeners/DxEventListener.cpp @@ -11,7 +11,9 @@ namespace dxfeed { DxEventListener* DxEventListener::create(JNIEnv* env, dxfg_feed_event_listener_function callback, void* userData) { auto dxSubscriptionJniClass = jni::safeFindClass(env, "com/dxfeed/api/DxSubscriptionJni"); auto newEventListenerId = jni::safeGetStaticMethodID(env, dxSubscriptionJniClass, "newEventListener", "(JJ)J"); - auto result = jni::checkedCallStaticLongMethod(env, dxSubscriptionJniClass, newEventListenerId, callback, userData); + auto pCallback = dxfeed::r_cast(callback); + auto pUserData = dxfeed::r_cast(userData); + auto result = jni::checkedCallStaticLongMethod(env, dxSubscriptionJniClass, newEventListenerId, pCallback, pUserData); env->DeleteLocalRef(dxSubscriptionJniClass); return new DxEventListener(result); } diff --git a/src/main/c/jni-lib/src/listeners/DxStateChangeListener.cpp b/src/main/c/jni-lib/src/listeners/DxStateChangeListener.cpp index 39ed96c5..a1c1480d 100644 --- a/src/main/c/jni-lib/src/listeners/DxStateChangeListener.cpp +++ b/src/main/c/jni-lib/src/listeners/DxStateChangeListener.cpp @@ -14,7 +14,9 @@ namespace dxfeed { auto dxEndpointClass = jni::safeFindClass(env, "com/dxfeed/api/DxEndpointJni"); auto newStateChangeListenerId = jni::safeGetStaticMethodID(env, dxEndpointClass, "newStateChangeEventListener", "(JJ)J"); - auto result = jni::checkedCallStaticLongMethod(env, dxEndpointClass, newStateChangeListenerId, callback, userData); + auto pCallback = dxfeed::r_cast(callback); + auto pUserData = dxfeed::r_cast(userData); + auto result = jni::checkedCallStaticLongMethod(env, dxEndpointClass, newStateChangeListenerId, pCallback, pUserData); env->DeleteLocalRef(dxEndpointClass); return new DxStateChangeListener(result); } diff --git a/src/main/c/jni-lib/src/listeners/DxSubscriptionChangeListener.cpp b/src/main/c/jni-lib/src/listeners/DxSubscriptionChangeListener.cpp index 843efa81..7ab71b11 100644 --- a/src/main/c/jni-lib/src/listeners/DxSubscriptionChangeListener.cpp +++ b/src/main/c/jni-lib/src/listeners/DxSubscriptionChangeListener.cpp @@ -18,8 +18,12 @@ namespace dxfeed { auto dxEndpointClass = jni::safeFindClass(env, "com/dxfeed/api/DxSubscriptionJni"); auto newChangeListenerId = jni::safeGetStaticMethodID(env, dxEndpointClass, "newChangeListener", "(JJJJ)J"); + auto pOnSymbolsAdded = dxfeed::r_cast(fSymbolsAdded); + auto pOnSymbolsRemoved = dxfeed::r_cast(fSymbolsRemoved); + auto pOnSubscriptionClosed = dxfeed::r_cast(fSubscriptionClosed); + auto pUserData = dxfeed::r_cast(userData); auto result = jni::checkedCallStaticLongMethod(env, dxEndpointClass, newChangeListenerId, - fSymbolsAdded, fSymbolsRemoved, fSubscriptionClosed, userData); + pOnSymbolsAdded, pOnSymbolsRemoved, pOnSubscriptionClosed, pUserData); env->DeleteLocalRef(dxEndpointClass); return new DxSubscriptionChangeListener(result); }