{"id":960096,"date":"2024-12-27T03:46:40","date_gmt":"2024-12-26T19:46:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960096.html"},"modified":"2024-12-27T03:46:42","modified_gmt":"2024-12-26T19:46:42","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8dll%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960096.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528dll\u7ec4\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102722\/4e119c96-789a-4be0-b849-6bef90ec97ba.webp\" alt=\"python\u5982\u4f55\u8c03\u7528dll\u7ec4\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u7528DLL\u7ec4\u4ef6\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528ctypes\u5e93\u3001\u4f7f\u7528cffi\u5e93\u3001\u901a\u8fc7Python\u7684\u7b2c\u4e09\u65b9\u5e93\u5982pywin32\u3002<\/strong>\u5176\u4e2d\uff0cctypes\u5e93\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528ctypes\u5e93\u8c03\u7528DLL\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001CTYPES\u5e93\u4ecb\u7ecd<\/p>\n<\/p>\n<p><p>ctypes\u662fPython\u7684\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u5e93\uff0c\u5b83\u5141\u8bb8\u5728Python\u4e2d\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\u6216\u5171\u4eab\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86C\u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u53ef\u4ee5\u52a8\u6001\u52a0\u8f7dDLL\u6587\u4ef6\u3002\u4f7f\u7528ctypes\u5e93\u7684\u4f18\u70b9\u662f\u5b83\u4e0d\u9700\u8981\u7f16\u5199\u4efb\u4f55C\u8bed\u8a00\u4ee3\u7801\uff0c\u6240\u6709\u64cd\u4f5c\u90fd\u53ef\u4ee5\u5728Python\u4e2d\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li>\u52a0\u8f7dDLL\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528ctypes\u8c03\u7528DLL\uff0c\u9996\u5148\u9700\u8981\u52a0\u8f7dDLL\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528ctypes.windll\u6216ctypes.cdll\u6765\u52a0\u8f7dDLL\u6587\u4ef6\uff1a<\/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<ol start=\"2\">\n<li>\u5b9a\u4e49\u51fd\u6570\u539f\u578b<\/li>\n<\/ol>\n<p><p>\u5728\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eargtypes\u548crestype\u5c5e\u6027\u6765\u5b8c\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u51fd\u6570\u539f\u578b<\/p>\n<p>my_function = my_dll.MyFunction<\/p>\n<p>my_function.argtypes = [ctypes.c_int, ctypes.c_double]  # \u53c2\u6570\u7c7b\u578b<\/p>\n<p>my_function.restype = ctypes.c_double  # \u8fd4\u56de\u7c7b\u578b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u8c03\u7528DLL\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u4e00\u65e6\u51fd\u6570\u539f\u578b\u5b9a\u4e49\u5b8c\u6bd5\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u7528\u51fd\u6570<\/p>\n<p>result = my_function(10, 20.5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528CFFI\u5e93<\/p>\n<\/p>\n<p><p>CFFI\uff08C Foreign Function Interface\uff09\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5728Python\u4e2d\u8c03\u7528C\u8bed\u8a00\u5e93\u7684\u5de5\u5177\u3002\u4e0ectypes\u4e0d\u540c\uff0cCFFI\u9700\u8981\u7f16\u5199\u4e00\u4e9bC\u4ee3\u7801\u6765\u58f0\u660e\u51fd\u6570\u63a5\u53e3\u3002\u5c3d\u7ba1\u5982\u6b64\uff0cCFFI\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u66f4\u597d\u7684C\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5CFFI<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528CFFI\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cffi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528CFFI\u8c03\u7528DLL<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528CFFI\u8c03\u7528DLL\uff1a<\/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\u63a5\u53e3<\/strong><\/h2>\n<p>ffi.cdef(&quot;&quot;&quot;<\/p>\n<p>    double MyFunction(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.MyFunction(10, 20.5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PYWIN32\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8eWindows\u7528\u6237\uff0cpywin32\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u4e0eWindows API\u8fdb\u884c\u4ea4\u4e92\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u5bfc\u51faCOM\u63a5\u53e3\u7684DLL\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pywin32<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5pywin32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528pywin32\u8c03\u7528DLL<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pywin32\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u901a\u5e38\u9700\u8981\u5148\u83b7\u53d6DLL\u7684COM\u63a5\u53e3\uff0c\u7136\u540e\u8c03\u7528\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.client<\/p>\n<h2><strong>\u521b\u5efaCOM\u5bf9\u8c61<\/strong><\/h2>\n<p>com_object = win32com.client.Dispatch(&quot;YourCOMObject.ProgID&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>result = com_object.YourMethod(10, 20.5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fddDLL\u6587\u4ef6\u8def\u5f84\u6b63\u786e<\/strong>\uff1a\u5728\u52a0\u8f7dDLL\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u63d0\u4f9b\u7684\u8def\u5f84\u662f\u6b63\u786e\u7684\u3002\u5982\u679cDLL\u6587\u4ef6\u4e0d\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5339\u914d\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b<\/strong>\uff1a\u5728\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u65f6\uff0c\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u4e0eDLL\u4e2d\u51fd\u6570\u7684\u5b9e\u9645\u7c7b\u578b\u5339\u914d\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong>\uff1a\u5728\u8c03\u7528DLL\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u51fd\u6570\u672a\u5b9a\u4e49\u6216\u53c2\u6570\u9519\u8bef\u3002\u5efa\u8bae\u5728\u8c03\u7528\u65f6\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e73\u53f0\u517c\u5bb9\u6027<\/strong>\uff1actypes\u548cCFFI\u90fd\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u4f46pywin32\u4ec5\u9002\u7528\u4e8eWindows\u7cfb\u7edf\u3002\u5728\u9009\u62e9\u5e93\u65f6\uff0c\u8bf7\u8003\u8651\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u8c03\u7528DLL\u7ec4\u4ef6\uff0c\u5b9e\u73b0\u4e0eC\u8bed\u8a00\u5e93\u7684\u4ea4\u4e92\u3002\u8fd9\u4e3aPython\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u6269\u5c55\u548c\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u52a0\u8f7d\u548c\u4f7f\u7528DLL\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u52a0\u8f7d\u548c\u4f7f\u7528DLL\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ctypes<\/code>\u6216<code>cffi<\/code>\u5e93\u3002\u8fd9\u4e24\u4e2a\u5e93\u5141\u8bb8Python\u4e0eC\u8bed\u8a00\u7f16\u5199\u7684DLL\u6587\u4ef6\u8fdb\u884c\u4ea4\u4e92\u3002<code>ctypes<\/code>\u662fPython\u81ea\u5e26\u7684\u5e93\uff0c\u800c<code>cffi<\/code>\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002\u4f7f\u7528<code>ctypes<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ctypes.WinDLL<\/code>\u6216<code>ctypes.CDLL<\/code>\u6765\u52a0\u8f7dDLL\u6587\u4ef6\uff0c\u7136\u540e\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002\u786e\u4fddDLL\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e86\u89e3\u6240\u9700\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u4ee5\u4fbf\u6b63\u786e\u4f20\u9012\u3002<\/p>\n<p><strong>\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u53c2\u6570\u7c7b\u578b\uff1f<\/strong><br \/>\u5728\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u65f6\uff0c\u4e86\u89e3\u53c2\u6570\u7684\u6570\u636e\u7c7b\u578b\u975e\u5e38\u91cd\u8981\u3002<code>ctypes<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5982<code>ctypes.c_int<\/code>\u3001<code>ctypes.c_double<\/code>\u7b49\uff0c\u53ef\u4ee5\u4e0eC\u8bed\u8a00\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u76f8\u5bf9\u5e94\u3002\u786e\u4fdd\u5728\u8c03\u7528\u51fd\u6570\u524d\uff0c\u4f7f\u7528\u9002\u5f53\u7684<code>ctypes<\/code>\u7c7b\u578b\u8fdb\u884c\u58f0\u660e\uff0c\u4ee5\u907f\u514d\u7c7b\u578b\u4e0d\u5339\u914d\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406DLL\u51fd\u6570\u8c03\u7528\u540e\u7684\u8fd4\u56de\u503c\uff1f<\/strong><br \/>\u8c03\u7528DLL\u51fd\u6570\u540e\uff0c\u8fd4\u56de\u503c\u7684\u5904\u7406\u65b9\u5f0f\u53d6\u51b3\u4e8e\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u5728\u4f7f\u7528<code>ctypes<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u7684\u8bbe\u7f6e\u6765\u6b63\u786e\u5904\u7406\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u65f6\u6307\u5b9a<code>restype<\/code>\u5c5e\u6027\u4e3a<code>ctypes.c_int<\/code>\u3002\u8fd9\u6837\uff0c\u8c03\u7528\u540e\u5c31\u53ef\u4ee5\u76f4\u63a5\u83b7\u5f97\u4e00\u4e2a\u6574\u6570\u503c\u3002\u5982\u679c\u8fd4\u56de\u7684\u662f\u6307\u9488\u6216\u7ed3\u6784\u4f53\u7c7b\u578b\uff0c\u5219\u9700\u8981\u4f7f\u7528\u76f8\u5e94\u7684<code>ctypes<\/code>\u7c7b\u578b\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u786e\u4fdd\u5728\u4f7f\u7528\u4e4b\u524d\u6b63\u786e\u5206\u914d\u5185\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528DLL\u7ec4\u4ef6\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528ctypes\u5e93\u3001\u4f7f\u7528cffi\u5e93\u3001\u901a\u8fc7Python\u7684\u7b2c\u4e09\u65b9\u5e93\u5982p [&hellip;]","protected":false},"author":3,"featured_media":960102,"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\/960096"}],"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=960096"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960096\/revisions"}],"predecessor-version":[{"id":960106,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960096\/revisions\/960106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960102"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}