#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)