python DLL

#ifndef PCH_H
#define PCH_H

// 添加要在此处预编译的标头
#include "framework.h"

#define IMPORT_DLL extern "C" _declspec(dllimport)


typedef struct StructPointerTest
{
    char name[20];
    int age;
    int arr[3];
    int arrTwo[2][3];
}StructTest, * StructPointer;

IMPORT_DLL int add(int a, int b);
IMPORT_DLL char* getRandData(char* arg);

IMPORT_DLL StructPointer test();
IMPORT_DLL void free_test(StructPointer p);

#endif //PCH_H
// pch.cpp: 与预编译标头对应的源文件

#include "pch.h"
#include <string.h>
#include <stdlib.h>

// 当使用预编译的头时,需要使用此源文件,编译才能成功。
int add(int a, int b)
{
	return a + b;
}

char* getRandData(char* arg) 
{
	return arg;
}

StructPointer test() // 返回结构体指针
{
	StructPointer p = (StructPointer)malloc(sizeof(StructTest));

	memset(p, 0, sizeof(StructTest));
	strcpy_s(p->name, "Lakers");


/*	strcpy_s(p->name, 6, "Lakers");
	p->age = 20;
	p->arr[0] = 3;
	p->arr[1] = 5;
	p->arr[2] = 10;
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
			p->arrTwo[i][j] = i * 10 + j;
	}
*/
	return p;
}

void free_test(StructPointer p)
{
	if(p) free(p);
}

import ctypes
from ctypes import *

open_cv_dll = CDLL('D://source//pydll//Dll1//x64//Debug//dll1.dll')
nResult = open_cv_dll.add(8, 6)
print(nResult)

# input parameter is char* and return parameter is char*
open_cv_dll.getRandData.restype = c_char_p
res = open_cv_dll.getRandData(b'hello')
print(res.decode())


class StructPointer(ctypes.Structure):
    _fields_ = [("name", ctypes.c_char * 20),
                ("age", ctypes.c_int),
                ("arr", ctypes.c_int * 3),
                ("arrTwo", (ctypes.c_int * 2) * 3)
                ]


open_cv_dll.test.restype = ctypes.POINTER(StructPointer)
p = open_cv_dll.test()

print(p.contents.name.decode())
print(p.contents.age)
print(p.contents.arr[0])
print(p.contents.arr[:])
print(p.contents.arrTwo[0][:])
print(p.contents.arrTwo[1][:])

open_cv_dll.free_test(p)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值