Python与C++动态链接库交互 win10平台

基础部分请参考dalao写的这篇博客,本篇博客主要学习各类数据的交互。

普通数据

主要指的是intfloatchar之类的,举个例子:
在python文件中

import ctypes

lib = ctypes.cdll.LoadLibrary("D:/360MoveData/Users/Erick/Desktop/Dll/x64/Debug/Dll.dll")
print(lib.func(ctypes.c_int(10)))

在DLL.cpp中复制下面的文件:

// Dll.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"


// Dll2.cpp: 定义 DLL 应用程序的导出函数。
#define EXPORT __declspec(dllexport)

#include<iostream>
using namespace std;

extern "C" {

    EXPORT double func(int x) {
        cout << "Get arg: " << x << endl;
        return 3.14;
    }
}

为了保证精度,都使用double类型的数据。编译完成后,执行python文件,即可输出:

Get arg: 10
3.14

数组类型的数据

传入数组

向C语言函数传递数组数据:

import ctypes

lib = ctypes.cdll.LoadLibrary("D:/360MoveData/Users/Erick/Desktop/Dll/x64/Debug/Dll.dll")

INPUT = ctypes.c_int * 9  # 相当于声明了长度为9的数据
input = INPUT()

ls = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

t = 0
for i in range(3):
    for j in range(3):
        input[t] = ls[i][j]
        t += 1

lib.read(input)

在C语言函数中:

#include "stdafx.h"

// Dll2.cpp: 定义 DLL 应用程序的导出函数。
#define EXPORT __declspec(dllexport)

#include<iostream>
using namespace std;

extern "C" {
    EXPORT void read(int* arr) {
        for (int i = 0; i < 9; ++i) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
}

传出结构体、数组

C语言不能直接向Python返回数组,需要借助结构体进行实现,先看python代码:

import ctypes

lib = ctypes.cdll.LoadLibrary("D:/360MoveData/Users/Erick/Desktop/Dll/x64/Debug/Dll.dll")


class MyStruct(ctypes.Structure):
    _fields_ = [("name", ctypes.c_char * 10),
                ("score", ctypes.c_double),
                ("number", ctypes.c_int * 4)]


lib.foo.restype = ctypes.POINTER(MyStruct)
p = lib.foo()

print(p.contents.name)
print(p.contents.score)
ls = p.contents.number
for i in ls:
    print(i)

注意两个地方,必须要继承ctype.Structure,同时所有的结构必须是在_fields这个域中,同时需要包括名字,因为后期需要使用名字进行索引。获取值的时候,在contents这个域中获取。

再来看C++的代码:

// Dll.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <stdint.h>

// Dll2.cpp: 定义 DLL 应用程序的导出函数。
#define EXPORT __declspec(dllexport)

#include<iostream>
using namespace std;

struct MyStructure {
    char name[10];
    double score;
    int number[4];
};

extern "C" {
    EXPORT MyStructure* foo() {
        auto p = new MyStructure;
        p->name[0] = 'T';
        p->name[1] = 'o';
        p->name[2] = 'm';
        p->name[3] = '\0';
        p->score = 98.383;
        p->number[0] = 1;
        p->number[1] = 0;
        p->number[2] = 0;
        p->number[3] = 1;
        return p;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值