{"id":1113758,"date":"2025-01-08T17:51:00","date_gmt":"2025-01-08T09:51:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113758.html"},"modified":"2025-01-08T17:51:02","modified_gmt":"2025-01-08T09:51:02","slug":"python3%e8%b0%83%e7%94%a8c%e5%a6%82%e4%bd%95%e4%bc%a0%e9%80%92%e6%8c%87%e9%92%88buff","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113758.html","title":{"rendered":"python3\u8c03\u7528c\u5982\u4f55\u4f20\u9012\u6307\u9488buff"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075238\/38318e9c-0657-4e2b-958e-fc3962ed253e.webp\" alt=\"python3\u8c03\u7528c\u5982\u4f55\u4f20\u9012\u6307\u9488buff\" \/><\/p>\n<p><p> <strong>Python3 \u8c03\u7528 C \u4ee3\u7801\u5e76\u4f20\u9012\u6307\u9488 buff \u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 ctypes\u3001cffi\u3001cython \u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1actypes\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\u4f7f\u7528 ctypes \u8c03\u7528 C \u51fd\u6570\u5e76\u4f20\u9012\u6307\u9488 buff\uff0c\u6211\u4eec\u9700\u8981\u505a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u7f16\u5199 C \u4ee3\u7801\u3001\u7f16\u8bd1 C \u4ee3\u7801\u4e3a\u5171\u4eab\u5e93\u3001\u5728 Python \u4e2d\u52a0\u8f7d\u5171\u4eab\u5e93\u5e76\u8c03\u7528 C \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7f16\u5199 C \u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684 C \u4ee3\u7801\uff0c\u5047\u8bbe\u8fd9\u4e2a\u4ee3\u7801\u9700\u8981\u4e00\u4e2a\u6307\u5411\u7f13\u51b2\u533a\u7684\u6307\u9488\u5e76\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ mylib.c<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>void process_buffer(char *buffer, int length) {<\/p>\n<p>    for (int i = 0; i &lt; length; i++) {<\/p>\n<p>        buffer[i] = buffer[i] + 1; \/\/ \u7b80\u5355\u5730\u5c06\u6bcf\u4e2a\u5b57\u7b26\u52a01<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u8bd1 C \u4ee3\u7801\u4e3a\u5171\u4eab\u5e93<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u5c06 C \u4ee3\u7801\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff0c\u4ee5\u4fbf Python \u53ef\u4ee5\u4f7f\u7528 ctypes \u52a0\u8f7d\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">gcc -shared -o mylib.so -fPIC mylib.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5728 Python \u4e2d\u52a0\u8f7d\u5171\u4eab\u5e93\u5e76\u8c03\u7528 C \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 ctypes \u52a0\u8f7d\u5171\u4eab\u5e93\u5e76\u8c03\u7528 <code>process_buffer<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u52a0\u8f7d\u5171\u4eab\u5e93<\/strong><\/h2>\n<p>mylib = ctypes.CDLL(&#39;.\/mylib.so&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49 C \u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b<\/strong><\/h2>\n<p>mylib.process_buffer.argtypes = [ctypes.POINTER(ctypes.c_char), ctypes.c_int]<\/p>\n<p>mylib.process_buffer.restype = None<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7f13\u51b2\u533a\u5e76\u4f20\u9012\u7ed9 C \u51fd\u6570<\/strong><\/h2>\n<p>buffer = ctypes.create_string_buffer(b&#39;hello, world&#39;)<\/p>\n<p>length = len(buffer) - 1  # \u53bb\u6389\u672b\u5c3e\u7684\u7a7a\u5b57\u7b26<\/p>\n<p>print(&quot;Before:&quot;, buffer.value)<\/p>\n<h2><strong>\u8c03\u7528 C \u51fd\u6570<\/strong><\/h2>\n<p>mylib.process_buffer(buffer, length)<\/p>\n<p>print(&quot;After:&quot;, buffer.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5 Python \u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>ctypes.CDLL<\/code> \u52a0\u8f7d\u5171\u4eab\u5e93 <code>mylib.so<\/code>\uff0c\u7136\u540e\u5b9a\u4e49 C \u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7f13\u51b2\u533a <code>buffer<\/code> \u5e76\u4f20\u9012\u7ed9 C \u51fd\u6570 <code>process_buffer<\/code>\u3002\u6700\u7ec8\uff0c\u8c03\u7528 C \u51fd\u6570\u5e76\u8f93\u51fa\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u63cf\u8ff0 ctypes<\/h3>\n<\/p>\n<p><p>ctypes \u662f Python \u7684\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u5e93\uff0c\u5b83\u5141\u8bb8\u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u4e2d\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u4e0e C \u8bed\u8a00\u4ea4\u4e92\u7684\u9700\u6c42\uff0cctypes \u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u76f4\u63a5\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u52a0\u8f7d\u5171\u4eab\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>ctypes.CDLL<\/code> \u6216 <code>ctypes.WinDLL<\/code> \u53ef\u4ee5\u52a0\u8f7d\u52a8\u6001\u94fe\u63a5\u5e93\u3002\u5bf9\u4e8e Unix \u7cfb\u7edf\uff0c\u901a\u5e38\u4f7f\u7528 <code>ctypes.CDLL<\/code>\uff1b\u800c\u5bf9\u4e8e Windows \u7cfb\u7edf\uff0c\u4f7f\u7528 <code>ctypes.WinDLL<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mylib = ctypes.CDLL(&#39;.\/mylib.so&#39;)  # Unix<\/p>\n<h2><strong>mylib = ctypes.WinDLL(&#39;mylib.dll&#39;)  # Windows<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u5b9a\u4e49\u51fd\u6570\u539f\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u662f\u4e3a\u4e86\u8ba9 ctypes \u77e5\u9053\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528 <code>argtypes<\/code> \u548c <code>restype<\/code> \u5c5e\u6027\u6765\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mylib.process_buffer.argtypes = [ctypes.POINTER(ctypes.c_char), ctypes.c_int]<\/p>\n<p>mylib.process_buffer.restype = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u521b\u5efa\u7f13\u51b2\u533a<\/strong><\/p>\n<\/p>\n<p><p><code>ctypes.create_string_buffer<\/code> \u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u7b26\u7f13\u51b2\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">buffer = ctypes.create_string_buffer(b&#39;hello, world&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001\u4f20\u9012\u6307\u9488<\/strong><\/p>\n<\/p>\n<p><p>\u5728 C \u4e2d\uff0c\u6307\u9488\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u7528\u4e8e\u6307\u5411\u5185\u5b58\u4e2d\u7684\u4e00\u4e2a\u4f4d\u7f6e\u3002\u5728 ctypes \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ctypes.POINTER<\/code> \u6765\u8868\u793a\u6307\u9488\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mylib.process_buffer(buffer, length)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5\u3001\u8c03\u7528\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u597d\u51fd\u6570\u539f\u578b\u548c\u53c2\u6570\u540e\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 C \u51fd\u6570\u4e86\u3002ctypes \u4f1a\u8d1f\u8d23\u5c06 Python \u4e2d\u7684\u6570\u636e\u8f6c\u6362\u4e3a C \u4e2d\u7684\u7b49\u4ef7\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mylib.process_buffer(buffer, length)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u66f4\u591a ctypes \u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u8c03\u7528\u51fd\u6570\u548c\u4f20\u9012\u6307\u9488\u5916\uff0cctypes \u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u529f\u80fd\uff0c\u4f8b\u5982\u5b9a\u4e49\u7ed3\u6784\u4f53\u3001\u5904\u7406\u56de\u8c03\u51fd\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5b9a\u4e49\u7ed3\u6784\u4f53<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>ctypes.Structure<\/code> \u6765\u5b9a\u4e49 C \u8bed\u8a00\u4e2d\u7684\u7ed3\u6784\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyStruct(ctypes.Structure):<\/p>\n<p>    _fields_ = [(&quot;field1&quot;, ctypes.c_int),<\/p>\n<p>                (&quot;field2&quot;, ctypes.c_float)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u5904\u7406\u56de\u8c03\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>ctypes.CFUNCTYPE<\/code> \u6765\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">CALLBACK = ctypes.CFUNCTYPE(None, ctypes.c_int)<\/p>\n<p>def my_callback(value):<\/p>\n<p>    print(&quot;Callback called with value:&quot;, value)<\/p>\n<p>cb = CALLBACK(my_callback)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528 ctypes\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728 Python \u4e2d\u8c03\u7528 C \u8bed\u8a00\u7684\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u6307\u9488\u7b49\u590d\u6742\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u79cd\u80fd\u529b\u4f7f\u5f97 Python \u53ef\u4ee5\u5229\u7528 C \u8bed\u8a00\u7684\u9ad8\u6027\u80fd\u4ee3\u7801\uff0c\u4ece\u800c\u5728\u9700\u8981\u9ad8\u6548\u5904\u7406\u7684\u573a\u666f\u4e2d\u53d1\u6325\u4f18\u52bf\u3002\u65e0\u8bba\u662f\u5904\u7406\u56fe\u50cf\u3001\u97f3\u9891\uff0c\u8fd8\u662f\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\uff0cctypes \u90fd\u4e3a Python \u63d0\u4f9b\u4e86\u4e00\u6761\u4e0e C \u8bed\u8a00\u65e0\u7f1d\u5bf9\u63a5\u7684\u9014\u5f84\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8c03\u7528C\u51fd\u6570\u5e76\u4f20\u9012\u6307\u9488\u53c2\u6570\uff1f<\/strong><\/p>\n<p>\u8981\u5728Python\u4e2d\u8c03\u7528C\u51fd\u6570\u5e76\u4f20\u9012\u6307\u9488\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528ctypes\u6216cffi\u5e93\u3002ctypes\u5141\u8bb8\u60a8\u52a0\u8f7d\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u5e76\u901a\u8fc7\u5b9a\u4e49C\u51fd\u6570\u7684\u539f\u578b\u6765\u4f20\u9012\u6307\u9488\u3002\u60a8\u9700\u8981\u786e\u4fdd\u5728C\u4ee3\u7801\u4e2d\u6b63\u786e\u5904\u7406\u5185\u5b58\u7ba1\u7406\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u6216\u8bbf\u95ee\u8fdd\u89c4\u3002<\/p>\n<p><strong>\u4f7f\u7528ctypes\u4f20\u9012\u6307\u9488\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528ctypes\u4f20\u9012\u6307\u9488\u65f6\uff0c\u786e\u4fdd\u60a8\u6b63\u786e\u5730\u5b9a\u4e49\u4e86\u6307\u9488\u7c7b\u578b\u3002\u5728Python\u4e2d\uff0c\u60a8\u9700\u8981\u4f7f\u7528ctypes\u63d0\u4f9b\u7684\u7c7b\u578b\uff0c\u5982ctypes.POINTER(ctypes.c_char)\u6765\u5b9a\u4e49\u5b57\u7b26\u6307\u9488\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728C\u4ee3\u7801\u4e2d\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\u65f6\uff0c\u9075\u5faa\u76f8\u5e94\u7684\u89c4\u5219\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406C\u51fd\u6570\u8fd4\u56de\u7684\u6307\u9488\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\u5904\u7406C\u51fd\u6570\u8fd4\u56de\u7684\u6307\u9488\u6570\u636e\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528ctypes\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u8bbf\u95ee\u548c\u8f6c\u6362\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5982\u679cC\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u6574\u6570\u6570\u7ec4\u7684\u6307\u9488\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528ctypes.cast\u5c06\u5176\u8f6c\u6362\u4e3actypes\u6570\u7ec4\u7c7b\u578b\u3002\u786e\u4fdd\u5728\u4f7f\u7528\u8fd4\u56de\u7684\u6570\u636e\u65f6\uff0c\u4e86\u89e3\u5176\u5185\u5b58\u5e03\u5c40\u548c\u5927\u5c0f\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u7684\u5185\u5b58\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python3 \u8c03\u7528 C \u4ee3\u7801\u5e76\u4f20\u9012\u6307\u9488 buff \u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 ctypes\u3001cffi\u3001cython \u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1113764,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113758"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1113758"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113758\/revisions"}],"predecessor-version":[{"id":1113766,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113758\/revisions\/1113766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113764"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}