之后就是python调用的代码了:
import ctypes
dll = ctypes.CDLL("D:\\myplace\\add\\Debug\\add.dll")
dll.foo.restype = ctypes.c_char_p
p = dll.foo()
print p
路径什么的可以确定没有问题,运行程序之后就出现下面的异常:
Traceback (most recent call last):
File "D:\workspace\NR8250\src\study\test2.py", line 3, in <module>
dll.foo.restype = ctypes.c_char_p
File "C:\Python26\lib\ctypes\__init__.py", line 366, in __getattr__
func = self.__getitem__(name)
File "C:\Python26\lib\ctypes\__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'foo' not found
不好意思,我刚把这个方法打错了,改了之后报错还是如下:
Traceback (most recent call last):
File "D:\workspace\NR8250\src\study\test2.py", line 3, in <module>
dll.foo.restype = ctypes.c_char_p
File "C:\Python26\lib\ctypes\__init__.py", line 366, in __getattr__
func = self.__getitem__(name)
File "C:\Python26\lib\ctypes\__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'foo' not found
真不知道是什么原因?再次跪求高手解决
之后我的python程序来调用动态库dll文件的foo方法,想得到返回的字符串“hello”:
import ctypes
dll = ctypes.CDLL("D:\\myplace\\add\\Debug\\add.dll")
dll.foo.restype = ctypes.c_char_p
p = dll.foo()
print p
但是我python中的程序调用c程序中的foo方法没有返回字符串"hello",而是出错了,报错如下:
Traceback (most recent call last):
File "D:\workspace\testdll\src\testdll\test.py", line 7, in <module>
dll.test.restype = ctypes.c_char_p
File "C:\Python26\lib\ctypes\__init__.py", line 366, in __getattr__
func = self.__getitem__(name)
File "C:\Python26\lib\ctypes\__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'test' not found
跪求高手帮忙,不知道哪里出错了?