{"id":14621,"date":"2024-09-04T09:33:46","date_gmt":"2024-09-04T01:33:46","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621"},"modified":"2024-09-27T11:03:29","modified_gmt":"2024-09-27T03:03:29","slug":"jni-h","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621","title":{"rendered":"jni.h\uff08JNIEnv\u548cJavaVM\u7ed3\u6784\u4f53\uff09"},"content":{"rendered":"<p>\u6587\u4ef6\u4f4d\u7f6e\uff1a<code>libnativehelper\/include_jni\/jni.h<\/code><\/p>\n<p><a href=\"https:\/\/developer.aliyun.com\/article\/1063454\">https:\/\/developer.aliyun.com\/article\/1063454<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/qq_32583189\/article\/details\/53172316\">https:\/\/blog.csdn.net\/qq_32583189\/article\/details\/53172316<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/ItJavawfc\/article\/details\/81209048\">https:\/\/blog.csdn.net\/ItJavawfc\/article\/details\/81209048<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/shizhonghuo19870328\/article\/details\/52402526\">https:\/\/blog.csdn.net\/shizhonghuo19870328\/article\/details\/52402526<\/a><\/p>\n<blockquote>\n<p>JavaVM\u4e0eJNIEnv\u7684\u5c42\u7ea7\u5173\u7cfb<\/p>\n<\/blockquote>\n<p>JavaVM\u662f\u865a\u62df\u673a\u5728JNI\u5c42\u7684\u4ee3\u8868\uff0c\u6bcf\u4e00\u4e2a\u865a\u62df\u673a\u8fdb\u7a0b\u53ea\u6709\u4e00\u4e2ajavaVM\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c \u5bf9\u4e8e\u6211\u4eec\u7684JNI \u6765\u8bf4\uff0c JavaVM \u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u3002<br \/>\nJNIEnv\u662f\u4e0e\u7ebf\u7a0b\u6709\u5173\u7684\u91cf\uff0c\u4e0d\u540c\u7ebf\u7a0b\u7684JNIEnv\u5f7c\u6b64\u72ec\u7acb\u3002<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621\/#%E4%B8%80%E3%80%81JNIEnv\" title=\"\u4e00\u3001JNIEnv\">\u4e00\u3001JNIEnv<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621\/#_JNIEnv\" title=\"_JNIEnv\">_JNIEnv<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621\/#JNINativeInterface\" title=\"JNINativeInterface\">JNINativeInterface<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621\/#%E4%BA%8C%E3%80%81JavaVM\" title=\"\u4e8c\u3001JavaVM\">\u4e8c\u3001JavaVM<\/a><ul class=\"ez-toc-list-level-2\"><li class=\"ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621\/#JNIInvokeInterface\" title=\"JNIInvokeInterface\">JNIInvokeInterface<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81JNIEnv\"><\/span>\u4e00\u3001JNIEnv<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre><code class=\"language-cpp\"> 139 #if defined(__cplusplus)\n 140 typedef _JNIEnv JNIEnv;\n 141 typedef _JavaVM JavaVM;\n 142 #else\n 143 typedef const struct JNINativeInterface* JNIEnv;\n 144 typedef const struct JNIInvokeInterface* JavaVM;\n 145 #endif<\/code><\/pre>\n<p>\u5bf9\u4e8ec++\u7f16\u7a0b\u8bed\u8a00\uff0cJNIEnv\u7684\u771f\u8eab\u662f_JNIEnv\u7ed3\u6784\u4f53\uff1b\u5bf9\u4e8ec\u8bed\u8a00\uff0c\u53ef\u77e5JNIEnv\u7684\u771f\u8eab\u662fJNINativeInterface\u6307\u9488\u3002<br \/>\n\u6240\u4ee5\u5e94\u7528\u5c42\u5728\u8c03\u7528jni\u51fd\u6570\u65f6\uff0c\u4f1a\u6709\u533a\u522b\uff1a<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=193\">http:\/\/xinyiworld.top\/wordpress_it\/?p=193<\/a><\/p>\n<p>\u6240\u4ee5\uff0cJavaVM \u548cJNIEnv \u7684\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5bf9\u4e8eC<\/li>\n<\/ol>\n<pre><code class=\"language-cpp\">(*env)-&gt;\u65b9\u6cd5\u540d(env,\u53c2\u6570\u5217\u8868)\n\n(*vm)-&gt;\u65b9\u6cd5\u540d(vm,\u53c2\u6570\u5217\u8868)<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5bf9\u4e8eC++<\/li>\n<\/ol>\n<pre><code class=\"language-cpp\">env-&gt;\u65b9\u6cd5\u540d(\u53c2\u6570\u5217\u8868)\n\nvm-&gt;\u65b9\u6cd5\u540d(\u53c2\u6570\u5217\u8868)<\/code><\/pre>\n<p>C++\u4e2d\u5b9a\u4e49\u4e86<strong>cplusplus\uff0cC\u8bed\u8a00\u4e2d\u6ca1\u6709\u8be5\u5b9a\u4e49\u3002\u5373\uff1a\u7528<\/strong>cplusplus\u6765\u8bc6\u522b\u662fC\u4ee3\u7801\u8fd8\u662fC++\u4ee3\u7801\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"_JNIEnv\"><\/span>_JNIEnv<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre><code class=\"language-cpp\">struct _JNIEnv {\n    \/* do not rename this; it does not seem to be entirely opaque *\/\n    const struct JNINativeInterface* functions;\n\n#if defined(__cplusplus)\n\n#endif \/*__cplusplus*\/\n};<\/code><\/pre>\n<p>\uff01\uff01<strong>_JNIEnv\u5176\u5b9e\u662f\u5bf9JNINativeInterface\u4f5c\u4e86\u4e00\u5c42\u5c01\u88c5\u800c\u5df2<\/strong>\uff01\uff01<\/p>\n<h1><span class=\"ez-toc-section\" id=\"JNINativeInterface\"><\/span>JNINativeInterface<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li>\u6a21\u62dfJNIEnv\u7684\u5b9e\u73b0<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.jianshu.com\/p\/30cdd4486396\">https:\/\/www.jianshu.com\/p\/30cdd4486396<\/a><\/p>\n<pre><code class=\"language-cpp\">#include&lt;stdio.h&gt;\n#include&lt;stdlib.h&gt;\n\ntypedef const struct JNINativeInterface_* JNIEnv;\ntypedef char* jstring;\/\/\u6a21\u62df\u5b9a\u4e49jstring\n\nstruct JNINativeInterface_\n{\n    char* (* NewStringUTF)\n        (JNIEnv* env, const char* utf);\n};\n\nchar* NewStringUTF(JNIEnv* env, const char* utf) {\n    return utf;\n}\n\n\/\/\u6a21\u62dfJava\u7684Native\u65b9\u6cd5\njstring Java_JniDemo_getString(JNIEnv* env) {\n    return (*env)-&gt;NewStringUTF(env,&quot;\u6a21\u62dfJNIEnv&quot;);\n}\n\nvoid main() {\n    struct JNINativeInterface_ jni_interface;\n    jni_interface.NewStringUTF = NewStringUTF;\n\n    JNIEnv jni_env = &amp;jni_interface;\n\n    JNIEnv* env = &amp;jni_env;\n\n    jstring j_string = Java_JniDemo_getString(env);\n\n    printf(&quot;%s\\n&quot;,j_string);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<ul>\n<li>\u51fd\u6570\u5177\u4f53\u5b9e\u73b0<br \/>\n\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u89c1\uff1a<code>\/art\/runtime\/jni_internal.cc<\/code>,<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14635\">http:\/\/xinyiworld.top\/wordpress_it\/?p=14635<\/a><\/li>\n<\/ul>\n<h1><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E3%80%81JavaVM\"><\/span>\u4e8c\u3001JavaVM<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u540cJNIEnv\uff0cJavaVM\u4e3a_JavaVM\u6216JNIInvokeInterface*\uff0c_JavaVM\u662f\u5bf9JNIInvokeInterface\u7684\u5c01\u88c5\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"JNIInvokeInterface\"><\/span>JNIInvokeInterface<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-cpp\">1034 \/*\n1035  * JNI invocation interface.\n1036  *\/\n1037 struct JNIInvokeInterface {\n1038     void*       reserved0;\n1039     void*       reserved1;\n1040     void*       reserved2;\n1041 \n1042     jint        (*DestroyJavaVM)(JavaVM*);\n1043     jint        (*AttachCurrentThread)(JavaVM*, JNIEnv**, void*);\n1044     jint        (*DetachCurrentThread)(JavaVM*);\n1045     jint        (*GetEnv)(JavaVM*, void**, jint);\n1046     jint        (*AttachCurrentThreadAsDaemon)(JavaVM*, JNIEnv**, void*);\n1047 };<\/code><\/pre>\n<button class=\"simplefavorite-button\" data-postid=\"14621\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6\u4f4d\u7f6e\uff1alibnativehelper\/include_jni\/jni.h https:\/\/dev [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1064],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14621"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14621"}],"version-history":[{"count":23,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14621\/revisions"}],"predecessor-version":[{"id":14801,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14621\/revisions\/14801"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14621"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}