{"id":996776,"date":"2024-12-27T09:17:51","date_gmt":"2024-12-27T01:17:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996776.html"},"modified":"2024-12-27T09:17:53","modified_gmt":"2024-12-27T01:17:53","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8dll","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996776.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u5916\u90e8dll"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072858\/b0ce89e6-246c-4298-a456-009b1ef01f2e.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u5916\u90e8dll\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528\u5916\u90e8DLL\u7684\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528ctypes\u6a21\u5757\u3001\u4f7f\u7528cffi\u5e93\u3001\u901a\u8fc7SWIG\u5de5\u5177\u751f\u6210Python\u63a5\u53e3\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0cctypes\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u9002\u5408\u7b80\u5355\u7684DLL\u8c03\u7528\uff1bcffi\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6027\u80fd\u548c\u5b89\u5168\u6027\uff1bSWIG\u5de5\u5177\u5219\u9002\u5408\u590d\u6742\u7684C\/C++\u5e93\u63a5\u53e3\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001CTYPES\u6a21\u5757<\/p>\n<\/p>\n<p><p>ctypes\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684DLL\u3002\u5b83\u652f\u6301\u52a0\u8f7d\u52a8\u6001\u94fe\u63a5\u5e93\u3001\u8c03\u7528C\u51fd\u6570\u3001\u4f20\u9012\u53c2\u6570\u7b49\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u7b80\u5355\u3001\u76f4\u63a5\u7684DLL\u8c03\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>1.1 \u52a0\u8f7dDLL<\/p>\n<\/p>\n<p><p>\u8981\u8c03\u7528\u5916\u90e8DLL\uff0c\u9996\u5148\u9700\u8981\u52a0\u8f7d\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528ctypes\u7684<code>CDLL<\/code>\u7c7b\u6765\u5b8c\u6210\u8fd9\u4e00\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u52a0\u8f7dDLL<\/strong><\/h2>\n<p>my_dll = ctypes.CDLL(&#39;path_to_dll.dll&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.2 \u8c03\u7528DLL\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u52a0\u8f7dDLL\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5176\u51fd\u6570\u6765\u5b9e\u73b0\u5177\u4f53\u7684\u529f\u80fd\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8c03\u7528\u51fd\u6570\u524d\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b<\/p>\n<p>my_dll.my_function.argtypes = [ctypes.c_int, ctypes.c_double]<\/p>\n<p>my_dll.my_function.restype = ctypes.c_double<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = my_dll.my_function(10, 3.14)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.3 \u5904\u7406\u590d\u6742\u6570\u636e\u7c7b\u578b<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u7ed3\u6784\u4f53\uff0c\u9700\u8981\u5728Python\u4e2d\u4f7f\u7528<code>ctypes<\/code>\u6a21\u5757\u5b9a\u4e49\u76f8\u5e94\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_double)]<\/p>\n<h2><strong>\u4f7f\u7528\u7ed3\u6784\u4f53\u4f5c\u4e3a\u53c2\u6570<\/strong><\/h2>\n<p>my_struct = MyStruct(5, 2.71)<\/p>\n<p>result = my_dll.my_function_with_struct(ctypes.byref(my_struct))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001CFFI\u5e93<\/p>\n<\/p>\n<p><p>CFFI\uff08C Foreign Function Interface for Python\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u8c03\u7528C\u4ee3\u7801\u7684\u5e93\uff0c\u4e0ectypes\u76f8\u6bd4\uff0cCFFI\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u66f4\u597d\u7684\u5b89\u5168\u6027\u3002CFFI\u5206\u4e3a\u4e24\u79cd\u6a21\u5f0f\uff1aABI\u6a21\u5f0f\u548cAPI\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p>2.1 ABI\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>ABI\u6a21\u5f0f\u4e0b\uff0cCFFI\u4e0d\u9700\u8981C\u7f16\u8bd1\u5668\uff0c\u53ea\u9700\u63d0\u4f9bC\u51fd\u6570\u7684\u539f\u578b\u58f0\u660e\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cffi import FFI<\/p>\n<p>ffi = FFI()<\/p>\n<h2><strong>\u58f0\u660e\u51fd\u6570\u539f\u578b<\/strong><\/h2>\n<p>ffi.cdef(&quot;&quot;&quot;<\/p>\n<p>    double my_function(int, double);<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u52a0\u8f7dDLL<\/strong><\/h2>\n<p>my_dll = ffi.dlopen(&#39;path_to_dll.dll&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = my_dll.my_function(10, 3.14)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2 API\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>API\u6a21\u5f0f\u9700\u8981C\u7f16\u8bd1\u5668\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5b9a\u4e49\u590d\u6742\u6570\u636e\u7c7b\u578b\u6216\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cffi import FFI<\/p>\n<p>ffi = FFI()<\/p>\n<h2><strong>\u58f0\u660eC\u6e90\u4ee3\u7801<\/strong><\/h2>\n<p>ffi.set_source(&quot;_my_module&quot;,<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>    #include &quot;my_header.h&quot;<\/p>\n<p>&quot;&quot;&quot;,<\/p>\n<p>    libraries=[&#39;my_library&#39;])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u5757<\/strong><\/h2>\n<p>ffi.compile()<\/p>\n<h2><strong>\u5bfc\u5165\u7f16\u8bd1\u540e\u7684\u6a21\u5757<\/strong><\/h2>\n<p>import _my_module<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = _my_module.lib.my_function(10, 3.14)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001SWIG\u5de5\u5177<\/p>\n<\/p>\n<p><p>SWIG\uff08Simplified Wrapper and Interface Generator\uff09\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u7528\u4e8e\u751f\u6210C\/C++\u5e93\u7684Python\u63a5\u53e3\u3002\u5b83\u9002\u7528\u4e8e\u9700\u8981\u8c03\u7528\u590d\u6742C\/C++\u5e93\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>3.1 \u521b\u5efa\u63a5\u53e3\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3\u6587\u4ef6\uff0c\u4ee5\u63cf\u8ff0C\/C++\u5e93\u7684\u51fd\u6570\u548c\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/* my_module.i *\/<\/p>\n<p>%module my_module<\/p>\n<p>%{<\/p>\n<p>#include &quot;my_header.h&quot;<\/p>\n<p>%}<\/p>\n<p>double my_function(int, double);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2 \u751f\u6210Python\u63a5\u53e3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528SWIG\u5de5\u5177\u751f\u6210Python\u63a5\u53e3\u548cC\/C++\u5305\u88c5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">swig -python -c++ my_module.i<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.3 \u7f16\u8bd1\u751f\u6210\u7684\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u7f16\u8bd1\u751f\u6210\u7684C\/C++\u5305\u88c5\u4ee3\u7801\u5e76\u94fe\u63a5\u5230DLL\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">g++ -shared -o _my_module.so my_module_wrap.cxx -I\/usr\/include\/python3.x -L. -lmy_library<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.4 \u4f7f\u7528\u751f\u6210\u7684\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5bfc\u5165\u751f\u6210\u7684Python\u6a21\u5757\u5e76\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = my_module.my_function(10, 3.14)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>4.1 \u9009\u62e9\u5408\u9002\u7684\u5de5\u5177<\/p>\n<\/p>\n<ul>\n<li>\u5982\u679c\u53ea\u662f\u7b80\u5355\u5730\u8c03\u7528\u5c11\u91cfC\u51fd\u6570\uff0c\u63a8\u8350\u4f7f\u7528<code>ctypes<\/code>\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u8c03\u7528\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u6216\u8ffd\u6c42\u66f4\u9ad8\u6027\u80fd\uff0c<code>cffi<\/code>\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5bf9\u4e8e\u5927\u578bC\/C++\u5e93\u7684\u7ed1\u5b9a\uff0c\u7279\u522b\u662f\u6d89\u53ca\u5927\u91cf\u63a5\u53e3\u7684\u573a\u5408\uff0c<code>SWIG<\/code>\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u5de5\u5177\u3002<\/li>\n<\/ul>\n<p><p>4.2 \u6ce8\u610f\u6570\u636e\u7c7b\u578b\u5339\u914d<\/p>\n<\/p>\n<p><p>\u8c03\u7528C\u51fd\u6570\u65f6\uff0c\u786e\u4fddPython\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u548cC\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u5339\u914d\u3002\u9519\u8bef\u7684\u6570\u636e\u7c7b\u578b\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u6216\u7ed3\u679c\u4e0d\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><p>4.3 \u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u8c03\u7528\u5916\u90e8DLL\u65f6\uff0c\u505a\u597d\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u9632\u6b62\u56e0DLL\u5185\u90e8\u9519\u8bef\u5bfc\u81f4Python\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><p>4.4 \u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5bf9\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u5e94\u7528\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>cffi<\/code>\u7684API\u6a21\u5f0f\u6216\u624b\u52a8\u4f18\u5316SWIG\u751f\u6210\u7684\u4ee3\u7801\uff0c\u4ee5\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u9009\u62e9\u5de5\u5177\u548c\u4f18\u5316\u7b56\u7565\uff0cPython\u53ef\u4ee5\u9ad8\u6548\u5730\u8c03\u7528\u5916\u90e8DLL\uff0c\u5b9e\u73b0\u4e0e\u5176\u4ed6\u8bed\u8a00\u7f16\u5199\u7684\u5e93\u7684\u4e92\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528DLL\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ctypes<\/code>\u6216<code>cffi<\/code>\u5e93\u6765\u52a0\u8f7d\u548c\u8c03\u7528\u5916\u90e8DLL\u6587\u4ef6\u3002<code>ctypes<\/code>\u662fPython\u7684\u5185\u7f6e\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\uff1b\u800c<code>cffi<\/code>\u5219\u9002\u5408\u9700\u8981\u66f4\u590d\u6742\u529f\u80fd\u7684\u7528\u6237\u3002\u8981\u5f00\u59cb\uff0c\u9996\u5148\u9700\u8981\u786e\u4fddDLL\u6587\u4ef6\u7684\u8def\u5f84\u6b63\u786e\uff0c\u7136\u540e\u4f7f\u7528<code>ctypes.CDLL<\/code>\u51fd\u6570\u52a0\u8f7dDLL\uff0c\u5e76\u901a\u8fc7\u5b9a\u4e49\u76f8\u5e94\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u6765\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u8c03\u7528DLL\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6570\u636e\u7c7b\u578b\u7684\u6620\u5c04\uff1f<\/strong><br \/>\u5728\u8c03\u7528DLL\u65f6\uff0c\u786e\u4fddPython\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u4e0eDLL\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\u76f8\u5339\u914d\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5e38\u89c1\u7684\u6620\u5c04\u6709\uff1aPython\u7684<code>int<\/code>\u5bf9\u5e94C\u7684<code>int<\/code>\uff0c<code>float<\/code>\u5bf9\u5e94<code>float<\/code>\uff0c<code>str<\/code>\u9700\u8981\u8f6c\u6362\u4e3a<code>c_char_p<\/code>\u3002\u6b64\u5916\uff0c\u590d\u6742\u6570\u636e\u7ed3\u6784\u5982\u7ed3\u6784\u4f53\u4e5f\u9700\u8981\u901a\u8fc7<code>ctypes<\/code>\u5b9a\u4e49\u76f8\u5e94\u7684Python\u7c7b\u6765\u8fdb\u884c\u6620\u5c04\uff0c\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u4f20\u9012\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406DLL\u8c03\u7528\u7684\u9519\u8bef\u6216\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8c03\u7528DLL\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ctypes.get_last_error()<\/code>\u83b7\u53d6\u6700\u540e\u4e00\u6b21\u8c03\u7528\u7684\u9519\u8bef\u7801\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5904\u7406\u5f02\u5e38\uff0c\u5efa\u8bae\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5e76\u5728\u6355\u83b7\u5230\u9519\u8bef\u65f6\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u6709\u52a9\u4e8e\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u5e76\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528\u5916\u90e8DLL\u7684\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528ctypes\u6a21\u5757\u3001\u4f7f\u7528cffi\u5e93\u3001\u901a\u8fc7SWIG\u5de5\u5177\u751f\u6210Pytho [&hellip;]","protected":false},"author":3,"featured_media":996782,"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\/996776"}],"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=996776"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996776\/revisions"}],"predecessor-version":[{"id":996783,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996776\/revisions\/996783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/996782"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=996776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=996776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=996776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}