{"id":141,"date":"2007-03-27T22:37:00","date_gmt":"2007-03-27T22:37:00","guid":{"rendered":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/"},"modified":"2007-03-27T22:37:00","modified_gmt":"2007-03-27T22:37:00","slug":"jni-c-object-instantiation","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/","title":{"rendered":"JNI, C++ object instantiation"},"content":{"rendered":"<p>So my next task is to create a java gui using a C++ library. <br \/>The library is pretty simple. There is one main class with a few supporting classes. The supporting classes are basically just structs which hold variables.<br \/>The first thing to be done is create a Java class similar to the main C++ class. This is called a <a href=\"http:\/\/java.sun.com\/developer\/JDCTechTips\/2001\/tt0612.html\" target=\"_blank\" rel=\"noopener\">peer class<\/a>. The methods in the java class are native functions whose implementations will be done using the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/guide\/jni\/\" target=\"_blank\" rel=\"noopener\">Java Native Interface (JNI).<\/a><br \/>In the java class constructor, a jni call to create the C++ class will need to be made. The java class will also need to override the finalize function in which it will have a jni call to delete the C++ class.<br \/>The java class also needs to contain a variable which will be used to contain the pointer to the C++ class.<br \/>Now my first problem: How do I implement the JNI function which creates the C++ class? If anyone knows the answer, feel free to leave me a comment.<br \/>According to <a href=\"http:\/\/groups.google.com\/group\/comp.lang.java.programmer\/browse_thread\/thread\/457d8ce90ac5178a\/ae203d2000dc5933%23ae203d2000dc5933\" target=\"_blank\" rel=\"noopener\">this link<\/a>, a reinterpret cast is involved. <\/p>\n<p>C++ class:<\/p>\n<p>#include &#8220;CPPObject.h&#8221; <br \/>#include <stdio.h> <\/p>\n<p>CppObject::CppObject(int aFoo) { <br \/>   foo = aFoo; <br \/>} <\/p>\n<p>void CppObject::printFoo() { <br \/>   printf (&#8220;Value of foo is: %in&#8221;, foo); <br \/>} <\/p>\n<p>Java peer class:<\/p>\n<p>public class CppObjectPeer { <br \/>   static { <br \/>     System.loadLibrary(&#8220;cppobjects&#8221;); <br \/>   } <br \/>   protected long ptr; <br \/>   public CppObjectPeer(int aFoo) { <br \/>     this.ptr = createCppObject(aFoo); \/\/create object in native code <br \/>   } <br \/>   public void printFoo() { <br \/>     printFoo(this.ptr); \/\/print from native code <br \/>   } <br \/>   \/\/native methods <br \/>   private final native long createCppObject(int aFoo); <br \/>   private native void printFoo(long ptr); <br \/>} <\/p>\n<p>Jni implementations:<\/p>\n<p>JNIEXPORT jlong JNICALL Java_CppObjectPeer_createCppObject <br \/>(JNIEnv *env, jobject obj, jint fooValue) { <br \/>    CppObject *cppObj = new CppObject(fooValue); <br \/>    return reinterpret_cast<jlong>(cppObj); <br \/>}<\/p>\n<p>JNIEXPORT void JNICALL Java_CppObjectPeer_printFoo <br \/>   (JNIEnv *env, jobject obj, jlong ptr) { <br \/>       CppObject *cppObj = reinterpret_cast<cppObject*>(ptr); <br \/>       cppObj->printFoo(); <br \/>} <\/p>\n<p>Well, it looks simple enough. Just not sure why the book: <a href=\"http:\/\/www.amazon.com\/gp\/product\/0136798950?ie=UTF8&amp;tag=extcas-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0136798950\">Essential Jni: Java Native Interface (Essential Java)<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/>, by Rob Gordon doesn&#8217;t use this technique. Is this a new technique?<\/p>\n<p>The book uses a Registry class and a hash code function to store the pointer to the C++ object. This method seems like a lot of work for what I need. As an alternative, he suggests using get\/setCID functions which call get\/setLongField functions. The set functions requires an int val be passed in and I don&#8217;t understand where the val comes from. I guess for now I&#8217;m going to try the reinterpret_cast method.<br \/>Stay tuned for how it works out.<\/p>\n<p><iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?t=extcas-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0136798950&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?t=extcas-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0201325772&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So my next task is to create a java gui using a C++ library. The library is pretty simple. There is one main class with a few supporting classes. The supporting classes are basically just structs which hold variables.The first thing to be done is create a Java class similar to the main C++ class. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295,296],"tags":[],"class_list":["post-141","post","type-post","status-publish","format-standard","hentry","category-c-object-instantiation","category-jni"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JNI, C++ object instantiation - Zofxare Blog Home<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JNI, C++ object instantiation - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"So my next task is to create a java gui using a C++ library. The library is pretty simple. There is one main class with a few supporting classes. The supporting classes are basically just structs which hold variables.The first thing to be done is create a Java class similar to the main C++ class. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2007-03-27T22:37:00+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950\" \/>\n<meta name=\"author\" content=\"zofxare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"zofxare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/\"},\"author\":{\"name\":\"zofxare\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\"},\"headline\":\"JNI, C++ object instantiation\",\"datePublished\":\"2007-03-27T22:37:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/\"},\"wordCount\":417,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950\",\"articleSection\":[\"C++ object instantiation\",\"JNI\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/\",\"name\":\"JNI, C++ object instantiation - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950\",\"datePublished\":\"2007-03-27T22:37:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950\",\"contentUrl\":\"http:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/03\\\/27\\\/jni-c-object-instantiation\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JNI, C++ object instantiation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\",\"name\":\"zofxare\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"caption\":\"zofxare\"},\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/zofxare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JNI, C++ object instantiation - Zofxare Blog Home","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/","og_locale":"en_US","og_type":"article","og_title":"JNI, C++ object instantiation - Zofxare Blog Home","og_description":"So my next task is to create a java gui using a C++ library. The library is pretty simple. There is one main class with a few supporting classes. The supporting classes are basically just structs which hold variables.The first thing to be done is create a Java class similar to the main C++ class. [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/","og_site_name":"Zofxare Blog Home","article_published_time":"2007-03-27T22:37:00+00:00","og_image":[{"url":"http:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950","type":"","width":"","height":""}],"author":"zofxare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"zofxare","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/"},"author":{"name":"zofxare","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce"},"headline":"JNI, C++ object instantiation","datePublished":"2007-03-27T22:37:00+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/"},"wordCount":417,"commentCount":0,"image":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#primaryimage"},"thumbnailUrl":"http:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950","articleSection":["C++ object instantiation","JNI"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/","name":"JNI, C++ object instantiation - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#primaryimage"},"image":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#primaryimage"},"thumbnailUrl":"http:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950","datePublished":"2007-03-27T22:37:00+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#primaryimage","url":"http:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950","contentUrl":"http:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=as2&amp;o=1&amp;a=0136798950"},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/03\/27\/jni-c-object-instantiation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"JNI, C++ object instantiation"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce","name":"zofxare","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","caption":"zofxare"},"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/zofxare\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":0,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}