jni 将字符串转换为字符数组

发布于 2025-01-03 21:40:10 字数 461 浏览 1 评论 0原文

对c++不熟悉,有人可以帮我将cmd添加到myStr数组并将其传递给main()函数,这是我的到目前为止:

JNIEXPORT void JNICALL Java_my_package_JNIActivity_callCmdLine(
    JNIEnv *env, jobject obj, jstring cmd)
{
    const char *nativeString = env->GetStringUTFChars(cmd, 0);
    env->ReleaseStringUTFChars(cmd, nativeString);

    char * myStr [] = {"v", nativeString};

    //int main(int argc, char *argv[])
    main(1, myStr); 
}

Not familiar with c++, can someone help me add cmd to the myStr array and pass it to the main() function, here is what I have so far:

JNIEXPORT void JNICALL Java_my_package_JNIActivity_callCmdLine(
    JNIEnv *env, jobject obj, jstring cmd)
{
    const char *nativeString = env->GetStringUTFChars(cmd, 0);
    env->ReleaseStringUTFChars(cmd, nativeString);

    char * myStr [] = {"v", nativeString};

    //int main(int argc, char *argv[])
    main(1, myStr); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2025-01-10 21:40:10

好吧,在完成之前不要释放它。

char * nativeString;

{    const char * _nativeString = env->GetStringUTFChars(cmd, 0);
     nativeString = strdup (_nativeString);
     env->ReleaseStringUTFChars(cmd, _nativeString);
}

char * myStr [] = {"v", nativeString};
main(1, myStr); 

free (nativeString);

Well, don't release it before you're finished with it.

char * nativeString;

{    const char * _nativeString = env->GetStringUTFChars(cmd, 0);
     nativeString = strdup (_nativeString);
     env->ReleaseStringUTFChars(cmd, _nativeString);
}

char * myStr [] = {"v", nativeString};
main(1, myStr); 

free (nativeString);
少年亿悲伤 2025-01-10 21:40:10

为什么不利用对象来保证删除是自动完成的......?

class ConvertStringHelper
{
public:
    ConvertStringHelper( JNIEnv *env, jstring value )
    {
        m_str = env->GetStringUTFChars(value, 0);
        m_value = &value;
        m_env = env;
    }
    ~ConvertStringHelper()
    {
        m_env->ReleaseStringUTFChars( *m_value, m_str);
    }

    jstring* m_value;
    const char *m_str;
    JNIEnv *m_env;
};

然后:

    ConvertStringHelper helper( env, cmd );
    const char* nativeStr = helper.m_str;
    // nativeStr is valid in helper's scope and memory will be cleanly released when exiting the scope!

Why not taking advantage of objects to garantee deletion is done automatically...?

class ConvertStringHelper
{
public:
    ConvertStringHelper( JNIEnv *env, jstring value )
    {
        m_str = env->GetStringUTFChars(value, 0);
        m_value = &value;
        m_env = env;
    }
    ~ConvertStringHelper()
    {
        m_env->ReleaseStringUTFChars( *m_value, m_str);
    }

    jstring* m_value;
    const char *m_str;
    JNIEnv *m_env;
};

Then:

    ConvertStringHelper helper( env, cmd );
    const char* nativeStr = helper.m_str;
    // nativeStr is valid in helper's scope and memory will be cleanly released when exiting the scope!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文