{"id":929726,"date":"2024-12-26T17:01:20","date_gmt":"2024-12-26T09:01:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/929726.html"},"modified":"2024-12-26T17:01:21","modified_gmt":"2024-12-26T09:01:21","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96dll","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/929726.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6dll"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064821\/6d373767-f54c-4b59-ba50-a1d1be758d16.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6dll\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u83b7\u53d6DLL\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528ctypes\u5e93\u3001\u901a\u8fc7\u7b2c\u4e09\u65b9\u5de5\u5177\u548c\u5e93\u3001\u89e3\u6790Windows\u6ce8\u518c\u8868\u7b49\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684ctypes\u5e93\u6765\u52a0\u8f7d\u548c\u8c03\u7528DLL\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\u6765\u4e0eC\u8bed\u8a00\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001CTYPES\u5e93\u52a0\u8f7dDLL<\/p>\n<\/p>\n<p><p><code>ctypes<\/code>\u662fPython\u7684\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u5e93\uff0c\u5b83\u5141\u8bb8\u4f60\u52a0\u8f7dDLL\u6587\u4ef6\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002\u901a\u8fc7ctypes\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u52a0\u8f7d\u52a8\u6001\u94fe\u63a5\u5e93\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528ctypes\u52a0\u8f7dDLL<\/li>\n<\/ol>\n<p><p>\u8981\u52a0\u8f7dDLL\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u77e5\u9053DLL\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u5176\u5bfc\u51fa\u7684\u51fd\u6570\u540d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u52a0\u8f7dDLL\u6587\u4ef6<\/strong><\/h2>\n<p>my_dll = ctypes.CDLL(&#39;path_to_your_dll.dll&#39;)<\/p>\n<h2><strong>\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>result = my_dll.your_function_name(arguments)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>ctypes.CDLL()<\/code>\u7528\u4e8e\u52a0\u8f7dDLL\uff0c<code>your_function_name<\/code>\u662fDLL\u4e2d\u5bfc\u51fa\u7684\u51fd\u6570\u7684\u540d\u79f0\uff0c<code>arguments<\/code>\u662f\u8be5\u51fd\u6570\u6240\u9700\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5904\u7406DLL\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u548c\u53c2\u6570\u7c7b\u578b<\/li>\n<\/ol>\n<p><p>\u5728\u8c03\u7528DLL\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u548c\u53c2\u6570\u7c7b\u578b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>restype<\/code>\u548c<code>argtypes<\/code>\u5c5e\u6027\u6765\u6307\u5b9a\u8fd9\u4e9b\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6307\u5b9a\u8fd4\u56de\u7c7b\u578b<\/p>\n<p>my_dll.your_function_name.restype = ctypes.c_int<\/p>\n<h2><strong>\u6307\u5b9a\u53c2\u6570\u7c7b\u578b<\/strong><\/h2>\n<p>my_dll.your_function_name.argtypes = [ctypes.c_int, ctypes.c_char_p]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u786e\u4fddPython\u80fd\u591f\u6b63\u786e\u5730\u89e3\u91ca\u51fd\u6570\u7684\u8fd4\u56de\u503c\u548c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7\u7b2c\u4e09\u65b9\u5de5\u5177\u548c\u5e93<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528ctypes\u53ef\u80fd\u4e0d\u591f\u65b9\u4fbf\u6216\u529f\u80fd\u6709\u9650\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u548c\u5e93\uff0c\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528pywin32\u5e93<\/li>\n<\/ol>\n<p><p><code>pywin32<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684Windows\u7279\u5b9a\u7684Python\u6269\u5c55\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e0eWindows API\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u4f7f\u7528pywin32\u5e93\uff0c\u4f60\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u4e0eDLL\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32api<\/p>\n<h2><strong>\u52a0\u8f7dDLL<\/strong><\/h2>\n<p>h_dll = win32api.LoadLibrary(&#39;path_to_your_dll.dll&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u51fd\u6570\u5730\u5740<\/strong><\/h2>\n<p>addr = win32api.GetProcAddress(h_dll, &#39;your_function_name&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<h2><strong>\u9700\u8981\u4f7f\u7528ctypes\u6216\u5176\u4ed6\u65b9\u6cd5\u6765\u8c03\u7528\u8be5\u5730\u5740<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528cffi\u5e93<\/li>\n<\/ol>\n<p><p><code>cffi<\/code>\u662f\u53e6\u4e00\u4e2a\u53ef\u4ee5\u7528\u6765\u52a0\u8f7d\u548c\u8c03\u7528DLL\u7684\u5e93\u3002\u5b83\u4e0ectypes\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cffi import FFI<\/p>\n<p>ffi = FFI()<\/p>\n<h2><strong>\u5b9a\u4e49\u51fd\u6570\u63a5\u53e3<\/strong><\/h2>\n<p>ffi.cdef(&quot;&quot;&quot;<\/p>\n<p>    int your_function_name(int, const char *);<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u52a0\u8f7dDLL<\/strong><\/h2>\n<p>C = ffi.dlopen(&#39;path_to_your_dll.dll&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = C.your_function_name(123, b&quot;example&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u89e3\u6790Windows\u6ce8\u518c\u8868<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0cDLL\u6587\u4ef6\u7684\u8def\u5f84\u53ef\u80fd\u5b58\u50a8\u5728Windows\u6ce8\u518c\u8868\u4e2d\u3002\u901a\u8fc7\u89e3\u6790\u6ce8\u518c\u8868\uff0c\u4f60\u53ef\u4ee5\u627e\u5230DLL\u6587\u4ef6\u7684\u8def\u5f84\u5e76\u52a0\u8f7d\u5b83\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528winreg\u6a21\u5757<\/li>\n<\/ol>\n<p><p><code>winreg<\/code>\u6a21\u5757\u5141\u8bb8\u4f60\u8bbf\u95eeWindows\u6ce8\u518c\u8868\u5e76\u8bfb\u53d6\u5176\u4e2d\u7684\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u67e5\u627eDLL\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import winreg<\/p>\n<p>def get_dll_path(registry_key):<\/p>\n<p>    try:<\/p>\n<p>        # \u6253\u5f00\u6ce8\u518c\u8868\u952e<\/p>\n<p>        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, registry_key)<\/p>\n<p>        # \u8bfb\u53d6\u503c<\/p>\n<p>        path, _ = winreg.QueryValueEx(key, &quot;YourDllValueName&quot;)<\/p>\n<p>        # \u5173\u95ed\u952e<\/p>\n<p>        winreg.CloseKey(key)<\/p>\n<p>        return path<\/p>\n<p>    except WindowsError:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u83b7\u53d6DLL\u8def\u5f84<\/strong><\/h2>\n<p>dll_path = get_dll_path(r&quot;SOFTWARE\\YourSoftwareKey&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4ece\u6ce8\u518c\u8868\u4e2d\u83b7\u53d6DLL\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5e76\u4f7f\u7528\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u52a0\u8f7d\u548c\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u52a0\u8f7d\u548c\u8c03\u7528DLL\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8def\u5f84\u95ee\u9898<\/strong>\uff1a\u786e\u4fddDLL\u6587\u4ef6\u7684\u8def\u5f84\u6b63\u786e\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u65f6\u3002<\/li>\n<li><strong>\u51fd\u6570\u7b7e\u540d<\/strong>\uff1a\u4e86\u89e3DLL\u4e2d\u51fd\u6570\u7684\u7b7e\u540d\uff0c\u4ee5\u4fbf\u6b63\u786e\u5730\u8c03\u7528\u5b83\u4eec\u3002\u8fd9\u901a\u5e38\u9700\u8981\u67e5\u770bDLL\u7684\u6587\u6863\u6216\u4f7f\u7528\u5de5\u5177\u6765\u5bfc\u51fa\u51fd\u6570\u5217\u8868\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u5728\u8c03\u7528DLL\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5982\u53c2\u6570\u4e0d\u5339\u914d\u3001\u8def\u5f84\u9519\u8bef\u7b49\u3002\u9700\u8981\u505a\u597d\u9519\u8bef\u5904\u7406\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u76f4\u63a5\u8c03\u7528DLL\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u9891\u7e41\u8c03\u7528\u65f6\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u4f18\u5316\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528Python\u7684\u529f\u80fd\u548c\u7b2c\u4e09\u65b9\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0eDLL\u8fdb\u884c\u4ea4\u4e92\uff0c\u4e3a\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u589e\u52a0\u66f4\u591a\u529f\u80fd\u3002\u65e0\u8bba\u662f\u4f7f\u7528ctypes\u8fd8\u662f\u5176\u4ed6\u5e93\uff0c\u90fd\u9700\u8981\u4ed4\u7ec6\u4e86\u89e3DLL\u7684\u7ed3\u6784\u548c\u529f\u80fd\uff0c\u4ee5\u4fbf\u6b63\u786e\u5730\u52a0\u8f7d\u548c\u8c03\u7528\u5b83\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\u8f7dDLL\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ctypes<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165<code>ctypes<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>ctypes.CDLL<\/code>\u6216<code>ctypes.WinDLL<\/code>\u6765\u52a0\u8f7dDLL\u3002\u52a0\u8f7d\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u76f8\u5e94\u7684\u51fd\u6570\u539f\u578b\u6765\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import ctypes\n\n# \u52a0\u8f7dDLL\nmy_dll = ctypes.CDLL(&#39;path_to_your_dll.dll&#39;)\n\n# \u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\nresult = my_dll.function_name(args)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u4f20\u9012\u7684\u53c2\u6570\u7c7b\u578b\u4e0eDLL\u4e2d\u51fd\u6570\u7684\u9884\u671f\u7c7b\u578b\u76f8\u5339\u914d\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u83b7\u53d6DLL\u6587\u4ef6\u4fe1\u606f\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>pefile<\/code>\u5e93\u6765\u83b7\u53d6DLL\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u901a\u8fc7\u89e3\u6790PE\uff08Portable Executable\uff09\u683c\u5f0f\uff0c\u53ef\u4ee5\u63d0\u53d6DLL\u7684\u7248\u672c\u3001\u5bfc\u51fa\u51fd\u6570\u3001\u4f9d\u8d56\u5173\u7cfb\u7b49\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import pefile\n\npe = pefile.PE(&#39;path_to_your_dll.dll&#39;)\nprint(pe.dump_info())\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51faDLL\u6587\u4ef6\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u65b9\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406DLL\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1f<\/strong><br \/>\u5904\u7406DLL\u51fd\u6570\u7684\u8fd4\u56de\u503c\u901a\u5e38\u9700\u8981\u6839\u636e\u8fd4\u56de\u7c7b\u578b\u8fdb\u884c\u9002\u5f53\u7684\u8f6c\u6362\u3002\u5728\u4f7f\u7528<code>ctypes<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\u6765\u786e\u4fdd\u8fd4\u56de\u503c\u7684\u6b63\u786e\u5904\u7406\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dll.function_name.restype = ctypes.c_int  # \u8bbe\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\nresult = my_dll.function_name(args)\n<\/code><\/pre>\n<p>\u6839\u636eDLL\u51fd\u6570\u7684\u4e0d\u540c\u8fd4\u56de\u7c7b\u578b\uff0c\u4f8b\u5982\u6574\u578b\u3001\u6d6e\u70b9\u578b\u6216\u6307\u9488\uff0c\u6b63\u786e\u8bbe\u7f6e<code>restype<\/code>\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u4ee5\u786e\u4fdd\u63a5\u6536\u5230\u7684\u503c\u662f\u51c6\u786e\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u83b7\u53d6DLL\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528ctypes\u5e93\u3001\u901a\u8fc7\u7b2c\u4e09\u65b9\u5de5\u5177\u548c\u5e93\u3001\u89e3\u6790Windo [&hellip;]","protected":false},"author":3,"featured_media":929732,"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\/929726"}],"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=929726"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929726\/revisions"}],"predecessor-version":[{"id":929734,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929726\/revisions\/929734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/929732"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=929726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=929726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=929726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}