{"id":13180,"date":"2023-02-11T22:47:35","date_gmt":"2023-02-11T14:47:35","guid":{"rendered":"http:\/\/139.9.1.231\/?p=13180"},"modified":"2023-02-12T14:00:35","modified_gmt":"2023-02-12T06:00:35","slug":"ctypes","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2023\/02\/11\/ctypes\/","title":{"rendered":"python \u548c c++ \u8054\u5408\u7f16\u8bd1&#8212;ctypes\u5e93"},"content":{"rendered":"\n<p class=\"has-text-align-center has-light-pink-background-color has-background\"><a rel=\"noreferrer noopener\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/592995553\" target=\"_blank\">Python \u4f7f\u7528 ctypes \u8c03\u7528 C\/C++ DLL \u52a8\u6001\u94fe\u63a5\u5e93<\/a><\/p>\n\n\n\n<h2>Python ctypes\u6a21\u5757\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h2>\n\n\n\n<p><strong>ctypes \u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul><li>Python\u5185\u5efa\uff0c\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5<\/li><li>\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u4e8c\u8fdb\u5236\u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u5728Python\u4e00\u4fa7\uff0c\u4e0d\u9700\u8981\u4e86\u89e3 c\/c++ dll \u5185\u90e8\u7684\u5de5\u4f5c\u65b9\u5f0f<\/li><li>\u5bf9C\/C++\u4e0ePython\u57fa\u672c\u7c7b\u578b\u7684\u76f8\u4e92\u6620\u5c04\u6709\u826f\u597d\u7684\u652f\u6301\u3002<\/li><\/ul>\n\n\n\n<p><strong>ctypes \u5728\u4e0b\u5217\u573a\u666f\u53ef\u4ee5\u53d1\u6325\u8f83\u5927\u4f5c\u7528<\/strong><\/p>\n\n\n\n<ul><li>\u8fd0\u7b97\u91cf\u5927\u7684\u64cd\u4f5c\u53ef\u4ee5\u5199\u6210 C\/C++ dll, python \u901a\u8fc7 ctypes \u6765\u8c03\u7528, \u5927\u5e45\u63d0\u5347Python\u4ee3\u7801\u6027\u80fd\u3002<\/li><li>python\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 C\/C++\u5404\u7c7b\u8d44\u6e90\uff0c\u5982boost\u5e93\u7b49\u3002<\/li><li>\u7b2c3\u65b9\u8f6f\u4ef6\u6216\u786c\u4ef6\u63d0\u4f9b\u7684SDK\u5e93\uff0c\u901a\u8fc7ctypes\u6765\u5b9e\u73b0\u8c03\u7528\u5bf9\u63a5\u3002 \u7406\u8bba\u4e0aC\/C++\u7684\u5e93\u90fd\u53ef\u4ee5\u5bf9\u63a5\uff0c\u8fd9\u6bd4\u5176\u5b83\u8bed\u8a00\u65b9\u4fbf\u5f97\u591a\u3002<\/li><\/ul>\n\n\n\n<p>Python\u672c\u8eab\u5f00\u53d1\u6548\u7387\u9ad8\uff0c\u8fd8\u5177\u5907\u4e30\u5bcc\u7684\u751f\u6001\u8d44\u6e90\uff0c\u6709\u4e86 ctypes \u52a0\u6301\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528C\/C++\u7684\u4f18\u79c0\u8d44\u6e90\uff0c\u56e0\u6b64\uff0c\u638c\u63e1\u4e86\u8fd9\u4e2a\u5de5\u5177\uff0c\u76f8\u4fe1\u6211\uff0c\u5728\u5927\u90e8\u5206\u9879\u76ee\u4e0a\u5c06\u7ed9\u4f60\u5e26\u6765\u60ca\u559c\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/ctypes.html\" target=\"_blank\" rel=\"noreferrer noopener\">ctypes \u5b98\u65b9\u6587\u6863<\/a>\uff0c\u66f4\u7740\u91cd\u662f\u4e00\u4efd\u4ea7\u54c1\u8bf4\u660e\u4e66\uff0c\u800c\u4e0d\u662f\u6559\u7a0b\uff0c\u9605\u8bfb\u5b98\u65b9\u6587\u6863\u6765\u5b66\u4e60\u6bd4\u8f83\u8017\u7cbe\u529b\u3002 \u672c\u4eba\u65e8\u5728\u7ed9\u63d0\u4f9b\u4e00\u4efd\u4f18\u79c0\u6559\u7a0b\uff0c\u5c3d\u53ef\u80fd\u6709\u6761\u7406\u5730\u8bb2\u6e05\u695a\u539f\u7406\u3001\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u6f14\u793a\u5982\u4f55\u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u6d89\u53ca\u4e00\u4e9b\u66f4\u590d\u6742\u7684\u4f7f\u7528\u573a\u666f\uff0c\u5982\u56de\u8c03\u51fd\u6570\u7b49\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u9664\u4e86ctypes \u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7 python C API\u3001Cython\u6765\u7f16\u5199C\/C++\u4ee3\u7801\u6a21\u5757\uff0c\u6216\u8005\u5229\u7528\u7b2c3\u65b9\u5de5\u5177\u5305\u6765\u5b8c\u6210\u878d\u5408\uff0c\u5982<br>Swig<br>pybind11<\/p><\/blockquote>\n\n\n\n<h2>1\u3001Visual Studio 2022 \u628a\u9879\u76ee\u6253\u5305\u6210DLL<\/h2>\n\n\n\n<p>\u5728\u793a\u4f8b\u8bb2\u89e3\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u7528Visual Studio2022\u521b\u5efa\u5e76\u7f16\u8bd11\u4e2aDLL\u9879\u76ee<\/p>\n\n\n\n<h3>1) \u521b\u5efa\u65b0\u9879\u76ee\uff0c \u9009\u62e9dll\u9879\u76ee<\/h3>\n\n\n\n<p>\u81f3\u6b64\u521b\u5efa\u5b8c\u6210\uff0c\u5305\u542b\u6e90\u6587\u4ef6dllmain.cpp\u3001pch.cpp\uff0c\u5934\u6587\u4ef6framework.h\u3001pch.h\u3002 ![\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0]<\/p>\n\n\n\n<h3>2\uff09 \u5b9a\u4e49\u5934\u6587\u4ef6 boFirst.h<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#define IMPORT_DLL __declspec(dllimport) \u5b9a\u4e49\u4e86IMPORT_DLL \u5b8f\uff0c __declspec(dllimport)\u8868\u793a\u51fd\u6570\u5c06\u88ab\u7f16\u8bd1\u4e3adll\nextern \"C\" IMPORT_DLL  ==\u300b \u6307\u5b9a\u8be5\u51fd\u6570\u53ef\u88ab\u5916\u90e8\u8c03\u7528\u3002 \n#pragma once\n\n#ifdef DLL_EXPORT\n#define IMPORT_DLL __declspec(dllexport)\n#else\n#define IMPORT_DLL __declspec(dllimport)\n#endif\n\n<em>\/\/\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\n<\/em><em><\/em>struct boShape\n{\n    char  shape&#91;20];\n    float width; \n    float height;\n    float depth; \n};\n\n<em>\/\/ \u81ea\u5b9a\u4e49\u51fd\u6570\n<\/em><em><\/em>extern \"C\" IMPORT_DLL int bo_add(int a, int b); \n<em>\/\/\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53c2\u6570\u4e3a\u7ed3\u6784\u4f53 boStruct \n<\/em><em><\/em>extern \"C\" IMPORT_DLL float bo_shape_vol(boShape bs);\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5206\u522b\u6dfb\u52a0bo_add, bo_shape_vol \u4e24\u4e2a\u53ef\u4f9b\u5916\u90e8\u8c03\u7528\u51fd\u6570\u58f0\u660e\uff0c \u4e5f\u53ef\u4ee5\u66f4\u7b80\u5316\uff0c\u53ea\u8981\u51fd\u6570\u524d\u9762\u6709extern &#8220;C&#8221; __declspec(dllexport) \u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef _PY_LIST_2_C_ARRAY_H_\n#define _PY_LIST_2_C_ARRAY_H_\n\n#include &lt;stdio.h&gt;\n\ntypedef struct {\n    int value;\n    wchar_t* name;\n} Item;\n\nextern \"C\" __declspec(dllexport)\nint sum_diagonal(Item** field, size_t size);\n\n#endif\n<\/code><\/pre>\n\n\n\n<h3>3\uff09\u5b9e\u73b0\u51fd\u6570\u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/ boFirst.cpp : Defines the exported functions for the DLL.\n<\/em><em><\/em>#include \"pch.h\" \/\/ use stdafx.h in Visual Studio 2017 and earlier\n#include &lt;utility&gt;\n#include &lt;limits.h&gt;\n#include \"boFirst.h\"\n# include &lt;iostream&gt;\n\nint bo_add(int a, int b)\n{\n    int c;\n    c = a + b; \n    return c; \n}\n\nfloat bo_shape_vol(boShape bs)\n{\n    float volume = bs.width * bs.height * bs.depth;  \n    std::cout &lt;&lt; bs.shape &lt;&lt; \"volume is \" &lt;&lt; volume &lt;&lt; std::endl; \n    return volume; \n}\n<\/code><\/pre>\n\n\n\n<h3>4) \u7f16\u8bd1\u751f\u6210dll<\/h3>\n\n\n\n<p>\u9009\u62e9build solution , \u8f93\u51fa\u7a97\u53e3\u53ef\u4ee5\u770b\u5230\uff0c\u5728x64\\Debug\\ \u76ee\u5f55\u4e0b\u751f\u6210\u4e86DLL_Test.dll \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Rebuild started...\n1&gt;------ Rebuild All started: Project: DLL_Test, Configuration: Debug x64 ------\n1&gt;pch.cpp\n1&gt;dllmain.cpp\n1&gt;boFirst.cpp\n1&gt;D:\\workplace\\temp\\C++\\DLL_Test\\DLL_Test\\boFirst.cpp(8,5): warning C4273: 'bo_add': inconsistent dll linkage\n1&gt;D:\\workplace\\temp\\C++\\DLL_Test\\DLL_Test\\boFirst.h(20,27): message : see previous definition of 'bo_add'\n1&gt;D:\\workplace\\temp\\C++\\DLL_Test\\DLL_Test\\boFirst.cpp(15,7): warning C4273: 'bo_shape_vol': inconsistent dll linkage\n1&gt;D:\\workplace\\temp\\C++\\DLL_Test\\DLL_Test\\boFirst.h(22,29): message : see previous definition of 'bo_shape_vol'\n1&gt;Generating Code...\n1&gt;   Creating library D:\\workplace\\temp\\C++\\DLL_Test\\x64\\Debug\\DLL_Test.lib and object D:\\workplace\\temp\\C++\\DLL_Test\\x64\\Debug\\DLL_Test.exp\n1&gt;DLL_Test.vcxproj -&gt; D:\\workplace\\temp\\C++\\DLL_Test\\x64\\Debug\\DLL_Test.dll\n1&gt;Done building project \"DLL_Test.vcxproj\".\n========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========\n<\/code><\/pre>\n\n\n\n<h2>2\u3001\u901a\u8fc7 ctypes \u8c03\u7528DLL\u7684\u7b80\u5355\u793a\u4f8b<\/h2>\n\n\n\n<p>\u5728\u4e0a\u4e00\u8282\u4e2d\u7684dll\u6709\u4e24\u4e2a\u65b9\u6cd5\uff0c\u5176\u4e2d1\u4e2a\u8f93\u5165\u53c2\u6570\u4f7f\u7528\u4e86C++ Struct \u7ed3\u6784\u4f53, python\u6ca1\u6709\u5bf9\u5e94\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n\n<h3>\u7b2c1\u79cd\u8c03\u7528\u65b9\u5f0f (\u6807\u51c6\u65b9\u5f0f\uff09\uff1a<\/h3>\n\n\n\n<h3>Step-1:<\/h3>\n\n\n\n<p>\u5c06DLL_Test.dll \u6587\u4ef6\u62f7\u8d1d\u81f3python\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff0c \u8be5dll\u4e2d\u5305\u542b\u5982\u4e0b\u53ef\u4f9b\u8c03\u7528\u7684\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int bo_add(int a, int b)\nfloat bo_shape_vol(boShape bs); \u5176\u4e2dboShape\u4e3a\u81ea\u5b9a\u4e49struct<\/code><\/pre>\n\n\n\n<h3>Step-2:<\/h3>\n\n\n\n<p>\u7f16\u5199\u4ee3\u7801,\u8c03\u7528 bo_add()\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from ctypes import *\ndll = cdll.LoadLibrary(\".\/DLL_Test.dll\")\nprint(\"bo_add \", dll.bo_add(20, 80))<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\uff0c\u5373\u53ef\u4ee5\u5f97\u5230\u8f93\u51fa 100<\/p>\n\n\n\n<p>cdll.LoadLibrary() \u65b9\u6cd5\u8fd4\u56de cdll \u5bf9\u8c61\uff0ccdll \u8c03\u7528C\/C++\u51fd\u6570\u7684\u65b9\u6cd5\u9075\u4ece cdecl \u65b9\u5f0f\uff08C\/C++\u51fd\u6570\u8c03\u7528\u7684\u6807\u51c6\u65b9\u5f0f\uff09\u3002<\/p>\n\n\n\n<p>\u4e0a\u4f8b\u662f\u76f4\u63a5\u8c03\u7528 dll\u4e2d\u7684\u51fd\u6570.<\/p>\n\n\n\n<h3>\u7b2c2\u79cd\u8c03\u7528\u65b9\u5f0f( \u51fd\u6570\u7b7e\u540d\u65b9\u5f0f\uff09<\/h3>\n\n\n\n<p>\u51fd\u6570\u7b7e\u540d\u65b9\u5f0f\uff0c\u76f8\u5f53\u4e8e\u5728python\u5185\u91cd\u65b0\u7533\u660e1\u4e2aC++\u51fd\u6570\u7684\u522b\u540d\uff0c\u7533\u660e\u5185\u5bb9\u5305\u62ec\u7528ctype\u6307\u5b9a\u51fd\u6570\u5f62\u53c2\u4e0e\u8fd4\u56de\u503c\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import ctypes\nclibrary = ctypes.CDLL('clibrary.so')\n\n<em># \u5b9a\u4e491\u4e2apython\u51fd\u6570\u540d\uff0c\u6307\u5411 C++ \u51fd\u6570<\/em>\naddTwoNumbers = clibrary.add\n\n<em># \u7528ctypes \u6570\u636e\u7c7b\u578b\uff0c\u5b9a\u4e49\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u4e0e\u8fd4\u56de\u503c\u7c7b\u578b, <\/em>\naddTwoNumbers.argtypes = &#91;ctypes.c_int, ctypes.c_int]\naddTwoNumbers.restype = ctypes.c_int\n\n<em># \u4f7f\u7528\u7b7e\u540d\u51fd\u6570<\/em>\nprint(\"Sum of two numbers is :\", addTwoNumbers(20, 10))<\/code><\/pre>\n\n\n\n<p>Output: Sum of two numbers is : 30<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>ctypes \u6570\u636e\u7c7b\u578b &#8211;&gt; Python \u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u662f\u7a0b\u5e8f\u5728\u540e\u53f0\u81ea\u52a8\u5b8c\u6210\u7684\u3002<\/p><\/blockquote>\n\n\n\n<h3>3\u3001ctypes \u6570\u636e\u7c7b\u578b<\/h3>\n\n\n\n<p>C\/C++\u7684\u6570\u636e\u7c7b\u578b\u4e0e Python\u6570\u636e\u7c7b\u578b\u53ca\u4f7f\u7528\u65b9\u5f0f\u90fd\u6709\u8f83\u591a\u5dee\u5f02\uff0c\u56e0\u6b64python\u8c03\u7528C++\u51fd\u6570\uff0c\u5fc5\u987b\u9700\u8981\u5bf9DLL\u51fd\u6570\u5f62\u53c2\u4ee5\u53ca\u8fd4\u56de\u503c\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u624d\u80fd\u628a\u6570\u636e\u4f20\u5165DLL\uff0c\u4ee5\u53ca\u89e3\u6790DLL\u7684\u8fd4\u56de\u503c\u3002ctypes \u6a21\u5757\u63d0\u4f9b\u4e86\u4e2d\u95f4\u6570\u636e\u7c7b\u578b,\u6765\u5e2e\u52a9Python\u5b8c\u6210\u6570\u636e\u8f6c\u6362\uff0c\u8fd9\u4e9b\u7c7b\u578b\u5728python\u4e2d\u90fd\u662f\u4ee5\u5bf9\u8c61\u65b9\u5f0f\u51fa\u73b0\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>Python \u8c03\u7528DLL\u51fd\u6570\u52063\u6b65\uff1a<\/strong><\/p>\n\n\n\n<p>1)&nbsp;<strong>\u5728Python\u4e2d\u91cd\u65b0\u7533\u660e DLL \u51fd\u6570<\/strong>\uff1a \u9664\u4e86\u51fd\u6570\u540d\u4e4b\u5916\uff0c\u8fd8\u8981\u7528ctypes\u7684\u6570\u636e\u7c7b\u578b\u5c06 DLL \u51fd\u6570\u7684\u5f62\u53c2\u4e0e\u8fd4\u56de\u503c\u91cd\u65b0\u7533\u660e\u4e00\u904d\u3002<\/p>\n\n\n\n<p>2)&nbsp;<strong>\u5b9e\u53c2\u51c6\u5907<\/strong>\uff1a\u5f53\u5b9e\u9645\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u5c06\u6570\u636e\u8d4b\u503c\u7ed9ctypes\u53c2\u6570\u540e\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n\n\n\n<p>3)&nbsp;<strong>\u89e3\u6790\u8fd4\u56de\u503c<\/strong>\uff1a\u5982\u679c\u6709\u8fd4\u56de\u503c\uff0c\u4ece ctypes \u7c7b\u578b\u4e2d\u89e3\u6790\u51fa\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\uff0c\u7b2c1\u6b65\uff0c\u7528ctypes \u7c7b\u578b\u7533\u660e DLL\u51fd\u6570\u5f62\u53c2\u6700\u4e3a\u5173\u952e\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u8fd9\u4e00\u6b65\u76ee\u7684\u662f\u5c06 C++\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3aPython\u6570\u636e\u7c7b\u578b\uff0cctypes \u8d77\u5230\u6865\u6881\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e0b ctypes \u63d0\u4f9b\u4e86\u54ea\u4e9b\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u3002<\/p>\n\n\n\n<h4>1) ctypes \u4e0ec\u3001python\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u5e94\u5173\u7cfb<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"989\" height=\"1007\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-386.png\" alt=\"\" class=\"wp-image-13953\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-386.png 989w, http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-386-295x300.png 295w, http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-386-768x782.png 768w\" sizes=\"(max-width: 989px) 100vw, 989px\" \/><\/figure>\n\n\n\n<h3>2)&nbsp;<strong>\u6574\u6570\u578b\u53c2\u6570\u53d8\u91cf\u5b9a\u4e49<\/strong><\/h3>\n\n\n\n<p><code>i = c_int()<\/code><\/p>\n\n\n\n<p>\u8d4b\u503c \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i = c_int(99) \ni.value=99\nprint(i) \nc_long(99)<\/code><\/pre>\n\n\n\n<h3>3)&nbsp;<strong>\u6d6e\u70b9\u6570\u503c\u53c2\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>d1 = c_float()\nd1.value=10.3\nd1\nc_float(10.300000190734863)\n&gt;&gt;&gt; d1.value\n10.300000190734863<\/code><\/pre>\n\n\n\n<h3>4)&nbsp;<strong>\u5b57\u7b26\u4e32\u53c2\u6570<\/strong><\/h3>\n\n\n\n<p>\u7528 char * 32 \u5b9a\u4e491\u4e2a32\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u8d4b\u503c\u7528 value\u5c5e\u6027<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; str1 = c_char * 32\n&gt;&gt;&gt; str1\n&lt;class '__main__.c_char_Array_32'&gt;\n&gt;&gt;&gt; str1.value = b\"hello\"\n&gt;&gt;&gt; str1\n&lt;class '__main__.c_char_Array_32'&gt;\n&gt;&gt;&gt; str1.value\nb'hello'<\/code><\/pre>\n\n\n\n<p>C\/C++\u4e2d\u5b57\u7b26\u4e32\u662f\u7528char* \u6307\u9488\u6216\u6570\u7ec4\u6765\u4ee3\u66ff\uff0cctypes \u63d0\u4f9b\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u6307\u9488\u7c7b\u578b c_char_p\uff0c\u7ed9c_char_p\u8d4b\u503c\u901a\u5e38\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a &#8211; \u628a python \u5b57\u7b26\u4e32\u8f6c\u4e3a bytes \u7c7b\u578b\uff0c \u4f7f\u7528endcode()\u65b9\u6cd5\u3002 &#8211; \u76f4\u63a5\u4f7f\u7528bytes \u7c7b\u578b\u5b57\u8282\u4e32\u3002<\/p>\n\n\n\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a bytes \u5bf9\u8c61\u540e\uff0c\u7528c_char_p\u7684value\u5c5e\u6027\u8d4b\u503c\uff0c \u7528\u6cd5\u793a\u4f8b str2.value = x \u8d4b\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; x = b\"abcdef\"\n&gt;&gt;&gt; str2 = c_char_p()\n&gt;&gt;&gt; str2.value = x\n&gt;&gt;&gt; str2\nc_char_p(140250436808304)\n&gt;&gt;&gt; str2.value\nb'abcdef'\n&gt;&gt;&gt; str2 = str1\n&gt;&gt;&gt; str2\n&lt;class '__main__.c_char_Array_32'&gt;\n&gt;&gt;&gt; str2.value\nb'hello'<\/code><\/pre>\n\n\n\n<p>c_char_p \u6307\u5411\u7684\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u662f\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u5373Bytes\u7c7b\u578b\uff0c\u5982\u679c\u662f\u4e2d\u6587\uff0c\u53ef\u4ee5\u7528utf-8\u7f16\u7801\uff0c\u663e\u793a\u65f6\u518d\u89e3\u7801.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; y=bytes(\"hello, \u5c0f\u738b\",\"utf-8\")\n&gt;&gt;&gt; str2.value=y\n&gt;&gt;&gt; str2.value\nb'hello, \\xe5\\xb0\\x8f\\xe7\\x8e\\x8b'\n&gt;&gt;&gt; str2.value.decode(encoding=\"utf-8\")\n'hello, \u5c0f\u738b'<\/code><\/pre>\n\n\n\n<p>ctypes \u8fd8\u63d0\u4f9b\u4e86&nbsp;<strong>create_string_buffer()<\/strong>&nbsp;\u65b9\u6cd5\u7528\u4e8e\u751f\u6210\u5b57\u7b26\u4e32\u3002&nbsp;<code>\u683c\u5f0f: ctypes.create_string_buffer(init_or_size, size=None)<\/code><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>str3 = create_string_buffer(b&#8217;world&#8217;,32)<br>print(str3)<br>&lt;<strong>main<\/strong>.c_char_Array_32 object at 0x7f8e9826eac0&gt;<br>print(str3.value)<br>b&#8217;world&#8217;<br>print(str3.raw)<br>b&#8217;\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00&#8242; repr(str3.raw)<\/p><\/blockquote>\n\n\n\n<p>create a 3 byte buffer, initialized to NUL bytes<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p = create_string_buffer(3)<\/code><\/pre>\n\n\n\n<p>create a buffer containing a NUL terminated string<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p = create_string_buffer(b'Hello') \nprint(sizeof(p)\nrepr(p.raw))<\/code><\/pre>\n\n\n\n<p>create a 10 byte buffer<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p = create_string_buffer(b'Hello', 10) print(sizeof(p), repr(p.raw))<\/code><\/pre>\n\n\n\n<h3>5) \u6570\u7ec4\u7c7b\u578b\u53d8\u91cf<\/h3>\n\n\n\n<p><br>\u6570\u7ec4\u7684\u521b\u5efa\u548cC\/C++\u8bed\u8a00\u7684\u7c7b\u4f3c\uff0c\u7ed9\u5b9a\u6570\u636e\u7c7b\u578b\u548c\u957f\u5ea6\u5373\u53ef<br>\u5982\u5b9a\u4e49 c_int \u7c7b\u578b\u6570\u7ec4, \u957f\u5ea6\u4e3a10. \u5148\u5b9a\u4e491\u4e2a INT_ARRAY \u7c7b\u578b\uff0c\u518d\u521b\u5efa1\u4e2a\u6570\u7ec4\u53d8\u91cf\uff0c\u5176\u7c7b\u578b\u4e3a c_long_Array_10\uff0c \u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; INT_ARRAY = c_int * 10\n&gt;&gt;&gt; array1 = INT_ARRAY(10,20,30,40,50,60,70,80,90,100)\n&gt;&gt;&gt; array1\n&lt;__main__.c_long_Array_10 object at 0x00000283665D7040&gt;\n&gt;&gt;&gt; array1&#91;5]\n60\n&gt;&gt;&gt; array1&#91;7:]\n&#91;80, 90, 100]<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u6570\u636e\u53d8\u91cf\u8fc7\u7a0b\u53ef\u4ee5\u5c06\u4e24\u6b65\u5e76\u4e3a1\u6b65\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; array2 = (c_int * 10)(10,20,30,40,50,60,70,80,90,100)\n&gt;&gt;&gt; array2\n&lt;__main__.c_long_Array_10 object at 0x0000028366558B40&gt;\n&gt;&gt;&gt; array2&#91;7:]\n&#91;80, 90, 100]<\/code><\/pre>\n\n\n\n<h3>6)&nbsp;<strong>Structure \u53d8\u91cf<\/strong><\/h3>\n\n\n\n<p>Python\u7684dict\u7c7b\u578b\u867d\u7136\u4e0eC\/C++ struct \u7ed3\u6784\u4f53\u7c7b\u578b\u5f88\u76f8\u4f3c\uff0c\u4f46\u4e0d\u80fd\u76f4\u63a5\u62ff\u6765\u7528\u3002ctypes\u6a21\u5757\u63d0\u4f9b\u4e86 Structure \u7c7b\u6765\u8fdb\u884c C\/C++ struct\u7ed3\u6784\u4f53\u7c7b\u578b\u8f6c\u6362\u3002 \u793a\u4f8b\uff0c\u5b9a\u4e491\u4e2aPOINT \u50cf\u7d20\u70b9\u7ed3\u6784\u4f53<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class POINT(Structure):\n...     _fields_ = &#91;(\"x\", c_int),\n...                 (\"y\", c_int)]\n&gt;&gt;&gt; point = POINT(10, 20)\n&gt;&gt;&gt; print(point.x, point.y)\n10 20<\/code><\/pre>\n\n\n\n<p>\u5b9a\u4e491\u4e2aRECT \u7ed3\u6784\u4f53\uff0c\u5176\u5b57\u6bb5\u4e3aPOINT\u7ed3\u6784\u4f53<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; class RECT(Structure):\n...     _fields_ = &#91;(\"upperleft\", POINT),\n...                 (\"lowerright\", POINT)]\n...\n&gt;&gt;&gt; rc = RECT(point)\n&gt;&gt;&gt; print(rc.upperleft.x, rc.upperleft.y)\n0 5\n&gt;&gt;&gt; print(rc.lowerright.x, rc.lowerright.y)\n0 0\n&gt;&gt;&gt;r = RECT(POINT(1, 2), POINT(3, 4))<\/code><\/pre>\n\n\n\n<p>1\u4e2a\u66f4\u590d\u6742\u7684\u7ed3\u6784\u4f53\uff0c \u5b57\u6bb5 b , \u4e3a\u6d6e\u70b9\u6570\uff0c \u201cpoint_arrary\u201d \u4e3a1\u4e2a\u5305\u542bPOINT\u7ed3\u6784\u4f53\u7c7b\u578b\u7684Array<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; class MyStruct(Structure):\n...     _fields_ = &#91;(\"a\", c_int),\n...                 (\"b\", c_float),\n...                 (\"point_array\", POINT * 4)]<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>\u8bf4\u660e\uff1a python3 \u65b0\u589e\u4e86struct \u6a21\u5757\uff0c\u57fa\u4e8e\u5e8f\u5217\u5316\u7684\u539f\u7406\uff0c\u53ef\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u6253\u5305\u8fdb ctypes bytes buffer\u53d8\u91cf\uff0c\u7528\u4e8e\u5411c\/c++ struct \u53d8\u91cf\u4f20\u503c\uff0c \u4f46\u76f4\u89c2\u5ea6\u4e0d\u5982 Structure\u53d8\u91cf\uff0c\u6709\u5174\u8da3\u8005\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3\u4e00\u4e0b python struct \u6a21\u5757\u7684\u4f7f\u7528<\/em><\/p><\/blockquote>\n\n\n\n<h3>7)&nbsp;<strong>\u6307\u9488\u53d8\u91cf<\/strong><\/h3>\n\n\n\n<p>ctypes\u63d0\u4f9b\u4e86pointer()\u548cPOINTER()\u4e24\u79cd\u65b9\u6cd5\u521b\u5efa\u6307\u9488<\/p>\n\n\n\n<h3>pointer( object )\u7528\u4e8e\u5c06\u67d0\u4e2a\u7c7b\u578b\u7684\u5bf9\u8c61\u8f6c\u5316\u4e3a\u6307\u9488<\/h3>\n\n\n\n<p>\u5176\u53c2\u6570\u5fc5\u987b\u662fctypes\u7684\u53d8\u91cf\u5bf9\u8c61\uff0c\u4e0d\u80fd\u662f\u7c7b\u578b. \u5982\u4e0b\u4f8b\uff0cint_p \u6307\u54111\u4e2ac_int \u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5f53\u524d\u503c\u4e3a99<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int_obj = c_int(99)\nint_p = pointer(int_obj)\nprint(int_p)\n\u4f7f\u7528contents\u65b9\u6cd5\u8bbf\u95ee\u6307\u9488\nprint(int_p.contents)\n<em># \u83b7\u53d6\u6307\u9488\u6307\u5411\u7684\u503c<\/em>\nprint(int_p&#91;0])<\/code><\/pre>\n\n\n\n<p>output \u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ctypes.wintypes.LP_c_long object at 0x00000217897102C0&gt;\nc_long(99)\n99<\/code><\/pre>\n\n\n\n<h3>POINTER()\u7528\u4e8e\u5b9a\u4e49\u6307\u9488\u53d8\u91cf\u7c7b\u578b<\/h3>\n\n\n\n<p>POINTER() \u7684\u53c2\u6570\u5fc5\u987b\u662f<code>\u7c7b\u578b\u540d\u79f0<\/code>\uff0c\u76f8\u5f53\u4e8e\u5b9a\u4e49\u4e86\u65b0\u76841\u4e2a\u6307\u9488\u53d8\u91cf\u7c7b\u578b\uff0c\u4f46\u6307\u5411\u503c\u7684\u7c7b\u578b\u5fc5\u987b\u662f\u6307\u5b9a\u7684<code>\u7c7b\u578b\u540d\u79f0<\/code>\u3002\u7136\u540e\u518d\u7528\u8fd9\u4e2a\u65b0\u7c7b\u578b\u5b9e\u4f8b\u53161\u4e2a\u6307\u9488\u53d8\u91cf\u3002 \u800c pointer() \u65b9\u6cd5\u9690\u5f0f\u5730\u5b8c\u6210\u4e86POINTER()\u7684\u5de5\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u6307\u9488\u7c7b\u578b<\/em>\nINT_P  = POINTER(c_int)  <em># \u5b9a\u4e49\u4e861\u4e2a\u65b0\u7684\u6307\u9488\u53d8\u91cf\u7c7b\u578b<\/em>\n<em># \u5b9e\u4f8b\u5316<\/em>\nint_obj = c_int(4)\nint_p_obj = INT_P(int_obj)   <em># \u5b9e\u4f8b\u53161\u4e2a\u6307\u9488\u53d8\u91cf<\/em>\nprint(int_p_obj)\nprint(int_p_obj.contents)\nprint(int_p_obj&#91;0])<\/code><\/pre>\n\n\n\n<p>output :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;__main__.LP_c_int object at 0x7f47df7f79e0&gt;\nc_int(4)\n4<\/code><\/pre>\n\n\n\n<h3>\u6307\u9488\u7c7b\u578b\u7684\u8f6c\u6362<\/h3>\n\n\n\n<p>ctypes\u63d0\u4f9b cast() \u65b9\u6cd5\u5c06\u4e00\u4e2actypes\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u6307\u5411\u53e6\u4e00\u4e2actypes\u6570\u636e\u7c7b\u578b\u7684\u6307\u9488\uff0ccast()\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662fctypes\u5bf9\u8c61\uff0c\u5b83\u662f\u6216\u53ef\u4ee5\u8f6c\u6362\u6210\u67d0\u79cd\u7c7b\u578b\u7684\u6307\u9488\uff0c\u53e6\u4e00\u4e2a\u662fctypes\u6307\u9488\u7c7b\u578b\u3002\u5b83\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8be5\u5b9e\u4f8b\u5f15\u7528\u4e0e\u7b2c\u4e00\u4e2a\u53c2\u6570\u76f8\u540c\u7684\u5185\u5b58\u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u521d\u59cb\u5316 1\u4e2a c_int\u7684\u6307\u9488\uff0c\u5f53\u524d\u503c\u4e3a4<\/em>\nint_p = pointer(c_int(99))\nprint(int_p)\n<em># \u5b9a\u4e491\u4e2a c_char\u7c7b\u578b\u7684\u6307\u9488\u7c7b\u578b<\/em>\nchar_p_type = POINTER(c_char)\nprint(char_p_type)\n<em># \u8f6c c_int\u53d8\u91cf\u8f6c\u6362\u4e3ac_char\u7c7b\u578b\u6307\u9488<\/em>\ncast_type = cast(int_p, char_p_type)\nprint(cast_type)<\/code><\/pre>\n\n\n\n<p>output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ctypes.wintypes.LP_c_long object at 0x0000021789710140&gt;\n&lt;class 'ctypes.LP_c_char'&gt;\n&lt;ctypes.LP_c_char object at 0x0000021789710E40&gt;<\/code><\/pre>\n\n\n\n<h3>8) enum \u679a\u4e3e\u7c7b\u578b<\/h3>\n\n\n\n<p>\u5728 C\/C++\u4e2d\u8fd8\u67091\u4e2a\u7ecf\u5e38\u7528\u5230\u7684\u7c7b\u578b\u662f enum\uff0cctypes \u6ca1\u6709\u76f8\u5173\u7c7b\u578b\uff0c\u4f46python\u67091\u4e2aenum\u7c7b. \u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55 \u5c06 python enum \u7c7b\u7528\u4e8e dll \u51fd\u6570\u53c2\u6570\u3002 \u5982 C++\u7a0b\u5e8f\u4e2d\u5b9a\u4e49\u4e861\u4e2a\u679a\u4e3e\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Priority {\n        CRITICAL = 0x1,     \n        IMPORTANT= 0x2,     \n        NORMAL = 0x3,       \n        MINOR = 0x4,    \n        INFO = 0x5  \n    };<\/code><\/pre>\n\n\n\n<p>\u67091\u4e2a\u51fd\u6570\uff1a long set_priority(Priority n) \u5728python\u4e2d\uff0c\u4e5f\u5b9a\u4e491\u4e2aenum\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import enum \nclass EnumPriority(enum.IntEnum):\n    CRITICAL = 0x1\n    IMPORTANT= 0x2\n    NORMAL = 0x3\n    MINOR = 0x4\n    INFO = 0x5\n\n    <em>#\u6ce8\u610f\u5fc5\u987b\u5c06\u6dfb\u52a0\u8fd9\u4e2a\u65b9\u6cd5\uff0cctyps \u8981\u6c42\uff0c\u5c06obj\u8f6c\u4e3a init\u578b<\/em>\n    @classmethod\n    def from_param(cls, obj):\n        return int(obj)<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\uff0c\u5fc5\u987b\u8981\u6dfb\u52a01\u4e2a from_param()\u65b9\u6cd5., \u5c06\u4e0a\u9762\u7684\u679a\u4e3e\u5143\u7d20\u8f6c\u4e3aint\u578b \u4e0b\u9762\u5c06 python enum \u505a\u4e3a\u53c2\u6570\u4f20\u5165dll<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dll =  cdll.LoadLibrary(dll_name)\nset_priority=dll.set_priority\nset_priority.restype = c_init\nset_priority.argtypes = &#91;EnumPriority]\n<em># \u8c03\u7528\u8be5\u51fd\u6570<\/em>\nprint(\"call back result is \",set_priority(EnumPriority.INFO))<\/code><\/pre>\n\n\n\n<h3>4. \u52a0\u8f7dDLL\u5e93<\/h3>\n\n\n\n<p>Ctypes \u63d0\u4f9b\u67092\u79cd\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u8c03\u7528\u65b9\u5f0f<\/p>\n\n\n\n<p>ctypes.cdll. LoadLibrary(&#8216;xxx.dll&#8217;) \u52a0\u8f7d cdel\u8c03\u7528\u65b9\u5f0f\u7684dll, \u9ed8\u8ba4c++, vc++\u751f\u6210\u7684dll\u5e93\u90fd\u662fcdel\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>ctypes.windll.LoadLibrary(&#8216;xxx.dll&#8217;) \u52a0\u8f7d win32\u8c03\u7528\u65b9\u5f0f\uff08stdcall\uff09\u7684dll\uff0c\u6240\u4ee5\u5f88\u5c11\u4f7f\u7528\u6b64\u79cd\u8c03\u7528\u65b9\u5f0f<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def find_example_ctypes(required):\n    '''\n    Finds and loads example shared object of the required major\n    '''\n    <em># Importing ``ctypes`` should be in scope of this function to prevent failure<\/em>\n    from ctypes import util, cdll\n\n    so_name = util.find_library('example.dll')\n    if so_name is None:\n        raise ExampleImportError('EXAMPLE shared object not found.')\n    example = cdll.LoadLibrary(so_name)\n    require_version(example.example_version(), required)\n    return example<\/code><\/pre>\n\n\n\n<h3>5. ctypes \u5411DLL\u51fd\u6570\u4f20\u5165\u53c2\u6570<\/h3>\n\n\n\n<p>\u6307\u5b9adll\u51fd\u6570\u53c2\u6570\u7c7b\u578b \u901a\u8fc7argtypes\u5c5e\u6027\u6765\u8bbe\u7f6e\u53c2\u6570\u7c7b\u578b\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; strchr.restype = c_char_p\n&gt;&gt;&gt; strchr.argtypes = &#91;c_char_p, c_char]<\/code><\/pre>\n\n\n\n<p>\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myadd = dll.bo_add\nmyadd.restype = c_int\nmyadd.argtypes = &#91;c_int, c_int]\nprint(\"call bo_add() with function signature approach \\n\", myadd(20, 80))<\/code><\/pre>\n\n\n\n<p><strong>\u4f20\u5165\u6307\u9488\u53c2\u6570<\/strong>&nbsp;c++ \u51fd\u6570addx()\u7684\u5f62\u53c2\u4e3a\u4e24\u4e2a\u6307\u9488\uff0c int addx( int&nbsp;<em>p1, int&nbsp;<\/em>p2)\uff0c python \u5b9e\u4f8b\u4e24\u4e2ac_int\u578b\u7684\u6307\u9488\u4f20\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int_obj1 = c_int(20)\nptr1 = pointer(int_obj1)\nint_obj2 = c_int(80)\nptr2 = pointer(int_obj2)\nprint(\"pass pointer to function addx(), \", dll.addx(ptr1, ptr2))<\/code><\/pre>\n\n\n\n<p><strong>\u4f20\u5165\u7ed3\u6784\u4f53\u53c2\u6570<\/strong><\/p>\n\n\n\n<p>\u6b65\u9aa4\uff1a &#8211; \u5728python\u5b9a\u4e491\u4e2aCtypes.Structure \u7c7b\uff0c \u5b57\u6bb5\u4e5fC++ Struct\u7c7b\u578b\u4fdd\u6301\u4e00\u81f4\u3002 &#8211; \u7528\u6b64\u7c7b\u6784\u5efa\u53c2\u6570\u503c &#8211; \u4f20\u5165dll\u51fd\u6570\uff0c\u524d\u9762\u8981\u52a0byref \u793a\u4f8b \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from ctypes import *\n\nclass MyStruct(Structure):\n    _fields_ = &#91;('shape', c_char*20),\n                ('w', c_double),\n                ('h', c_double),\n                ('d', c_double),\n                ]<\/code><\/pre>\n\n\n\n<h3>\u7b2c2\u79cd\u8c03\u7528\u65b9\u5f0f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>myadd = dll.bo_add\nmyadd.restype = c_int\nmyadd.argtypes = &#91;c_int, c_int]\nprint(\"another call method to bo_add() : \", myadd(20, 80))<\/code><\/pre>\n\n\n\n<h3>\u4f20\u5165\u7ed3\u6784\u4f53\u53c2\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>dw, dh, dd = 4.0, 7.0, 3.0\ndstr = b\"cuboid \"\ns1 = MyStruct(dstr, dw, dh, dd)\nprint(s1.w, s1.h, s1.d)\nprint(s1.shape)\nprint(dll.bo_shape_vol(byref(s1)))<\/code><\/pre>\n\n\n\n<p>Output 4.0 7.0 3.0 b&#8217;cuboid &#8216; cuboid volume is 84 233390192<\/p>\n\n\n\n<p>\u95ee\u9898\uff1a print(dll.bo_shape_vol(byref(s1))) \u6253\u5370\u51fa\u7684\u662f\u5730\u5740\uff0c\u4e0d\u662f\u7ed3\u679c 84 \u89e3\u51b3\u529e\u6cd5\uff1a\u7528\u7c7b\u7684\u8c03\u7528\u65b9\u5f0f\uff0c\u5b9a\u4e49dll\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"\u7b2c2\u79cd\u8c03\u7528\u65b9\u6cd5\") \nmyVol = dll.bo_shape_vol\nmyVol.restype = c_double  <em>#\u5b9a\u4e49dll\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b<\/em>\nmyVol.argtypes = &#91;POINTER(MyStruct)]  <em>#\u7ed3\u6784\u4f53\u53c2\u6570\u662f\u4ee5\u5730\u5740\u65b9\u5f0f\u4f20\u5165\uff0c\u56e0\u6b64\u8981\u8f6c\u4e3a\u6307\u9488<\/em>\ndx = myVol(byref(s1))\nprint(dx)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c2\u79cd\u8c03\u7528\u65b9\u6cd5\ncuboid volume is 84\n84.0<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u662f\u53e61\u4e2a\u4f8b \u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from ctypes import *\n\nclass Passport(Structure):              \n    _fields_ = &#91;(\"name\", c_char_p),    \n                (\"surname\", c_char_p),\n                (\"var\", c_int)]\n\nlib_dll = cdll.LoadLibrary(\"DLL_example.dll\")   \nlib_dll.SetPassport.argtypes  = &#91;POINTER(Passport)] \n\nlib_dll.GetPassport()          \n\nlib_dll.SetName(c_char_p(b\"Yury\"))\nlib_dll.SetSurname(c_char_p(b\"Wang\"))\n\nlib_dll.GetPassport()         \n\nname = str.encode((\"Feng\"))  \nsurname = c_char_p((b'Li')) \n\npassport = Passport(name, surname, 34) \n\nlib_dll.SetPassport(pointer(passport)) \n\nlib_dll.GetPassport()<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Load DLL in Python\nSetName\nSetSurname\nGetPassport: Default | Passport | 17\nSetName\nSetSurname\nGetPassport: Yury | Orlov | 17\nSetPassport\nGetPassport: Vasiliy | Pupkin | 34\nDETACH DLL<\/code><\/pre>\n\n\n\n<h3>6. \u5bf9\u56de\u8c03\u51fd\u6570\u7684\u652f\u6301<\/h3>\n\n\n\n<p>C++\u4e2d\uff0c\u7528\u51fd\u6570\u6307\u9488\u975e\u5e38\u5bb9\u6613\u5730\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\uff0cpython\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u3002 \u7b2c1\u6b65\uff1a \u7528 CFUNCTYPE() \u5b9a\u4e491\u4e2a\u51fd\u6570\u6307\u9488\uff0cwindows \u4f7f\u7528 WINFUNCTYPE() \u3002 CFUNCTYPE()\u7b2c1\u4e2a\u53c2\u6570\u662f\u56de\u8c03\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u540e\u9762\u662f\u51fd\u6570\u5f62\u53c2\u3002 \u7b2c2\u6b65\uff1a\u5c06\u56de\u8c03\u51fd\u6570\u540d\u8d4b\u7ed9CFUNCTYPE\u53d8\u91cf\uff0c\u76f8\u5f53\u4e8e\u5c06\u51fd\u6570\u5730\u5740\u8d4b\u7ed9\u51fd\u6570\u6307\u9488\u3002 \u7b2c3\u6b65\uff1a\u5c06\u8be5\u56de\u8c03\u51fd\u6570\u6307\u9488\u505a\u4e3a\u53e61\u4e2a\u51fd\u6570\u7684\u5f62\u53c2\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from ctypes import *\nimport sys\n\n<em># \u56de\u8c03\u51fd\u6570\u7c7b\u578b\u5b9a\u4e49<\/em>\nif 'linux' in sys.platform:\n    fun_ctype = CFUNCTYPE\nelse:\n    fun_ctype = WINFUNCTYPE\n\ndef add(int a,int b):\n    return a+b\n\nADDFUNC = fun_ctype(c_int, c_int, c_int)\nadd_callback = ADDFUNC(add)\n<em>#\u5c06CFUNCTYPE\u53d8\u91cf\u505a\u4e3a\u51fd\u6570\u5f62\u53c2<\/em>\ndef test(x,y,ADDFUNC):\n    print(f\"data is {x} and {y} \n    return add_callback(x,y)\n\n<em>#\u5b9e\u9645\u6d4b\u8bd5\u65f6\uff0c\u628a\u56de\u8c03\u51fd\u6570\u505a\u4e3a\u53c2\u6570\u4f20\u5165<\/em>\ntest(10,20,add_callback)<\/code><\/pre>\n\n\n\n<h3>\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<p>1\uff09 ctypes \u5bf9c\/c++ dll\/so\u4e2d\u7684\u51fd\u6570\u4e0e\u5f62\u53c2\uff0c\u9700\u8981\u5148\u7533\u660e\u624d\u80fd\u4f7f\u7528<\/p>\n\n\n\n<p>2\uff09ctypes \u63d0\u4f9b\u4e86\u4e00\u5957\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8eC \u6570\u636e\u7c7b\u578b\u4e0e python \u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c \u6ce8\u610f\u6570\u7ec4\uff0c\u6307\u9488\u7c7b\u578b\u7684\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\uff13) ctypes \u8c03\u7528 c\/c++ dll\/so \u52a8\u6001\u94fe\u63a5\u5e93\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u4e24\u79cd<\/p>\n\n\n\n<ul><li>\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528<\/li><li>\u7b7e\u540d\u51fd\u6570\u7684\u65b9\u5f0f\u8c03\u7528<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4f7f\u7528 ctypes \u8c03\u7528 C\/C++ DLL \u52a8\u6001\u94fe\u63a5\u5e93 Python ctypes\u6a21\u5757\u4f18\u70b9\u4e0e\u9002 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2023\/02\/11\/ctypes\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python \u548c c++ \u8054\u5408\u7f16\u8bd1&#8212;ctypes\u5e93<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/13180"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=13180"}],"version-history":[{"count":11,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/13180\/revisions"}],"predecessor-version":[{"id":13955,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/13180\/revisions\/13955"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=13180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=13180"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=13180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}