{"id":3110,"date":"2014-03-16T20:28:40","date_gmt":"2014-03-16T20:28:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/16\/how-do-i-get-a-java-jna-call-to-a-dll-to-get-data-returned-in-parameters-collection-of-common-programming-errors\/"},"modified":"2014-03-16T20:28:40","modified_gmt":"2014-03-16T20:28:40","slug":"how-do-i-get-a-java-jna-call-to-a-dll-to-get-data-returned-in-parameters-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/16\/how-do-i-get-a-java-jna-call-to-a-dll-to-get-data-returned-in-parameters-collection-of-common-programming-errors\/","title":{"rendered":"How do I get a java JNA call to a DLL to get data returned in parameters?-Collection of common programming errors"},"content":{"rendered":"<p>You need to declare the parameters as special types that convey the fact that they are passed by reference. So if the Delphi parameters are like this:<\/p>\n<pre><code>procedure Foo(var i: Integer; var d: Double);\n<\/code><\/pre>\n<pre><code>void Foo(IntByReference i, DoubleByReference d);\n<\/code><\/pre>\n<pre><code>IntByReference iref = new IntByReference();\nDoubleByReference dref = new DoubleByReference();\nINSTANCE.Foo(iref, dref);\nint i = iref.getValue();\ndouble d = dref.getValue():\n<\/code><\/pre>\n<blockquote>\n<p><strong>Using ByReference Arguments<\/strong><\/p>\n<p>When a function accepts a pointer-to-type argument you can use one of the ByReference types to capture the returned value, or subclass your own. For example:<\/p>\n<pre><code>\/\/ Original C declaration\nvoid allocate_buffer(char **bufp, int* lenp);\n\n\/\/ Equivalent JNA mapping\nvoid allocate_buffer(PointerByReference bufp, IntByReference lenp);\n\n\/\/ Usage\nPointerByReference pref = new PointerByReference();\nIntByReference iref = new IntByReference();\nlib.allocate_buffer(pref, iref);\nPointer p = pref.getValue();\nbyte[] buffer = p.getByteArray(0, iref.getValue());\n<\/code><\/pre>\n<p>Alternatively, you could use a Java array with a single element of the desired type, but the ByReference convention better conveys the intent of the code. The Pointer class provides a number of accessor methods in addition to getByteArray() which effectively function as a typecast onto the memory.<\/p>\n<p>Type-safe pointers may be declared by deriving from the PointerType class.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>You need to declare the parameters as special types that convey the fact that they are passed by reference. So if the Delphi parameters are like this: procedure Foo(var i: Integer; var d: Double); void Foo(IntByReference i, DoubleByReference d); IntByReference iref = new IntByReference(); DoubleByReference dref = new DoubleByReference(); INSTANCE.Foo(iref, dref); int i = iref.getValue(); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3110","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3110"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3110\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}