一、建一个cpp的dll。项目名为PyTestDll
头文件
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
int __stdcall Add(int a, int b);
#ifdef __cplusplus
}
#endif
源文件
#include "pch.h"
int __stdcall Add(int a, int b)
{
return a + b;
}
二、python使用
# -*- coding: utf-8 -*-
import os
CUR_PATH = os.path.dirname(__file__)
dllPath = os.path.join(CUR_PATH, "PyTestDll.dll")
print(dllPath)
import ctypes
dll = ctypes.cdll.LoadLibrary(dllPath)
a = ctypes.c_int(12)
b = ctypes.c_int(11)
print(str(dll.Add(a, b)))