CMakeLists.txt
# project(工程名) project(xxx) # add_library(链接库名称 SHARED 链接库代码) add_library(xxx SHARED xxx.cpp)
xxx.cpp
#include <iostream> using namespace std; // c++ 结构体定义 struct struck_ { // 股票名,字符串 char * stock_code_; // 开盘价 double stock_open_; }; // 声明为标准 C 格式导出的函数 extern "C" { // 参数接受结构体指针 int struck_function_(struck_ * struck_pointer_) { cout << struck_pointer_->stock_code_ << endl; cout << struck_pointer_->stock_open_ <<endl; return 0; } }xxx.py
from ctypes import * # python 结构体定义 class py_struct_(Structure): _fields_ = [("stock_code_", c_char_p), ("stock_open_", c_double)] # python 结构体实例化,初始化 py_struct_1 = py_struct_() py_struct_1.stock_code_ = b"Hello world!" py_struct_1.stock_open_ = 123456 # 取结构体指针 py_struct_1_pointer_ = byref(py_struct_1) # 获取 dll 句柄 h_dll_ = CDLL('C:\Users\Perelman\.CLion2016.1\system\cmake\generated\xxx-34f96d0d\34f96d0d\Debug\libxxx.dll') # 打印结果 print(h_dll_.struck_function_(py_struct_1_pointer_))