/usr/share/doc/libjna-java/api/overview-summary.html is in libjna-java-doc 4.5.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html>
<head>
<!-- Generated by javadoc -->
<title>Overview (JNA API)</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<script type="text/javascript"><!--
try {
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="Overview (JNA API)";
}
}
catch(err) {
}
//-->
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="topNav"><a name="navbar.top">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.top.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li class="navBarCell1Rev">Overview</li>
<li>Package</li>
<li>Class</li>
<li><a href="overview-tree.html">Tree</a></li>
<li><a href="deprecated-list.html">Deprecated</a></li>
<li><a href="index-all.html">Index</a></li>
<li><a href="help-doc.html">Help</a></li>
</ul>
<div class="aboutLanguage"><b>JNA API</><font size="-1"> 4.5.1</font></div>
</div>
<div class="subNav">
<ul class="navList">
<li>Prev</li>
<li>Next</li>
</ul>
<ul class="navList">
<li><a href="index.html?overview-summary.html" target="_top">Frames</a></li>
<li><a href="overview-summary.html" target="_top">No Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_top">
<li><a href="allclasses-noframe.html">All Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_top");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<a name="skip.navbar.top">
<!-- -->
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<div class="header">
<h1 class="title">JNA API Documentation</h1>
</div>
<div class="header">
<div class="subTitle">
<div class="block">This document is the API specification for the
<a href=http://github.com/twall/jna>JNA</a>
library for simplified native library access for Java.</div>
</div>
<p>See: <a href="#overview.description">Description</a></p>
</div>
<div class="contentContainer">
<table class="overviewSummary" border="0" cellpadding="3" cellspacing="0" summary="Java Native Access table, listing packages, and an explanation">
<caption><span>Java Native Access</span><span class="tabEnd"> </span></caption>
<tr>
<th class="colFirst" scope="col">Package</th>
<th class="colLast" scope="col">Description</th>
</tr>
<tbody>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/package-summary.html">com.sun.jna</a></td>
<td class="colLast">
<div class="block">Provides simplified native library access.</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="com/sun/jna/ptr/package-summary.html">com.sun.jna.ptr</a></td>
<td class="colLast">
<div class="block">Provides various native pointer-to-type (<code><type> *</code>)
representations.</div>
</td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/win32/package-summary.html">com.sun.jna.win32</a></td>
<td class="colLast">
<div class="block">Provides type and function mappers required for standard APIs on the Windows platform.</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="contentContainer">
<table class="overviewSummary" border="0" cellpadding="3" cellspacing="0" summary="Platform Utilities table, listing packages, and an explanation">
<caption><span>Platform Utilities</span><span class="tabEnd"> </span></caption>
<tr>
<th class="colFirst" scope="col">Package</th>
<th class="colLast" scope="col">Description</th>
</tr>
<tbody>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/platform/package-summary.html">com.sun.jna.platform</a></td>
<td class="colLast">
<div class="block">Provides cross-platform utilities based on platform-specific libraries.</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="com/sun/jna/platform/dnd/package-summary.html">com.sun.jna.platform.dnd</a></td>
<td class="colLast">
<div class="block">Provides integrated, extended drag and drop functionality,
allowing ghosted drag images to be used on all platforms.</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="contentContainer">
<table class="overviewSummary" border="0" cellpadding="3" cellspacing="0" summary="Platform Specific table, listing packages, and an explanation">
<caption><span>Platform Specific</span><span class="tabEnd"> </span></caption>
<tr>
<th class="colFirst" scope="col">Package</th>
<th class="colLast" scope="col">Description</th>
</tr>
<tbody>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/platform/mac/package-summary.html">com.sun.jna.platform.mac</a></td>
<td class="colLast">
<div class="block">Provides common library mappings for the OS X platform.</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="com/sun/jna/platform/unix/package-summary.html">com.sun.jna.platform.unix</a></td>
<td class="colLast">
<div class="block">Provides common library mappings for Unix and X11-based platforms.</div>
</td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/platform/unix/solaris/package-summary.html">com.sun.jna.platform.unix.solaris</a></td>
<td class="colLast">
<div class="block">Provides common library mappings for the Solaris (SunOS) platform.</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="com/sun/jna/platform/win32/package-summary.html">com.sun.jna.platform.win32</a></td>
<td class="colLast">
<div class="block">Provides common library mappings for the Windows platform.</div>
</td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/platform/win32/COM/package-summary.html">com.sun.jna.platform.win32.COM</a></td>
<td class="colLast">
<div class="block">Provides...</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="com/sun/jna/platform/win32/COM/tlb/package-summary.html">com.sun.jna.platform.win32.COM.tlb</a></td>
<td class="colLast">
<div class="block">Provides...</div>
</td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/platform/win32/COM/tlb/imp/package-summary.html">com.sun.jna.platform.win32.COM.tlb.imp</a></td>
<td class="colLast">
<div class="block">Provides...</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="com/sun/jna/platform/win32/COM/util/package-summary.html">com.sun.jna.platform.win32.COM.util</a></td>
<td class="colLast"> </td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="com/sun/jna/platform/win32/COM/util/annotation/package-summary.html">com.sun.jna.platform.win32.COM.util.annotation</a></td>
<td class="colLast"> </td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="com/sun/jna/platform/wince/package-summary.html">com.sun.jna.platform.wince</a></td>
<td class="colLast"> </td>
</tr>
</tbody>
</table>
</div>
<div class="contentContainer"><a name="overview.description">
<!-- -->
</a>
<div class="block">This document is the API specification for the
<a href=http://github.com/twall/jna>JNA</a>
library for simplified native library access for Java.
<p>
<a href=#navbar_top>Top</a>
<a href=http://github.com/twall/jna>JNA Home</a>
<a href=index.html target=_top>API w/FRAMES</a>
<p>
<a name="overview"></a>
<h1>Java Native Access (JNA)</h1>
JNA provides simplified access to native library methods without requiring any
additional JNI or native code.
<p>
<a name="toc"></a>
<h2>Table of Contents</h2>
<ul>
<li><a href="#loading">Loading JNA</a>
<li><a href="#library-mapping">Library Mapping</a>
<li><a href="#function-mapping">Function Mapping</a>
<li><a href="#marshalling">Type Mapping</a>
<ul>
<li><a href="#marshalling">Primitive Types</a>
<li><a href="#pointers">Pointers</a>
<li><a href="#strings">Strings</a>
<li><a href="#wide-strings">Wide (UNICODE) Strings</a>
<li><a href="#arrays">Primitive Arrays</a>
<li><a href="#buffers">Buffers/Memory Blocks</a>
<li><a href="#callbacks">Callbacks/Function Pointers</a>
<li><a href="#varargs">Variable Argument Lists (Varargs)</a>
<li><a href="#structures">Structures</a>
<li><a href="#unions">Unions</a>
<li><a href="#java-objects">Java Objects</a>
<li><a href="#last-error">Last Error</a>
</ul>
<li><a href="#invocation-mapping">Invocation Mapping</a>
<li><a href="#global-data">Library Global Data</a>
<li><a href="#crash-protection">VM Crash Protection</a>
<li><a href="#performance">Performance</a>
</ul>
<p>
<a href=#navbar_top>Top</a>
<a name="loading"></a>
<h2>Loading JNA</h2>
JNA includes a small, platform-specific shared library which enables all
native access. When the <a href="com/sun/jna/Native.html" title="class in com.sun.jna"><code>Native</code></a> class is first accessed,
JNA will first attempt to load this library from the directories specified
in <code>jna.boot.library.path</code>. If that fails, it will fall back to
loading from the system library paths. Finally it will attempt to extract the
stub library from from the JNA jar file, and load it.
<p/>
The <code>jna.boot.library.path</code> property is mainly to support
jna.jar being included in -Xbootclasspath, where
<code>java.library.path</code> and LD_LIBRARY_PATH are ignored. It is also
useful for designating a version of the library to use in preference to any
which may already be installed on the system.
<p/>
Loading from the system may be disabled by <code>jna.nosys=true</code>,
and unpacking from the jar file may be disabled by
<code>jna.nounpack=true</code>.
<p/>
The library name used to search for JNA's native library may be altered
by setting <code>jna.boot.library.name</code>, which defaults to
"jnidispatch". It may be useful to set this value if your system
requires unique names for shared libraries (rather than unique paths),
or if your system must store different versions of the JNA shared
library (e.g. for different architectures) in the same directory.
<p/>
<a href=#navbar_top>Top</a>
<a name="library-mapping"></a>
<h2>Library Mapping</h2>
When you've determined which shared library holds the methods to which you
need access, create a class corresponding to that library. For
example, a mapping for the C library itself would look like one of the
following:<br>
<blockquote><code><pre>
// Alternative 1: interface-mapped class, dynamically load the C library
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)Native.loadLibrary("c", CLibrary.class);
}
// Alternative 2: direct-mapped class (uses a concrete class rather than an
// interface, with a slight variation in <a href="#direct-mapping">method
// declarations</a>).
public class CLibrary {
static {
Native.register("c");
}
}
</pre></code></blockquote>
The <code>String</code> passed to the
<a href="com/sun/jna/Native.html#loadLibrary-java.lang.String-java.lang.Class-"><code>Native.loadLibrary(String,Class)</code></a>
(or <a href="com/sun/jna/NativeLibrary.html#getInstance-java.lang.String-"><code>NativeLibrary.getInstance(String)</code></a>) method
is the undecorated name of the shared library file. Here are some examples of
library name mappings.<p>
<style type="text/css">
table.styled { border-collapse:collapse; background-color:#EAEAEA; }
td { padding-left: 10px; padding-right: 10px; }
blockquote { background-color:#EAEAEA; }
</style>
<table border=1 borderColor=white class=styled>
<tr><td><b>OS</b></td><td><b>Library Name</b></td><td><b>String</b></td></tr>
<tr><td>Windows</td><td>user32.dll</td><td>user32</td></tr>
<tr><td>Linux</td><td>libX11.so</td><td>X11</td></tr>
<tr><td>Mac OS X</td><td>libm.dylib</td><td>m</td></tr>
<tr><td>Mac OS X Framework</td><td>/System/Library/Frameworks/Carbon.framework/Carbon</td><td>Carbon</td></tr>
<tr><td>Any Platform</td><td><current process></td><td><code>null</code></td></tr>
</table>
<p>
Any given native library with a unique filesystem path is represented by a single instance of <a href="com/sun/jna/NativeLibrary.html" title="class in com.sun.jna"><code>NativeLibrary</code></a> and obtained via <a href="com/sun/jna/NativeLibrary.html#getInstance-java.lang.String-"><code>NativeLibrary.getInstance(String)</code></a>. The native library will be unloaded when no longer referenced by any Java code.
<p>
If the library name is <code>null</code>, your mappings will apply to the
current process instead of a separately loaded library. This may help avoid
conflicts if there are several incompatible versions of a library available.
<p>
The search path for loaded native libraries may be modified by
setting <code>jna.library.path</code> and a few other properties. You may
also bundle native libraries in a jar file and have JNA automatically extract
them for loading. See <a href="com/sun/jna/NativeLibrary.html" title="class in com.sun.jna"><code>NativeLibrary</code></a> for details.
<p>
<a href="#toc">Table of Contents</a>
<a name="function-mapping"></a>
<h2>Function Mapping</h2>
Function names are mapped directly from their Java interface name to the
symbol exported by the native library. For instance, the function to convert
an ASCII string into an integer would look like this:<br>
<blockquote><code><pre>
public interface CLibrary extends Library {
int atol(String s);
}
</pre></code></blockquote>
<a name="direct-mapping"></a>
Alternatively, you can map directly to a declared native method
(with <a href="/twall/jna/blob/master/www/DirectMapping.md">some restrictions</a>):<br>
<blockquote><code><pre>
public class CLibrary {
public static native int atol(String s);
}
</pre></code></blockquote>
If you prefer to rename the Java methods to conform to Java coding conventions, then you can provide an entry (<a href="com/sun/jna/Library.html#OPTION_FUNCTION_MAPPER"><code>Library.OPTION_FUNCTION_MAPPER</code></a>/<a href="com/sun/jna/FunctionMapper.html" title="interface in com.sun.jna"><code>FunctionMapper</code></a>) in the options <a href="/usr/share/doc/default-jdk-doc/api/java/util/Map.html?is-external=true" title="class or interface in java.util"><code>Map</code></a> passed to <a href="com/sun/jna/Native.html#loadLibrary-java.lang.String-java.lang.Class-java.util.Map-"><code>Native.loadLibrary()</code></a> which maps the Java names to the native names. While this keeps your Java code a little cleaner, the additional mapping of names may make it a little less obvious the native functions being called.<p>
An instance of the <a href="com/sun/jna/Function.html" title="class in com.sun.jna"><code>Function</code></a> class is obtained through the <a href="com/sun/jna/NativeLibrary.html" title="class in com.sun.jna"><code>NativeLibrary</code></a> instance corresponding to the containing native library. This <a href="com/sun/jna/Function.html" title="class in com.sun.jna"><code>Function</code></a> instance handles argument marshalling and delegation to the native function.
<p>
<a href="#toc">Table of Contents</a>
<a name="marshalling"></a>
<h2>Marshalling/Unmarshalling (Java/Native Type Conversions)</h2>
Java types must be chosen to match native types of the same size. Following are the types supported by the JNA library.<p>
<center>
<table border=1 borderColor=white class=styled width="100%">
<tr><td><b>C Type</b></td><td><b>Native Representation</b></td><td><b>Java Type</b></td></tr>
<tr><td>char</td><td>8-bit integer</td><td>byte</td></tr>
<tr><td>wchar_t</td><td>platform-dependent</td><td>char</td></tr>
<tr><td>short</td><td>16-bit integer</td><td>short</td></tr>
<tr><td>int</td><td>32-bit integer</td><td>int</td></tr>
<tr><td>int</td><td>boolean flag</td><td>boolean</td></tr>
<tr><td>enum</td><td>enumeration type</td><td>int (usually)</td></tr>
<tr><td>long long, __int64</td><td>64-bit integer</td><td>long</td></tr>
<tr><td>float</td><td>32-bit floating point</td><td>float</td></tr>
<tr><td>double</td><td>64-bit floating point</td><td>double</td></tr>
<tr><td>pointer (e.g. void*)</td><td>platform-dependent (32- or 64-bit pointer to memory)</td><td><a href="/usr/share/doc/default-jdk-doc/api/java/nio/Buffer.html?is-external=true" title="class or interface in java.nio"><code>Buffer</code></a><br>
<a href="com/sun/jna/Pointer.html" title="class in com.sun.jna"><code>Pointer</code></a></td></tr>
<tr><td>pointer (e.g. void*),<br>array</td><td>32- or 64-bit pointer to memory (argument/return)<br>contiguous memory (struct member)</td><td><P>[] (array of primitive type)</td></tr>
<tr><td colspan='3' style='padding:20px 5px'>In addition to the above types, which are supported at the native layer, the JNA Java library automatically handles the following types. All but <code>NativeMapped</code> and <code>NativeLong</code> are converted to <a href="com/sun/jna/Pointer.html" title="class in com.sun.jna"><code>Pointer</code></a> before being passed to the native layer.</td></tr>
<tr><td>long</td><td>platform-dependent (32- or 64-bit integer)</td><td><a href="com/sun/jna/NativeLong.html" title="class in com.sun.jna"><code>NativeLong</code></a></td></tr>
<tr><td>const char*</td><td>NUL-terminated array (native encoding or <code>jna.encoding</code>)</td><td><a href="/usr/share/doc/default-jdk-doc/api/java/lang/String.html?is-external=true" title="class or interface in java.lang"><code>String</code></a></td></tr>
<tr><td>const wchar_t*</td><td>NUL-terminated array (unicode)</td><td><a href="com/sun/jna/WString.html" title="class in com.sun.jna"><code>WString</code></a></td></tr>
<tr><td>char**</td><td>NULL-terminated array of C strings</td><td><a href="/usr/share/doc/default-jdk-doc/api/java/lang/String.html?is-external=true" title="class or interface in java.lang"><code>String[]</code></a></td></tr>
<tr><td>wchar_t**</td><td>NULL-terminated array of wide C strings</td><td><a href="com/sun/jna/WString.html" title="class in com.sun.jna"><code>WString[]</code></a></td></tr>
<tr><td>void**</td><td>NULL-terminated array of pointers</td><td><a href="com/sun/jna/Pointer.html" title="class in com.sun.jna"><code>Pointer[]</code></a></td></tr>
<tr><td>struct*<br>struct</td><td>pointer to struct (argument or return) (<a href="com/sun/jna/Structure.ByReference.html" title="interface in com.sun.jna"><code>or explicitly</code></a>)<br>struct by value (member of struct) (<a href="com/sun/jna/Structure.ByValue.html" title="interface in com.sun.jna"><code>or explicitly</code></a>)</td><td><a href="com/sun/jna/Structure.html" title="class in com.sun.jna"><code>Structure</code></a></td></tr>
<tr><td>union</td><td>same as <code>Structure</code></td><td><a href="com/sun/jna/Union.html" title="class in com.sun.jna"><code>Union</code></a></td></tr>
<tr><td>struct[]</td><td>array of structs, contiguous in memory</td><td><a href="com/sun/jna/Structure.html" title="class in com.sun.jna"><code>Structure[]</code></a></td></tr>
<tr><td>void (*FP)()</td><td>function pointer (Java or native)</td><td><a href="com/sun/jna/Callback.html" title="interface in com.sun.jna"><code>Callback</code></a></td></tr>
<tr><td>pointer (<T> *)</td><td>same as <code>Pointer</code></td><td><a href="com/sun/jna/PointerType.html" title="class in com.sun.jna"><code>PointerType</code></a></td></tr>
<tr><td>other</td><td>integer type</td><td><a href="com/sun/jna/IntegerType.html" title="class in com.sun.jna"><code>IntegerType</code></a></td></tr>
<tr><td>other</td><td>custom mapping, depends on definition</td><td><a href="com/sun/jna/NativeMapped.html" title="interface in com.sun.jna"><code>NativeMapped</code></a></td></tr>
</table>
</center>
<p>
<b>NOTES</b>
<ul>
<li>Unsigned values may be passed by assigning the corresponding
two's-complement representation to the signed type of the same size.
<li>Java arrays of primitive type may be wrapped by <a href="/usr/share/doc/default-jdk-doc/api/java/nio/Buffer.html?is-external=true" title="class or interface in java.nio"><code>Buffer</code></a> in
order to access a subset of the array (changing the effective size and/or
offest).
<li>Java arrays of primitive type are only valid for use within the scope of a
single call. If the native code keeps a reference to the memory, use <a href="com/sun/jna/Memory.html" title="class in com.sun.jna"><code>Memory</code></a> or <a href="/usr/share/doc/default-jdk-doc/api/java/nio/Buffer.html?is-external=true" title="class or interface in java.nio"><code>Buffer</code></a> instead.
<li>Primitive arrays and structures as members of a structure are overlaid on the parent structure memory.
<li>Bitfields must be manually packed into an integer type.
<li>All other types must eventually be converted to one of the types in the this table. Methods with arguments or return values of types other than these must either use types deriving from <a href="com/sun/jna/NativeMapped.html" title="interface in com.sun.jna"><code>NativeMapped</code></a> or supply type conversion information for the unsupported types.
<li>Type mapping behavior may be customized by providing a <a href="com/sun/jna/TypeMapper.html" title="interface in com.sun.jna"><code>TypeMapper</code></a> for the <a href="com/sun/jna/Library.html#OPTION_TYPE_MAPPER"><code>Library.OPTION_TYPE_MAPPER</code></a> option when initializing a library interface. See <a href="com/sun/jna/win32/W32APITypeMapper.html" title="class in com.sun.jna.win32"><code>W32APITypeMapper</code></a> for an example which provides custom conversion of boolean and String types. You are free to use whatever types are convenient in your defined interfaces, but all custom types <em>must</em> provide a mapping to one of the basic or derived types listed above.
<li>Type mapping may also be customized on a per-class basis for user-defined types by making the user-defined type implement the <a href="com/sun/jna/NativeMapped.html" title="interface in com.sun.jna"><code>NativeMapped</code></a> interface.
<li><code>Structure</code> and <code>Union</code> are <em>not</em> converted to <code>Pointer</code> when passed by value.
</ul>
<a name="arrays"></a>
<h3>Primitive Arrays</h3>
Java primitive arrays may be used wherever a native primitive array is used. Any changes made by the native code to an array during a function call will be reflected in the Java array. If the native code will use the array outside of the function call where the array is provided, <a href="com/sun/jna/Memory.html" title="class in com.sun.jna"><code>Memory</code></a> or <a href="/usr/share/doc/default-jdk-doc/api/java/nio/Buffer.html?is-external=true" title="class or interface in java.nio"><code>Buffer</code></a> should be used instead (see <a href="#buffers">Buffers</a>).
<p>
To map a native multi-dimensional array, use a single-dimensional Java array with a number of elements equivalent to the full native array, e.g.<br>
<blockquote><code><pre>
// Original C code
#define DIM0 2
#define DIM1 3
int array[DIM0][DIM1];
int i,j;
for (i=0;i < DIM0;i++) {
for (j=0;j < DIM1;j++) {
array[i][j] = i*DIM1 + j;
}
}
// Equivalent JNA code
final int DIM0 = 2;
final int DIM1 = 3;
int[] array = new int[6];
for (int i=0;i < DIM0;i++) {
for (int j=0;j < DIM1;j++) {
array[i*DIM1 + j] = i*DIM1 + j;
}
}
</pre></code></blockquote>
<a name="pointers"></a>
<h3>Pointers</h3>
Pointers may be used as an opaque type from which other data types may be extracted. The Pointer type is a reasonable fallback for any pointer-based type (including arrays). The user is generally not allowed to construct a Pointer de novo.
<p>
Type-safe pointers may be defined by deriving from the <a href="com/sun/jna/PointerType.html" title="class in com.sun.jna"><code>PointerType</code></a> class. Any such user-defined type will be treated the same as a <a href="com/sun/jna/Pointer.html" title="class in com.sun.jna"><code>Pointer</code></a>.
<a name="strings"></a>
<h3>Strings</h3>
Java <code>String</code>s perform the same function as the native types
<code>const char*</code> and <code>const wchar_t*</code>
(<code>NUL</code>-terminated arrays). In order to use the proper type when
calling a native function, we have to introduce some sort of annotation to
identify how the java <code>String</code> should be converted.
Java <code>String</code>s are normally converted to <code>char*</code>
since this is the most common usage of strings. Strings are automatically
converted to a <code>NUL</code>-terminated array of <code>char</code> across
the function call. Returned <code>char*</code> values are automatically
copied into a <code>String</code> if the method signature returns
<code>String</code> (<code>strdup</code>, for example).
<p>
If the native method returns char* and actually allocates
memory, a return type of <a href="com/sun/jna/Pointer.html" title="class in com.sun.jna"><code>Pointer</code></a> should be used to avoid
leaking the memory. It is then up to you to take the necessary steps to free
the allocated memory.
<p>
When converting Java unicode characters into an array of <code>char</code>,
the default platform encoding is used, unless the system property
<code>jna.encoding</code> is set to a valid encoding. This property may be
set to "UTF8", for example, to ensure all native strings use that encoding.
<p>
Arrays of <code>String</code> passed to native code (either as a function argument or callback return value) will be converted into a NULL-terminated array of <code>char*</code> (or <code>wchar_t*</code> in the case of an array of <code>WString</code>.
<a name="wide-strings"></a>
<h3>Wide Strings</h3>
The <a href="com/sun/jna/WString.html" title="class in com.sun.jna"><code>WString</code></a> class is used to identify wide character strings. Unicode values are copied directly from the Java <code>char</code> array to a native <code>wchar_t</code> array.
<p>
<a name="buffers"></a>
<h3>Buffers/Memory Blocks</h3>
Use arrays to represent buffers of primitive types passed to a function for use only during the function invocation. A native method cannot return a Java array, since there is no canonical way to indicate the intended length of the returned array. Instead, use one of the array access methods in the Pointer class, supplying the length of the returned array.<p>
<a href="/usr/share/doc/default-jdk-doc/api/java/nio/Buffer.html?is-external=true" title="class or interface in java.nio"><code>Buffer</code></a>s may also be used as a memory buffer input
argument; direct byte buffers can often provide much improved performance over
primitive arrays. A pointer provided by native code may be converted to a <a href="/usr/share/doc/default-jdk-doc/api/java/nio/Buffer.html?is-external=true" title="class or interface in java.nio"><code>Buffer</code></a> by calling <a href="com/sun/jna/Pointer.html#getByteBuffer-long-long-"><code>Pointer.getByteBuffer(long, long)</code></a>.
<p>
If you need to pass in a subset of a primitive array, you can do so by
wrapping it in a <a href="/usr/share/doc/default-jdk-doc/api/java/nio/Buffer.html?is-external=true" title="class or interface in java.nio"><code>Buffer</code></a> subclass, such as <a href="/usr/share/doc/default-jdk-doc/api/java/nio/ByteBuffer.html?is-external=true" title="class or interface in java.nio"><code>ByteBuffer</code></a>, using the <a href="/usr/share/doc/default-jdk-doc/api/java/nio/ByteBuffer.html?is-external=true#wrap-byte:A-int-int-" title="class or interface in java.nio"><code>ByteBuffer.wrap(byte[],int,int)</code></a> method. Wrapping an array in a
buffer also allows you to pass only a subset of a Java array to the native
function.
<a name="callbacks"></a>
<h3>Callbacks (Function Pointers)</h3>
JNA supports supplying Java callbacks to native code. You must define an
interface that extends the <a href="com/sun/jna/Callback.html" title="interface in com.sun.jna"><code>Callback</code></a> interface, and define
a single <code>callback</code> method with a signature that matches the
function pointer required by the native code. The name of the method
may be something other than "callback" only if there is only a single method
in the interface which extends Callback or the class which implements
<a href="com/sun/jna/Callback.html" title="interface in com.sun.jna"><code>Callback</code></a>. The arguments and return value follow the same
rules as for a direct function invocation.
<p>
When accessing Windows APIs, sometimes the documentation indicates that a
function pointer parameter must refer to a function that resides in a
DLL. In these instances, add the <a href="com/sun/jna/win32/DLLCallback.html" title="interface in com.sun.jna.win32"><code>DLLCallback</code></a>
interface to your callback definition. The function pointer as seen by
Windows will be located in the <code>jnidispatch.dll</code> module.
<p>
If the callback returns a <code>String</code> or <code>String[]</code>, the
returned memory will be valid until the returned object is GC'd.
<p>
If your native code initializes function pointers within a struct, JNA will
automatically generate a <code>Callback</code> instance matching the declared
type. This enables you to easily call the function supplied by native code
using proper Java syntax.
<blockquote><code><pre>
// Original C code
struct _functions {
int (*open)(const char*,int);
int (*close)(int);
};
// Equivalent JNA mapping
public class Functions extends Structure {
public static interface OpenFunc extends Callback {
int invoke(String name, int options);
}
public static interface CloseFunc extends Callback {
int invoke(int fd);
}
public OpenFunc open;
public CloseFunc close;
}
...
Functions funcs = new Functions();
lib.init(funcs);
int fd = funcs.open.invoke("myfile", 0);
funcs.close.invoke(fd);
</pre></code></blockquote>
Callbacks may also be used as return values. Native function pointers are wrapped in a proxy implementing the declared Callback type, to facilitate calling from Java.
<blockquote><code><pre>
// Original C code
typedef void (*sig_t)(int);
sig_t signal(int signal, sig_t sigfunc);
// Equivalent JNA mapping
public interface CLibrary extends Library {
public interface SignalFunction extends Callback {
void invoke(int signal);
}
SignalFunction signal(int signal, SignalFunction func);
}
</pre></code></blockquote>
If you need control over the thread context in which a <code>Callback</code>
operates, you can install a <a href="com/sun/jna/CallbackThreadInitializer.html" title="class in com.sun.jna"><code>CallbackThreadInitializer</code></a> for
any given callback object. The first time the callback is called on a thread
that is not currently attached to the VM, the initializer will be queried to
determine how the thread should be set up. You can indicate the desired name,
thread group, and daemon state for the thread, as well as indicating whether
the thread should be left attached to the VM after callback exit. The latter
improves performance if you know you will be getting multiple callbacks on the
same thread, avoiding the need for the VM to generate multiple Java Thread
objects for the same native thread. If you do leave the native thread
attached, you should either ensure you detach it at some later point (by
calling <a href="com/sun/jna/Native.html#detach-boolean-"><code>Native.detach(boolean)</code></a> from within the callback just prior
to return) or return true from your
<a href="com/sun/jna/CallbackThreadInitializer.html#isDaemon-com.sun.jna.Callback-"><code>CallbackThreadInitializer.isDaemon(Callback)</code></a> method so
that the native thread will not prevent the VM from exiting.<p/>
If you don't need to otherwise customize the callback thread, you can simply
call <a href="com/sun/jna/Native.html#detach-boolean-"><code>Native.detach(boolean)</code></a> from within your callback to
indicate whether the thread attachment should be maintained or not.<p/>
<a name="varargs"></a>
<h3>Varargs</h3>
The C varargs function definition may be mapped to a Java varargs method definition. For example,
<blockquote><code><pre>
// Original C code
extern int printf(const char* fmt, ...);
// Equivalent JNA mapping
interface CLibrary extends Library {
int printf(String fmt, ...);
}
</pre></code></blockquote>
<em>Varargs are not supported when using <a href="/twall/jna/blob/master/www/DirectMapping.md">Direct mapping</a>.</em>
<a name="structures"></a>
<h3>Structures</h3>
The Java <a href="com/sun/jna/Structure.html" title="class in com.sun.jna"><code>Structure</code></a> represents a native <code>struct</code>. By default, this type is treated as a pointer to structure (<code>struct *</code>) on the native side when used as a parameter or return value. When used as a structure field, the structure is interpreted as by value. To force the complementary interpretation, the tagging interfaces <a href="com/sun/jna/Structure.ByValue.html" title="interface in com.sun.jna"><code>Structure.ByValue</code></a> and <a href="com/sun/jna/Structure.ByReference.html" title="interface in com.sun.jna"><code>Structure.ByReference</code></a> are provided.<p>
The data within a Java <code>Structure</code> is automatically written to
native memory just before a native function call with a struct parameter, and
automatically read from native memory after the function returns.<p>
<h4>Pointer-to-Structure Arguments</h4>
To pass a pointer to a structure as an argument, simply use the Java structure
subclass, and a pointer to native data memory will be used. The contents of
the structure will be passed to the function and updated when the function
returns. Structures are packed according to the default alignment rules for
the platform's native C <code>struct</code>s.
<blockquote><code><pre>
// Original C code
typedef struct _Point {
int x, y;
} Point;
Point* translate(Point* pt, int dx, int dy);
// Equivalent JNA mapping
class Point extends Structure { public int x, y; }
Point translate(Point pt, int x, int y);
...
Point pt = new Point();
Point result = translate(pt, 100, 100);
</pre></code></blockquote>
<a name="byvalue"></a>
<h4>Structure by Value Arguments/Return</h4>
To pass a structure by value, first define the structure, then define an empty
class from that which implements <a href="com/sun/jna/Structure.ByValue.html" title="interface in com.sun.jna"><code>Structure.ByValue</code></a>. Use
the <code>ByValue</code> class as the argument or return type.<p>
<blockquote><code><pre>
// Original C code
typedef struct _Point {
int x, y;
} Point;
Point translate(Point pt, int dx, int dy);
// Equivalent JNA mapping
class Point extends Structure {
public static class ByValue extends Point implements Structure.ByValue { }
public int x, y;
}
Point.ByValue translate(Point.ByValue pt, int x, int y);
...
Point.ByValue pt = new Point.ByValue();
Point result = translate(pt, 100, 100);
</pre></code></blockquote>
<h4>Array-of-Structure Arguments</h4>
To pass an array of structures, simply use a Java array of the desired
structure type. If the array is uninitialized, it will be auto-initialized
prior to the function call.<p>
<blockquote><code><pre>
// Original C code
void get_devices(struct Device[], int size);
// Equivalent JNA mapping
int size = ...
Device[] devices = new Device[size];
lib.get_devices(devices, devices.length);
</pre></code></blockquote>
Alternatively, you can reallocate a single Structure instance into an array as
follows:<br>
<blockquote><code><pre>
Device dev = new Device();
// As an array of Structure
Structure[] structs = dev.toArray(size);
// As an array of Device
Device[] devices = (Device[])dev.toArray(size);
</pre></code></blockquote>
<h4>Returning an Array of <code>struct</code></h4>
Declare the method as returning a <a href="com/sun/jna/Structure.html" title="class in com.sun.jna"><code>Structure</code></a> of the
appropriate type, then invoke <a href="com/sun/jna/Structure.html#toArray-int-"><code>Structure.toArray(int)</code></a> to
convert to an array of initialized structures of the appropriate size. Note
that your <a href="com/sun/jna/Structure.html" title="class in com.sun.jna"><code>Structure</code></a> class must have a no-args constructor,
and you are responsible for freeing the returned memory if applicable in
whatever way is appropriate for the called function.<p>
<blockquote><code><pre>
// Original C code
struct Display* get_displays(int* pcount);
void free_displays(struct Display* displays);
// Equivalent JNA mapping
Display get_displays(IntByReference pcount);
void free_displays(Display[] displays);
...
IntByReference pcount = new IntByReference();
Display d = lib.get_displays(pcount);
Display[] displays = (Display[])d.toArray(pcount.getValue());
...
lib.free_displays(displays);
</pre></code></blockquote>
<h4>Nested Structure Definitions</h4>
Nested structures are treated as consecutive memory (as opposed to pointers to
structures). For example:<br>
<blockquote><code><pre>
// Original C code
typedef struct _Point {
int x, y;
} Point;
typedef struct _Line {
Point start;
Point end;
} Line;
// Equivalent JNA mapping
class Point extends Structure {
public int x, y;
}
class Line extends Structure {
public Point start;
public Point end;
}
</pre></code></blockquote>
Explicit initialization of nested structures is not required; the objects will
be created as needed and properly mapped to the parent structure's memory.<p>
If you need a pointer to a structure within your structure, you can use the
<a href="com/sun/jna/Structure.ByReference.html" title="interface in com.sun.jna"><code>Structure.ByReference</code></a> tagging interface to indicate the
field should be treated as a pointer instead of inlining the full structure.
<blockquote><code><pre>
// Original C code
typedef struct _Line2 {
Point* p1;
Point* p2;
} Line2;
// Equivalent JNA mapping
class Point extends Structure {
public static class ByReference extends Point implements Structure.ByReference { }
public int x, y;
}
class Line2 extends Structure {
public Point.ByReference p1;
public Point.ByReference p2;
}
</pre></code></blockquote>
The more general case is just a pointer to memory. This allows you to define
the field without necessarily defining the inner structure itself, similar to
declaring a struct without defining it in C:<br>
<blockquote><code><pre>
// Original C code
typedef struct _Line2 {
Point* p1;
Point* p2;
} Line2;
// Equivalent JNA mapping
class Line2 extends Structure {
public Pointer p1;
public Pointer p2;
}
Line2 line2;
Point p1, p2;
...
line2.p1 = p1.getPointer();
line2.p2 = p2.getPointer();
</pre></code></blockquote>
<h4>Nested arrays</h4>
Structures with nested arrays require an explicit constructor to ensure the
structure size is properly calculated.
<blockquote><code><pre>
typedef struct _Buffer {
char buf1[32];
char buf2[1024];
} Buffer;
class Buffer extends Structure {
public byte[] buf1 = new byte[32];
public byte[] buf2 = new byte[1024];
}
</pre></code></blockquote>
Calculation of the native size of the structure is deferred until the
structure is actually used.
<h4>Variable-sized structures</h4>
Structures with variable size, or with primitive array elements, for example:<br>
<blockquote><code><pre>
// Original C code
typedef struct _Header {
int flags;
int buf_length;
char buffer[1];
} Header;
</pre></code></blockquote>
require a constructor which establishes the required size for the structure
and initializes things appropriately. For example:<br>
<blockquote><code><pre>
// Equivalent JNA mapping
class Header extends Structure {
public int flags;
public int buf_length;
public byte[] buffer;
public Header(int bufferSize) {
buffer = new byte[bufferSize];
buf_length = buffer.length;
allocateMemory();
}
}
</pre></code></blockquote>
<h4>Volatile fields</h4>
Normally, JNA will write the entire contents of a <code>Structure</code> prior
to a function call and read back from native memory after the function call.
Sometimes a structure field is not intended for client use, gets modified
asynchronously by hardware, or otherwise is effectively read-only.
If you expect any fields of the structure to be modified by any agent outside
your Java program, you should mark the field <code>volatile</code>. This
prevents JNA from automatically updating the native memory from the Java
value. You can still force an update of the native memory from the Java value
by calling <a href="com/sun/jna/Structure.html#writeField-java.lang.String-"><code>Structure.writeField(String)</code></a> for the field in
question.
<blockquote><code><pre>
class Data extends com.sun.jna.Structure {
public volatile int refCount;
public int value;
}
...
Data data = new Data();
</pre></code></blockquote>
In the above example, the field <code>refCount</code> will only be written
to native memory based on the Java value with a call to
<code>data.writeField("refCount")</code>. To obtain the current state of
native memory, call <a href="com/sun/jna/Structure.html#read--"><code>Structure.read()</code></a> (to update the entire
structure) or <a href="com/sun/jna/Structure.html#readField-java.lang.String-"><code>data.readField("refCount")</code></a> (to update just the <code>refCount</code> field).
<h4>Read-only fields</h4>
If you want to absolutely prevent Java code from modifying
a <code>Structure</code>'s contents, you may mark its
fields <code>final</code>. Structure reads can still overwrite the values
based on native memory contents, but no Java code will be able to modify any
of the fields.
<blockquote><code><pre>
class ReadOnly extends com.sun.jna.Structure {
// Do not initialize the field here, or the compiler will inline the value!
public final int refCount;
{
// Initialize fields here, to ensure the values are not inlined
refCount = -1;
read();
// refCount might now have a different value
}
}
...
ReadOnly ro = new ReadOnly();
// Will not compile!
ro.refCount = 0;
</pre></code></blockquote>
Make certain you attend to the following:
<ol>
<li>All final fields should be initialized in the constructor.
<li>If you call Structure.read() from anywhere but the constructor, keep in
mind that the compiler and/or hotspot will be assuming field values will not
change across that function call.
</ol>
<a name="unions"></a>
<h3>Unions</h3>
Unions are a special type of Structure. Each declared field within the union
overlays the same space in native memory. When writing a union to native
memory, you <em>must</em> specify which field is to be written by supplying
the desired field's class to the <a href="com/sun/jna/Union.html#setType-java.lang.Class-"><code>Union.setType(java.lang.Class<?>)</code></a>
method. On read, all non-pointer-based fields will be initialized from native
memory. Structure, String, and WString members will <em>not</em> be
initialized unless they are selected via <a href="com/sun/jna/Union.html#setType-java.lang.Class-"><code>Union.setType(java.lang.Class<?>)</code></a>.
<a name="last-error"></a>
<h3>Obtaining "last" error</h3>
If a function sets the system error property
(<code><a href="http://www.opengroup.org/onlinepubs/009695399/functions/errno.html">errno</a></code> or <code><a href="http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx">GetLastError()</a></code>), the error code will be thrown as a <a href="com/sun/jna/LastErrorException.html" title="class in com.sun.jna"><code>LastErrorException</code></a> if you declare the exception in your JNA mapping. Alternatively, you can use <a href="com/sun/jna/Native.html#getLastError--"><code>Native.getLastError()</code></a> to retrieve it, providing that <a href="com/sun/jna/Native.html#setPreserveLastError-boolean-"><code>Native.setPreserveLastError(boolean)</code></a> has been called with a <code>true</code> value. Throwing an exception is preferred since it has better performance.
<a name="java-objects"></a>
<h3>Arbitrary Java Object arguments/return values</h3>
In some cases, such as invoking native VM functions directly, it is necessary
to pass Java objects to the native methods. By default, JNA disallows using
any Java object that is not explicitly supported unless it derives from
<a href="com/sun/jna/NativeMapped.html" title="interface in com.sun.jna"><code>NativeMapped</code></a>, because it is generally unnecessary to use
such objects and usually signals a programmer error. To avoid errors flagging
the use of Java objects, use the library load option <a href="com/sun/jna/Library.html#OPTION_ALLOW_OBJECTS"><code>Library.OPTION_ALLOW_OBJECTS</code></a> with Boolean.TRUE.
<p>
<a href="#toc">Table of Contents</a>
<a name="invocation-mapping"></a>
<h2>Invocation Mapping</h2>
Sometimes native functions exist only as C preprocessor macros or as inline functions. If you need to do more than simply change the name of the invoked function (which can be handled via <a href="#function-mapping">Function Mapping</a>), an <a href="com/sun/jna/InvocationMapper.html" title="interface in com.sun.jna"><code>InvocationMapper</code></a> allows you to arbitrarily reconfigure the function invocation, including changing the method name and reordering, adding, or removing arguments. See the <a href="com/sun/jna/InvocationMapper.html" title="interface in com.sun.jna"><code>InvocationMapper</code></a> documentation for details.
<p>
<a href="#toc">Table of Contents</a>
<a name="global-data"></a>
<h2>Library Global Data</h2>
The method <a href="com/sun/jna/NativeLibrary.html#getGlobalVariableAddress-java.lang.String-"><code>NativeLibrary.getGlobalVariableAddress(java.lang.String)</code></a> may be used to obtain the address of global variables as a <a href="com/sun/jna/Pointer.html" title="class in com.sun.jna"><code>Pointer</code></a>. Pointer methods may then be used to read or write the value as appropriate for the variable type.
<p>
<a href="#toc">Table of Contents</a>
<a name="crash-protection"></a>
<h2>VM Crash Protection</h2>
It is not uncommon when defining a new library and writing tests to encounter memory access errors which crash the VM. These are often caused by improper mappings or invalid arguments passed to the native library. To generate Java errors instead of crashing the VM, call <a href="com/sun/jna/Native.html#setProtected-boolean-"><code>Native.setProtected(true)</code></a></code>. Not all platforms support this protection; if not, the value of <a href="com/sun/jna/Native.html#isProtected--"><code>Native.isProtected()</code></a> will remain <code>false</code>.<p>
NOTE: When protected mode is enabled, you should make use of the jsig library, if available (see <a href="http://download.oracle.com/javase/6/docs/technotes/guides/vm/signal-chaining.html">Signal Chaining</a>) to avoid interfering with the JVM's use of signals. In short, set the environment variable <code>LD_PRELOAD</code> (or <code>LD_PRELOAD_64</code>) to the path to <code>libjsig.so</code> in your JRE lib directory (usually ${java.home}/lib/${os.arch}/libjsig.so) before launching your Java application.
<p>
<a href="#toc">Table of Contents</a>
<a name="performance"></a>
<h2>Performance</h2>
<h3>Use direct mapping of methods</h3>
Using <a href="#direct-mapping">direct mapping</a> of methods makes native
calls more efficiently than does interface mapping. Direct mapping does not
support varargs calls or arrays of Pointer, String, or WString as an argument
or return value. For optimium results, use only primitive arguments and
return values; you'll have to convert to and from objects yourself explicitly.
<h3>Avoid type mapping</h3>
Type mapping incurs additional overhead on each function call. You can avoid
this by ensuring that your arguments and/or return types are already primitive
types.
<h3>Pointer/Array/Buffer Variants</h3>
Java primitive arrays are generally slower to use than direct memory
(Pointer, Memory, or ByReference) or NIO buffers, since the Java memory has to
be pinned and possibly copied across the native call, since the Java array is not necessarily contiguously allocated.
<h3>Large Structures</h3>
Structures are normally written to native memory before and read back from
native memory after a function call. With very large structures, there can be
a performance hit using reflection to walk through all the fields. Structure
auto-synch can be disabled by calling
<a href="com/sun/jna/Structure.html#setAutoSynch-boolean-"><code>Structure.setAutoSynch(boolean)</code></a> with a <code>false</code> parameter.
It is then up to you to use <a href="com/sun/jna/Structure.html#readField-java.lang.String-"><code>Structure.readField(String)</code></a>
and <a href="com/sun/jna/Structure.html#writeField-java.lang.String-"><code>Structure.writeField(String)</code></a> or <a href="com/sun/jna/Structure.html#writeField-java.lang.String-java.lang.Object-"><code>Structure.writeField(String,Object)</code></a> to synch with just the fields
of interest.
<h3>Throw exceptions on last error</h3>
To avoid the overhead of preserving the system last error information on every
native call, invoke
<a href="com/sun/jna/Native.html#setPreserveLastError-boolean-"><code>setPreserveLastError(false)</code></a> or
set the System property <code>jna.preserve_last_error=false</code>.<p/>
In those methods where you are interested in the value of errno/GetLastError(),
declare your method to throw <a href="com/sun/jna/LastErrorException.html" title="class in com.sun.jna"><code>LastErrorException</code></a>.</div>
</div>
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.bottom.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li class="navBarCell1Rev">Overview</li>
<li>Package</li>
<li>Class</li>
<li><a href="overview-tree.html">Tree</a></li>
<li><a href="deprecated-list.html">Deprecated</a></li>
<li><a href="index-all.html">Index</a></li>
<li><a href="help-doc.html">Help</a></li>
</ul>
<div class="aboutLanguage"><b>JNA API</><font size="-1"> 4.5.1</font></div>
</div>
<div class="subNav">
<ul class="navList">
<li>Prev</li>
<li>Next</li>
</ul>
<ul class="navList">
<li><a href="index.html?overview-summary.html" target="_top">Frames</a></li>
<li><a href="overview-summary.html" target="_top">No Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_bottom">
<li><a href="allclasses-noframe.html">All Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_bottom");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<a name="skip.navbar.bottom">
<!-- -->
</a></div>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
<p class="legalCopy"><small><center><i>Copyright © 2007-2018 Timothy Wall. All Rights Reserved.</i></center></small></p>
</body>
</html>
|