如果需要在c\c++和python之间传递数据,则需要用到ctypes。
ctypes是python自带的一个库,不用安装,直接可以调用。
基本用法:
#include "stdafx.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #define DLLEXPORT extern "C" __declspec(dllexport) // 在dll中顶一个add函数 DLLEXPORT int __stdcall add(int a, int b) { int sum = a + b; return sum; }
在Python中调用dll。
import ctypes path = r'E:\01_Lab\VisualStudioLab\cpp_dll\cpp_dll\Debug\cpp_dll.dll' dll = ctypes.WinDLL(path) dll.add(1,2)