{"id":1115785,"date":"2025-01-08T18:10:08","date_gmt":"2025-01-08T10:10:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115785.html"},"modified":"2025-01-08T18:10:10","modified_gmt":"2025-01-08T10:10:10","slug":"32%e7%9a%84python%e5%a6%82%e4%bd%95%e7%94%a864%e4%bd%8d%e7%9a%84dll","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115785.html","title":{"rendered":"32\u7684python\u5982\u4f55\u752864\u4f4d\u7684dll"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080410\/95b116b7-b099-4327-9603-8d817ea263cc.webp\" alt=\"32\u7684python\u5982\u4f55\u752864\u4f4d\u7684dll\" \/><\/p>\n<p><p> <strong>\u572832\u4f4d\u7684Python\u4e2d\u4f7f\u752864\u4f4d\u7684DLL\uff1a\u4e0d\u53ef\u80fd\u3001\u9700\u8981\u4f7f\u752832\u4f4d\u7684DLL\u3001\u53ef\u4ee5\u901a\u8fc7\u91cd\u7f16\u8bd1DLL<\/strong><\/p>\n<\/p>\n<p><p>\u572832\u4f4d\u7684Python\u73af\u5883\u4e2d\u76f4\u63a5\u4f7f\u752864\u4f4d\u7684DLL\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u56e0\u4e3a32\u4f4d\u548c64\u4f4d\u7684\u7a0b\u5e8f\u5728\u5185\u5b58\u7ba1\u7406\u548c\u8c03\u7528\u7ea6\u5b9a\u7b49\u65b9\u9762\u5b58\u5728\u6839\u672c\u7684\u5dee\u5f02\u3002\u4e3a\u4e86\u4f7f\u752864\u4f4dDLL\uff0c\u6709\u51e0\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a<strong>\u4f7f\u752832\u4f4d\u7684DLL\u7248\u672c<\/strong>\uff0c<strong>\u91cd\u7f16\u8bd1DLL\u4e3a32\u4f4d\u7248\u672c<\/strong>\uff0c<strong>\u4f7f\u7528\u4e2d\u95f4\u8fdb\u7a0b\u6216\u670d\u52a1\u8fdb\u884c\u8c03\u7528<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u752832\u4f4d\u7684DLL\u7248\u672c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u53ef\u80fd\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u627e\u5230\u6216\u751f\u6210DLL\u768432\u4f4d\u7248\u672c\u3002\u8fd9\u662f\u6700\u76f4\u63a5\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u8de8\u4f4d\u5bbd\u8c03\u7528\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u83b7\u53d632\u4f4dDLL\u7248\u672c\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u786e\u8ba4DLL\u662f\u5426\u670932\u4f4d\u7248\u672c<\/strong>\uff1a\u8bb8\u591a\u5e93\u63d0\u4f9b\u4e8632\u4f4d\u548c64\u4f4d\u7684\u7248\u672c\uff0c\u68c0\u67e5\u5f00\u53d1\u8005\u7684\u5b98\u65b9\u8d44\u6e90\u3002<\/li>\n<li><strong>\u4e0b\u8f7d\u6216\u7f16\u8bd132\u4f4dDLL<\/strong>\uff1a\u5982\u679c\u5f00\u53d1\u8005\u63d0\u4f9b\u4e8632\u4f4d\u7248\u672c\uff0c\u76f4\u63a5\u4e0b\u8f7d\u5373\u53ef\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5c1d\u8bd5\u83b7\u53d6\u6e90\u4ee3\u7801\u5e76\u81ea\u884c\u7f16\u8bd1\u3002<\/li>\n<li><strong>\u66ff\u6362DLL<\/strong>\uff1a\u5c0664\u4f4dDLL\u66ff\u6362\u4e3a32\u4f4dDLL\uff0c\u5e76\u786e\u4fdd\u8def\u5f84\u914d\u7f6e\u6b63\u786e\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u91cd\u7f16\u8bd1DLL\u4e3a32\u4f4d\u7248\u672c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709DLL\u7684\u6e90\u4ee3\u7801\uff0c\u91cd\u7f16\u8bd1\u4e3a32\u4f4d\u7248\u672c\u662f\u4e00\u4e2a\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\u3002\u9700\u8981\u51c6\u5907\u4e00\u4e2a32\u4f4d\u7684\u5f00\u53d1\u73af\u5883\uff0c\u5e76\u8fdb\u884c\u7f16\u8bd1\u3002<\/p>\n<\/p>\n<p><p><strong>\u91cd\u7f16\u8bd132\u4f4dDLL\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u51c6\u590732\u4f4d\u5f00\u53d1\u73af\u5883<\/strong>\uff1a\u5b89\u88c532\u4f4d\u7248\u672c\u7684\u7f16\u8bd1\u5668\u548c\u76f8\u5173\u5de5\u5177\u94fe\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Visual Studio\u53ef\u4ee5\u9009\u62e932\u4f4d\u7f16\u8bd1\u9009\u9879\u3002<\/li>\n<li><strong>\u914d\u7f6e\u7f16\u8bd1\u9009\u9879<\/strong>\uff1a\u5728\u9879\u76ee\u8bbe\u7f6e\u4e2d\uff0c\u5c06\u76ee\u6807\u5e73\u53f0\u8bbe\u7f6e\u4e3ax86\uff0832\u4f4d\uff09\u3002<\/li>\n<li><strong>\u7f16\u8bd1\u5e76\u751f\u6210DLL<\/strong>\uff1a\u8fdb\u884c\u7f16\u8bd1\uff0c\u751f\u621032\u4f4d\u7248\u672c\u7684DLL\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4e2d\u95f4\u8fdb\u7a0b\u6216\u670d\u52a1\u8fdb\u884c\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u65e0\u6cd5\u627e\u5230\u6216\u91cd\u7f16\u8bd132\u4f4dDLL\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u4e2a\u4e2d\u95f4\u8fdb\u7a0b\u6216\u670d\u52a1\u6765\u8c03\u752864\u4f4dDLL\u300232\u4f4dPython\u53ef\u4ee5\u901a\u8fc7\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u4e0e64\u4f4d\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u4e2d\u95f4\u8fdb\u7a0b\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u4e2d\u95f4\u8fdb\u7a0b<\/strong>\uff1a\u7f16\u5199\u4e00\u4e2a64\u4f4d\u7684\u4e2d\u95f4\u8fdb\u7a0b\uff0c\u8be5\u8fdb\u7a0b\u8d1f\u8d23\u52a0\u8f7d64\u4f4dDLL\u5e76\u63d0\u4f9b\u63a5\u53e3\u3002<\/li>\n<li><strong>\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/strong>\uff1a\u4f7f\u7528IPC\u673a\u5236\uff0c\u4f8b\u5982\u6d88\u606f\u961f\u5217\u3001\u547d\u540d\u7ba1\u9053\u3001\u7f51\u7edc\u901a\u4fe1\u7b49\uff0c\u6765\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002<\/li>\n<li><strong>\u8c03\u7528\u63a5\u53e3<\/strong>\uff1a\u572832\u4f4dPython\u4e2d\uff0c\u901a\u8fc7IPC\u673a\u5236\u8c03\u7528\u4e2d\u95f4\u8fdb\u7a0b\u7684\u63a5\u53e3\uff0c\u95f4\u63a5\u4f7f\u752864\u4f4dDLL\u7684\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u793a\u4f8b\u4ee3\u7801\u4e0e\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u591a\u8fdb\u7a0b\u548c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u572832\u4f4dPython\u4e2d\u8c03\u752864\u4f4dDLL\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4e2d\u95f4\u8fdb\u7a0b (64\u4f4d)<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u768464\u4f4dPython\u811a\u672c\uff0c\u8d1f\u8d23\u8c03\u752864\u4f4dDLL\u5e76\u63d0\u4f9b\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># middle_process.py<\/p>\n<p>import ctypes<\/p>\n<p>from multiprocessing.connection import Listener<\/p>\n<p>class DLLWrapper:<\/p>\n<p>    def __init__(self, dll_path):<\/p>\n<p>        self.dll = ctypes.CDLL(dll_path)<\/p>\n<p>    def some_function(self, arg):<\/p>\n<p>        # \u5047\u8bbeDLL\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3asome_function\u7684\u51fd\u6570<\/p>\n<p>        result = self.dll.some_function(ctypes.c_int(arg))<\/p>\n<p>        return result<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    dll_wrapper = DLLWrapper(&quot;path_to_64bit_dll.dll&quot;)<\/p>\n<p>    address = (&#39;localhost&#39;, 6000)  # IPC\u5730\u5740<\/p>\n<p>    listener = Listener(address, authkey=b&#39;secret&#39;)<\/p>\n<p>    while True:<\/p>\n<p>        conn = listener.accept()<\/p>\n<p>        try:<\/p>\n<p>            while True:<\/p>\n<p>                msg = conn.recv()<\/p>\n<p>                if msg == &#39;exit&#39;:<\/p>\n<p>                    break<\/p>\n<p>                else:<\/p>\n<p>                    arg = int(msg)<\/p>\n<p>                    result = dll_wrapper.some_function(arg)<\/p>\n<p>                    conn.send(result)<\/p>\n<p>        except EOFError:<\/p>\n<p>            conn.close()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4e3b\u8fdb\u7a0b (32\u4f4d)<\/h4>\n<\/p>\n<p><p>\u572832\u4f4dPython\u4e2d\uff0c\u901a\u8fc7IPC\u4e0e\u4e2d\u95f4\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main_process.py<\/p>\n<p>from multiprocessing.connection import Client<\/p>\n<p>def call_dll_function(arg):<\/p>\n<p>    address = (&#39;localhost&#39;, 6000)<\/p>\n<p>    conn = Client(address, authkey=b&#39;secret&#39;)<\/p>\n<p>    conn.send(arg)<\/p>\n<p>    result = conn.recv()<\/p>\n<p>    conn.close()<\/p>\n<p>    return result<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    arg = 10<\/p>\n<p>    result = call_dll_function(arg)<\/p>\n<p>    print(f&quot;Result from DLL: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u572832\u4f4d\u7684Python\u4e2d\u76f4\u63a5\u4f7f\u752864\u4f4d\u7684DLL\u662f\u4e0d\u53ef\u80fd\u7684<\/strong>\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u89e3\u51b3\uff1a<strong>\u4f7f\u752832\u4f4d\u7684DLL\u7248\u672c\u3001\u91cd\u7f16\u8bd1DLL\u4e3a32\u4f4d\u7248\u672c\u3001\u4f7f\u7528\u4e2d\u95f4\u8fdb\u7a0b\u6216\u670d\u52a1\u8fdb\u884c\u8c03\u7528<\/strong>\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u627e\u5230\u6216\u751f\u6210DLL\u768432\u4f4d\u7248\u672c\uff0c\u5982\u679c\u4e0d\u884c\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u7f16\u8bd1DLL\u6216\u901a\u8fc7\u8fdb\u7a0b\u95f4\u901a\u4fe1\u95f4\u63a5\u8c03\u752864\u4f4dDLL\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9879\u76ee\u9700\u6c42\u548c\u73af\u5883\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u572832\u4f4dPython\u4e2d\u52a0\u8f7d64\u4f4d\u7684DLL\uff1f<\/strong><br \/>\u572832\u4f4dPython\u73af\u5883\u4e2d\uff0c\u76f4\u63a5\u52a0\u8f7d64\u4f4d\u7684DLL\u6587\u4ef6\u662f\u4e0d\u53ef\u884c\u7684\uff0c\u56e0\u4e3a32\u4f4d\u548c64\u4f4d\u7684\u8fdb\u7a0b\u4e4b\u95f4\u4e0d\u517c\u5bb9\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u752864\u4f4d\u7684DLL\uff0c\u5efa\u8bae\u5c06Python\u73af\u5883\u5347\u7ea7\u523064\u4f4d\u7248\u672c\uff0c\u786e\u4fdd\u5b83\u4e0eDLL\u7684\u4f4d\u6570\u5339\u914d\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u66ff\u4ee3\u65b9\u6848\u6765\u4f7f\u752864\u4f4dDLL\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u53ef\u4ee5\u8003\u8651\u7f16\u5199\u4e00\u4e2a\u4e2d\u95f4\u5c42\uff0c\u4f7f\u752864\u4f4d\u7684\u5e94\u7528\u7a0b\u5e8f\u6765\u8c03\u7528DLL\u529f\u80fd\uff0c\u7136\u540e\u901a\u8fc7\u7f51\u7edc\u3001IPC\uff08\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff09\u6216\u6587\u4ef6\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u589e\u52a0\u4e86\u590d\u6742\u6027\uff0c\u4f46\u53ef\u4ee5\u8ba9\u4f60\u572832\u4f4d\u73af\u5883\u4e2d\u95f4\u63a5\u4f7f\u752864\u4f4dDLL\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u6211\u7684Python\u73af\u5883\u662f32\u4f4d\u8fd8\u662f64\u4f4d\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5728Python\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\u6765\u68c0\u67e5\uff1a  <\/p>\n<pre><code class=\"language-python\">import platform\nprint(platform.architecture())\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542bPython\u7684\u4f4d\u6570\u4fe1\u606f\u3002\u5982\u679c\u8fd4\u56de\u7ed3\u679c\u663e\u793a\u201c32bit\u201d\uff0c\u5219\u8868\u660e\u4f60\u7684Python\u73af\u5883\u662f32\u4f4d\u7684\uff0c\u53cd\u4e4b\u5219\u4e3a64\u4f4d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u572832\u4f4d\u7684Python\u4e2d\u4f7f\u752864\u4f4d\u7684DLL\uff1a\u4e0d\u53ef\u80fd\u3001\u9700\u8981\u4f7f\u752832\u4f4d\u7684DLL\u3001\u53ef\u4ee5\u901a\u8fc7\u91cd\u7f16\u8bd1DLL \u572832\u4f4d\u7684P [&hellip;]","protected":false},"author":3,"featured_media":1115789,"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\/1115785"}],"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=1115785"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115785\/revisions"}],"predecessor-version":[{"id":1115793,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115785\/revisions\/1115793"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115789"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}