{"id":995060,"date":"2024-12-27T09:01:16","date_gmt":"2024-12-27T01:01:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995060.html"},"modified":"2024-12-27T09:01:19","modified_gmt":"2024-12-27T01:01:19","slug":"python%e5%a6%82%e4%bd%95%e5%b0%81%e8%a3%85dll%e8%a7%86%e9%a2%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/995060.html","title":{"rendered":"python\u5982\u4f55\u5c01\u88c5dll\u89c6\u9891"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071816\/114ebd94-d346-4aa6-a8f7-9461cef7af19.webp\" alt=\"python\u5982\u4f55\u5c01\u88c5dll\u89c6\u9891\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5c01\u88c5DLL\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efaC\/C++\u4ee3\u7801\u3001\u7f16\u8bd1\u6210DLL\u6587\u4ef6\u3001\u4f7f\u7528ctypes\u6216cffi\u52a0\u8f7dDLL\u3002<\/strong>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8bf4\u660e\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u6765\u5b9e\u73b0Python\u5c01\u88c5DLL\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efaC\/C++\u4ee3\u7801<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f16\u5199C\/C++\u51fd\u6570\uff1a<\/strong>\u9996\u5148\uff0c\u9700\u8981\u7528C\u6216C++\u7f16\u5199\u4f60\u5e0c\u671b\u5c01\u88c5\u6210DLL\u7684\u51fd\u6570\u3002\u786e\u4fdd\u8fd9\u4e9b\u51fd\u6570\u7684\u63a5\u53e3\u662f\u660e\u786e\u7684\uff0c\u5e76\u4e14\u53ef\u4ee5\u88ab\u5916\u90e8\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u6cd5\u51fd\u6570\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-c\">\/\/ example.c<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>__declspec(dllexport) int add(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b9a\u4e49\u5bfc\u51fa\u7b26\u53f7\uff1a<\/strong>\u4f7f\u7528<code>__declspec(dllexport)<\/code>\u6765\u5b9a\u4e49\u54ea\u4e9b\u51fd\u6570\u5c06\u88ab\u5bfc\u51fa\u5230DLL\u4e2d\uff0c\u8fd9\u6837\u8fd9\u4e9b\u51fd\u6570\u5c31\u53ef\u4ee5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u8c03\u7528\u3002<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u7f16\u8bd1\u6210DLL\u6587\u4ef6<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u7f16\u8bd1\u5de5\u5177\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u5982GCC\uff08MinGW\u5728Windows\u4e0a\uff09\u6216Visual Studio\u7b49\u7f16\u8bd1\u5de5\u5177\u5c06C\/C++\u4ee3\u7801\u7f16\u8bd1\u6210DLL\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u8bd1\u8fc7\u7a0b\uff1a<\/strong>\u4f8b\u5982\uff0c\u4f7f\u7528GCC\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">gcc -shared -o example.dll example.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6761\u547d\u4ee4\u4f1a\u5c06<code>example.c<\/code>\u7f16\u8bd1\u6210<code>example.dll<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528ctypes\u52a0\u8f7dDLL<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165ctypes\u6a21\u5757\uff1a<\/strong>\u5728Python\u4e2d\uff0c<code>ctypes<\/code>\u6a21\u5757\u5141\u8bb8\u60a8\u52a0\u8f7dDLL\u5e76\u8c03\u7528\u5176\u51fd\u6570\u3002\u9996\u5148\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u52a0\u8f7dDLL\u6587\u4ef6\uff1a<\/strong>\u4f7f\u7528<code>ctypes.CDLL<\/code>\u52a0\u8f7d\u521a\u521a\u7f16\u8bd1\u7684DLL\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">example_dll = ctypes.CDLL(&#39;.\/example.dll&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\uff1a<\/strong>\u901a\u8fc7<code>ctypes<\/code>\uff0c\u53ef\u4ee5\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u3002\u9700\u8981\u6307\u5b9a\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u4ee5\u786e\u4fdd\u6b63\u786e\u8c03\u7528\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6307\u5b9a\u8fd4\u56de\u7c7b\u578b<\/p>\n<p>example_dll.add.restype = ctypes.c_int<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = example_dll.add(5, 3)<\/p>\n<p>print(f&quot;Result: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528cffi\u52a0\u8f7dDLL<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5cffi\u5e93\uff1a<\/strong>\u5982\u679c\u9009\u62e9\u4f7f\u7528<code>cffi<\/code>\u6a21\u5757\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install cffi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bfc\u5165\u5e76\u8bbe\u7f6ecffi\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import cffi<\/p>\n<p>ffi = cffi.FFI()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b9a\u4e49\u51fd\u6570\u63a5\u53e3\uff1a<\/strong>\u901a\u8fc7<code>ffi.cdef()<\/code>\u5b9a\u4e49DLL\u4e2d\u51fd\u6570\u7684\u63a5\u53e3\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">ffi.cdef(&quot;&quot;&quot;<\/p>\n<p>    int add(int a, int b);<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u52a0\u8f7dDLL\u5e76\u8c03\u7528\u51fd\u6570\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7dDLL<\/p>\n<p>example_dll = ffi.dlopen(&#39;.\/example.dll&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = example_dll.add(5, 3)<\/p>\n<p>print(f&quot;Result: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fddDLL\u7684\u8def\u5f84\u6b63\u786e\uff1a<\/strong>\u5728\u52a0\u8f7dDLL\u65f6\uff0c\u786e\u4fdd\u6307\u5b9a\u7684\u8def\u5f84\u6b63\u786e\u3002\u5982\u679cDLL\u4e0d\u5728\u5f53\u524d\u76ee\u5f55\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u5b8c\u6574\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u5339\u914d\uff1a<\/strong>\u786e\u4fdd\u5728\u4f7f\u7528<code>ctypes<\/code>\u65f6\uff0c\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u4e0eC\/C++\u4ee3\u7801\u4e2d\u7684\u5b9a\u4e49\u5339\u914d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong>\u5728\u8c03\u7528DLL\u51fd\u6570\u65f6\uff0c\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u4ee3\u7801\u4ee5\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\u6216\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6269\u5c55\u5e94\u7528<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5904\u7406\u590d\u6742\u6570\u636e\u7c7b\u578b\uff1a<\/strong>\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u7c7b\u578b\uff0c\u5982\u7ed3\u6784\u4f53\u3001\u6570\u7ec4\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ctypes<\/code>\u6216<code>cffi<\/code>\u5b9a\u4e49\u5bf9\u5e94\u7684Python\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u5e73\u53f0\u652f\u6301\uff1a<\/strong>\u5982\u679c\u9700\u8981\u652f\u6301\u591a\u4e2a\u5e73\u53f0\uff0c\u6ce8\u610f\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u7684DLL\u7f16\u8bd1\u548c\u52a0\u8f7d\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316\uff1a<\/strong>\u5728\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316C\/C++\u4ee3\u7801\u6216\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7f16\u8bd1\u5668\u9009\u9879\u6765\u63d0\u9ad8DLL\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6210\u529f\u5c01\u88c5\u548c\u4f7f\u7528DLL\u6587\u4ef6\uff0c\u5229\u7528C\/C++\u7684\u9ad8\u6027\u80fd\u8ba1\u7b97\u80fd\u529b\u6765\u589e\u5f3aPython\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u8c03\u7528DLL\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8c03\u7528DLL\u6587\u4ef6\u901a\u5e38\u4f7f\u7528<code>ctypes<\/code>\u6216<code>cffi<\/code>\u5e93\u3002<code>ctypes<\/code>\u662fPython\u81ea\u5e26\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u52a0\u8f7d\u548c\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684DLL\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u7136\u540e\u4f7f\u7528<code>ctypes.CDLL()<\/code>\u52a0\u8f7dDLL\uff0c\u63a5\u7740\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u51fd\u6570\u540d\u79f0\u6765\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u3002\u786e\u4fdd\u4f60\u4e86\u89e3DLL\u4e2d\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6b63\u786e\u7684\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p><strong>2. \u5c01\u88c5DLL\u4e3aPython\u6a21\u5757\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5c06DLL\u5c01\u88c5\u4e3aPython\u6a21\u5757\u7684\u8fc7\u7a0b\u5305\u62ec\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u4f7f\u7528<code>ctypes<\/code>\u6216<code>cffi<\/code>\u52a0\u8f7dDLL\uff1b\u63a5\u4e0b\u6765\uff0c\u5b9a\u4e49Python\u51fd\u6570\u5e76\u5c06DLL\u4e2d\u7684\u51fd\u6570\u6620\u5c04\u5230\u8fd9\u4e9bPython\u51fd\u6570\u4e0a\u3002\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6216\u7c7b\u5c01\u88c5\u8fd9\u4e9b\u529f\u80fd\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u4f7f\u7528\u3002\u6700\u540e\uff0c\u7f16\u8bd1\u548c\u6d4b\u8bd5\u8fd9\u4e9b\u6a21\u5757\uff0c\u786e\u4fdd\u5b83\u4eec\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u5e76\u8fd4\u56de\u9884\u671f\u7ed3\u679c\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u63a8\u8350\u7684\u5de5\u5177\u6216\u5e93\u6765\u7b80\u5316DLL\u5c01\u88c5\u8fc7\u7a0b\uff1f<\/strong><br \/>\u6709\u51e0\u4e2a\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316DLL\u5c01\u88c5\u8fc7\u7a0b\u3002<code>pybind11<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06C++\u4ee3\u7801\uff08\u5305\u62ecDLL\uff09\u4e0ePython\u8fdb\u884c\u7ed1\u5b9a\u3002<code>SWIG<\/code>\uff08Simplified Wrapper and Interface Generator\uff09\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u80fd\u591f\u81ea\u52a8\u751f\u6210Python\u7ed1\u5b9a\u4ee3\u7801\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u51cf\u5c11\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u7684\u5de5\u4f5c\u91cf\uff0c\u5e76\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5c01\u88c5DLL\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efaC\/C++\u4ee3\u7801\u3001\u7f16\u8bd1\u6210DLL\u6587\u4ef6\u3001\u4f7f\u7528ctypes\u6216cffi\u52a0\u8f7dDL [&hellip;]","protected":false},"author":3,"featured_media":995064,"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\/995060"}],"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=995060"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995060\/revisions"}],"predecessor-version":[{"id":995066,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995060\/revisions\/995066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/995064"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=995060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=995060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=995060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}