如果你暂时还没有把自己写的C++程序封装成DLL文件,可参照
https://blog.csdn.net/Xeon_CC/article/details/122597635
直接写代码
准备封装为DLL的C++代码
#define EXPORT __declspec(dllexport)
#include <iostream>
#include <cmath>
#include "list"
using namespace std;
typedef struct test_struct {
int a;
char* b;
}stct;
extern "C" {
EXPORT stct* test_list_func(stct* st) {
st->a = 16;
char carr[] = "qwertyuiop";
st->b = carr;
return st;
}
}
调用DLL文件的Python代码
class test_list_dll就是python自定义类,对应C语言的结构体test_list_dll
import ctypes
from ctypes import *
# lib = ctypes.cdll.LoadLibrary('D:\\AllProjects\\py_projects\\radarGP\\QFupdate\\Dll4.dll')
lib = CDLL('D:\\AllProjects\\py_projects\\radarGP\\QFupdate\\Dll4.dll')
class test_list_dll(Structure):
_fields_ = [('a', c_int), ('b', c_char_p)]
tlf = test_list_dll()
lib.test_list_func.restype = POINTER(test_list_dll)
t = lib.test_list_func(byref(tlf))
print t.contents.a
print t.contents.b
执行结果