{"id":172429,"date":"2020-10-25T20:13:35","date_gmt":"2020-10-25T12:13:35","guid":{"rendered":"http:\/\/4563.org\/?p=172429"},"modified":"2020-10-25T20:13:35","modified_gmt":"2020-10-25T12:13:35","slug":"%e5%85%b3%e4%ba%8e-python-%e5%88%a9%e7%94%a8-ctypes-%e5%af%bc%e5%85%a5-c-%e8%af%ad%e8%a8%80%e5%8a%a8%e6%80%81%e9%93%be%e6%8e%a5%e5%ba%93%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=172429","title":{"rendered":"\u5173\u4e8e Python \u5229\u7528 ctypes \u5bfc\u5165 C \u8bed\u8a00\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e Python \u5229\u7528 ctypes \u5bfc\u5165 C \u8bed\u8a00\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : silencht <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>Linux \uff08 Ubuntu18.04 \uff09\u73af\u5883\uff1a \u6b63\u5728\u505a\u4e00\u4e2a\u9879\u76ee\uff0c\u6253\u7b97\u628a C \u4ee3\u7801\u505a\u6210.so \u52a8\u6001\u5e93\uff0c\u7136\u540e python \u901a\u8fc7 ctypes \u8fde\u94fe\u63a5\u4f7f\u7528\uff0c\u4f46\u662f\u5728\u770b\u6559\u7a0b\uff08 http:\/\/blog.sina.com.cn\/s\/blog_4513dde60100njs8.html \uff09\u7684\u65f6\u5019\u9047\u5230\u4e86\u95ee\u9898\u3002\u5982\u4e0b\uff1a<\/p>\n<p>1.\u6211\u7684 c \u6587\u4ef6\uff0cabc.c<\/p>\n<pre><code>#include &lt;stdlib.h&gt;  typedef struct{   int a;   int b; }mystruct;  mystruct * create(){   mystruct * s = (mystruct *)calloc(1, sizeof(mystruct));   s-&gt;a = 100;   s-&gt;b = 200;   return s; }  void destroy(mystruct * s){   free(s); } <\/code><\/pre>\n<p>\u901a\u8fc7 gcc -fPIC &#8211;shared .\/abc.c -o libabc.so \u7f16\u8bd1\u6210.so \u5e93<\/p>\n<p>2.\u4e0b\u9762\u662f\u6211\u7684 python \u6587\u4ef6\uff0cabc.py<\/p>\n<pre><code>import ctypes as ct class mystruct(ct.Structure):  __fields__=[('a',ct.c_int),('b',ct.c_int)] lib = ct.cdll.LoadLibrary('.\/libabc.so') lib.create.restype=ct.POINTER(mystruct) p = lib.create() p.contents.a p.contents.b <\/code><\/pre>\n<p>3.\u540c\u7ea7\u76ee\u5f55\u4e0b\uff0c\u5229\u7528$ python3 abc.py \u8fd0\u884c\uff0c\u51fa\u73b0\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre><code>Traceback (most recent call last):   File \"abc.py\",line 11,in&lt;modele&gt;       p.contents.a  AttributeError: 'mystruct' object has no attribute 'a' <\/code><\/pre>\n<p>4.QUESTION\uff1a\u4e3a\u5565\u6211\u91cd\u73b0\u4e0d\u4e86\u6559\u7a0b\u91cc\u9762\u7684\u6e90\u7801\u5462\uff1f\u627e\u4e86\u5f88\u591a CSDN \u548c stack flow \u65b9\u9762\u7684\u95ee\u9898\uff0c\u4f46\u4f9d\u7136\u89e3\u51b3\u4e0d\u4e86\u3002\u671b V \u53cb\u4e0d\u541d\u8d50\u6559\uff0c\u4e07\u5206\u611f\u6fc0\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3877329\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gwy15 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             _fields_\uff0c\u4e00\u4e2a\u4e0b\u5212\u7ebf\uff0c\u4f60\u6253\u6210\u4e86\u4e24\u4e2a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3877330\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gwy15 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5199 python \u4e0d\u4e70\u6e38\u6807\u5361\u5c3a\u7684\u5417 XD                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3877331\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : silencht <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gwy15 \u771f\u7684\u592a\u8c22\u8c22\u4f60\u4e86\uff01\u6211\u4e00\u4e0b\u5348\u4e00\u5934\u96fe\u6c34\u3002\u3002\u3002\u3002\u7528 python \u6ca1\u591a\u4e45\uff0c\u88ab__main__\u4e24\u4e2a\u4e0b\u5212\u7ebf\u5e26\u8dd1\u504f\u4e86\u3002\u771f\u7684\u662f\u592a\u611f\u8c22\u4f60\u4e86\uff01\uff08\u662f\u65f6\u5019\u8003\u8651\u4e00\u4e0b\u4e70\u4e00\u4e2a\u6e38\u6807\u5361\u5c3a\u4e86\uff0c\u4eba\u751f\u82e6\u77ed\uff0c\u6211\u7528\u6e38\u6807\u5361\u5c3a&#8230;&#8230;&#8230;&#8230;..                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3877332\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : silencht <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gwy15 \u6211\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\u60f3\u5411\u60a8\u8bf7\u6559\uff0c\u60a8\u662f\u5982\u4f55\u770b\u51fa\u4e24\u4e2a\u4e0b\u5212\u7ebf\u51fa\u73b0\u95ee\u9898\u7684\u5462\uff1f\u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u518d\u5b66\u4e00\u4e0b python \u7684\u54ea\u4e9b\u77e5\u8bc6\u70b9\u52a0\u6df1\u7406\u89e3\uff1f\uff08\u795d\u60a8\u6bcf\u5929\u5f00\u5fc3\uff1a\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3877333\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Huelse <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7a0b\u5e8f\u5458\u5b57\u4f53\u53ef\u4ee5\u5e2e\u4f60\u770b\u51fa\u6765\uff0c\u7a0d\u5fae\u5927\u70b9\u7684\u5e93\u7528 pybind11 \u6765\u6253\u5305\u4f1a\u66f4\u597d\u7528                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3877334\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gwy15 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @silencht \u5176\u5b9e\u6211\u89c9\u5f97\u633a\u660e\u663e\u7684\uff0c\u53ef\u80fd\u4f60\u5c31\u662f\u770b\u82b1\u4e86\u5427\u3002\u4e00\u822c\u53cc\u4e0b\u5212\u7ebf\u5c31\u662f py built-in \u7684\uff0c__name__\/__import__\/__slots__ \u8fd9\u79cd\uff0c\u5355\u4e0b\u5212\u7ebf\u4e00\u822c\u5c31\u662f\u5e93\u7ea7\u522b\u7684\u3002<\/p>\n<p>\u53e6\u5916\u5982\u679c\u4f60\u4e60\u60ef\u7ffb\u6587\u6863\u7684\u8bdd\uff0cctypes.Structure \u4e0b\u9762\u7b2c\u4e00\u4e2a\u52a0\u9ed1\u52a0\u7c97\u7684\u5c31\u662f _fields_ \u7684\u8bf4\u660e\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e Python \u5229\u7528 ctyp&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\/172429"}],"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=172429"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/172429\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=172429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=172429"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=172429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}