如何使用ctypes从python的MSVC编译的dll中调用函数?

问题描述

我想包装一些C代码并通过ctypes在python中调用

//example.c
#include <stdio.h>
#include "example.h" //contains foo() declaration.

void foo(){
  printf("Hello World!\n");
}

使用Visual Studio命令行编译器,我创建了一个.dll文件

cl /LD example.c创建了一个名为example.dll文件

然后我尝试像这样在python中调用foo:

# example.py
import os
os.add_dll_directory(os.getcwd())
from ctypes import *

read_dll = WinDLL('example.dll')

foo = read_dll.foo #AttributeError: function 'foo' not found

然后我从AttributeError: function 'foo' not found的最后一行收到错误消息:example.py

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)