{"id":135515,"date":"2020-07-15T18:41:39","date_gmt":"2020-07-15T10:41:39","guid":{"rendered":"http:\/\/4563.org\/?p=135515"},"modified":"2020-07-15T18:41:39","modified_gmt":"2020-07-15T10:41:39","slug":"%e5%85%b3%e4%ba%8e-c-%e5%8f%8c%e6%8c%87%e9%92%88%e5%8f%82%e6%95%b0%e7%9a%84-jna-%e9%97%ae%e9%a2%98%e6%b1%82%e5%8a%a9-%e4%be%8b%e5%ad%90%e5%be%88%e7%ae%80%e5%8d%95%e6%b1%82%e5%a4%a7%e4%bd%ac%e6%8c%87","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=135515","title":{"rendered":"\u5173\u4e8e C \u53cc\u6307\u9488\u53c2\u6570\u7684 JNA \u95ee\u9898\u6c42\u52a9, \u4f8b\u5b50\u5f88\u7b80\u5355,\u6c42\u5927\u4f6c\u6307\u70b9\u4e0b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e C \u53cc\u6307\u9488\u53c2\u6570\u7684 JNA \u95ee\u9898\u6c42\u52a9, \u4f8b\u5b50\u5f88\u7b80\u5355,\u6c42\u5927\u4f6c\u6307\u70b9\u4e0b               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : camork <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8bdd\u4e0d\u591a\u8bf4,\u76f4\u63a5\u4e0a\u4ee3\u7801:<\/p>\n<p>C \u7ed3\u6784\u4f53<\/p>\n<pre><code>typedef struct _A {     unsigned int num;     struct _A *next; } A, *PA;  <\/code><\/pre>\n<p>\u6d4b\u8bd5\u65b9\u6cd5<\/p>\n<pre><code>void test(PA *a) {     PA current = (PA) malloc(sizeof(A));     current-&gt;num = 123321;      PA next = (PA) malloc(sizeof(A));     next-&gt;num = 456;     current-&gt;next = next;      *a = current; } <\/code><\/pre>\n<p>\u8fd9\u662f\u5728 C \u91cc\u9762\u7684\u8fd0\u884c\u4f8b\u5b50,<\/p>\n<pre><code>int main() {     PA a = NULL;     test(&amp;a);      printf(\"%dn\", a-&gt;num);     printf(\"%d\", a-&gt;next-&gt;num); } <\/code><\/pre>\n<p>JNA \u4ee3\u7801(\u4e0d\u786e\u5b9a\u6620\u5c04\u5bf9\u4e0d\u5bf9)<\/p>\n<pre><code>public interface DLLLibrary extends Library {  ......   void test(PointerByReference a); }  public class A extends Structure {  public int num;  public ByReference next;  public A() {   super();  }  protected List&lt;String&gt; getFieldOrder() {   return Arrays.asList(\"num\", \"next\");  }   public A(Pointer peer) {   super(peer);  }  public static class ByReference extends A implements Structure.ByReference { }  public static class ByValue extends A implements Structure.ByValue { } } <\/code><\/pre>\n<pre><code>public static void main(String[] args) {     PointerByReference pointer = new PointerByReference();     DLLLibrary.INSTANCE.test(pointer);      assert pointer.getValue().getInt(0) == 123321; \/\/\u8fd9\u91cc\u662f\u6b63\u786e\u7684     A a = new A(pointer.getValue());     \/\/assert a.next.num == 456;  \/\/\u540e\u7eed\u671f\u671b\u7684\u8c03\u7528     a.read(); \/\/java.lang.Error: Invalid memory access } <\/code><\/pre>\n<p>\u6700\u540e Java \u8fd9\u8fb9\u4f20\u4e2a\u53cc\u91cd\u6307\u9488\u7ed9 C \u51fd\u6570, \u4f46\u662f\u83b7\u53d6\u7ed3\u679c\u51fa\u9519\u4e86, \u4e5f\u4e0d\u592a\u6e05\u695a\u5177\u4f53\u83b7\u53d6\u7684\u6b65\u9aa4\u5bf9\u4e0d\u5bf9, \u6709\u6ca1\u6709\u5927\u4f6c\u6307\u70b9\u4e0b.<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e C \u53cc\u6307\u9488\u53c2\u6570\u7684 JNA \u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/135515"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=135515"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/135515\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=135515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=135515"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=135515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}