无法将.dll文件加载到python脚本中

时间:2015-03-09 07:11:36

标签: python c dll

我想从windows上的python脚本调用C函数。因为我在python中包含了.dll的C代码。但是它不会调用该函数,而在运行python时没有显示任何错误。

C代码:sum.c

#include<stdio.h>
int our_function(int num_numbers, int numbers)
{
  return num_numbers*numbers;
}

的Python:sum.py

import ctypes
sum = ctypes.cdll.LoadLibrary('C:\Python27\sum.dll')
our_function = sum.our_function
our_function.restype = ctypes.c_int
our_function.argtypes = [ctypes.c_int,ctypes.c_int]
result = our_function(10,5)
print result

DLL文件由Cygwin编译器生成。

有人能告诉我这有什么问题吗?

1 个答案:

答案 0 :(得分:0)

可能你在问题上有一些拼写错误。

return num_numbers*number; // should be numbers (number is not declared)

Python代码拼写错误:

sum = ctypes.cdll.LoadLibrary('C:\Python27\sum.dll')
# should be r'C:\Python27\sum.dll' or 'C:\\Python27\\sum.dll'

并使用明确的 _cdecl 约定:

int _cdecl our_function(int num_numbers, int numbers)