2010-12-02 7 views
2

J'ai besoin d'appeler mon code java puis de retourner une chaîne de C++ en utilisant JNI.Comment retourner une chaîne java en C++ en utilisant JNI

Fondamentalement en C++ je veux utiliser une fonction comme "String getMyString()" pour obtenir une chaîne de Java. Comment pourrais-je l'obtenir?

+0

en double de http://stackoverflow.com/questions/4359453/calling-a-java-function-from-c-via-jni- that-returns-a-string –

Répondre

0

Suivre est mon emballage pour retourner std :: wstring. Notez qu'il est utilise "chaîne critique"

/** 
* Wraps Get/ReleaseStringCritical pairs of unicode java-string 
*/ 
struct jniCriticalString 
{ 
    jniCriticalString(JNIEnv *env, jstring str): 
     _env(env), 
     _str(str) 
    { 
     _pstr = _env->GetStringCritical(_str, &_is_copy); 
    } 
    ~jniCriticalString() 
    { 
     if(_pstr != NULL) 
     { 
      _env->ReleaseStringCritical(_str, _pstr); 
     } 
    } 
    /** 
    * True, if wrapped string is valid - e.g. call of GetStringCritical returned valid value 
    */ 
    bool is_valid() const 
    { 
     return _pstr != NULL; 
    } 
    /** True when GetStringCritical created copy instead of direct pointer */ 
    bool is_copy() const 
    { 
     return _is_copy == JNI_TRUE; 
    } 
    /** Return unicode NOT NULL TERMINATED! string.*/ 
    const wchar_t* c_str() const 
    { 
     return reinterpret_cast<const wchar_t*>(_pstr); 
    } 
    /** Get the length of internal string */ 
    jsize length() const 
    { 
     return _env->GetStringLength(_str); 
    } 
    std::wstring as_string() const 
    { 
     return std::wstring(c_str(), length()); 
    } 
    operator std::wstring() const 
    { 
     return as_string(); 
    } 

private: 
    JNIEnv *_env; 
    jstring _str; 
    jboolean _is_copy; 
    const jchar *_pstr; 
}; 

MISE À JOUR

code suivant utilise la chaîne comme paramètre d'entrée:

code JAVA:

private static native void log_message(
    String category, String message, int level); 

C++ impl:

JNIEXPORT void JNICALL Java_bla_bla_bla_bla_log_1message 
    (JNIEnv *env, jclass, jstring category, jstring message, jint level) 
{ 
    jniCriticalString pCat(env, category); 
    if(!pCat.is_valid()) 
     return; 
    jniCriticalString pMsg(env, message); 
    if(!pMsg.is_valid()) 
     return; 

    std::wstring lCat(pCat.c_str(), pCat.length()); 
    std::wstring lmessage(pMsg.c_str(), pMsg.length()); 
    OP::Logging::instance().log_message(lCat, lmessage, (OP::Logging::LogLevel)level); 
} 

méthode C++ pour revenir chaîne utilise peu une autre technique:

Java_bla_bla_bla_getName(
    JNIEnv *env, jclass operationClass, jlong handler) 
     {                          
      std::wstring retval = ... 
      //.. code to resolve std::wstring 

      return env->NewString(
       (const jchar*)retval.c_str(), 
       (jsize)retval.length()); 


     } 
+1

Pourriez-vous montrer un exemple de comment définir et initialiser un jmethodID spécifique pour cette utilisation, puis comment l'utiliser pour appeler une fonction java et ensuite retourner la chaîne? – rantravee

+0

@rantravee - juste ajouté des échantillons – Dewfy

+0

Méthode @StockB Java_bla_bla_bla_bla_log_1message avec param 'category' fais ce que tu demandes. la même technique peut être utilisée pour la valeur de retour. La signature sera 'jstring Java_bla_bla_bla_bla_log_1message' – Dewfy

Questions connexes