Python调用C语言的时候,如何将Python自定义对象作为C语言的指针传参到C语言的函数里面?

5 篇文章 0 订阅

如果你暂时还没有把自己写的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

执行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值