我想从Python调用DLL中的函数。但是我收到了这个错误:
"Attribute Error function not found"
这是我的代码:
import os
import ctypes
os.chdir("C:\\Program Files\\Compact Automated Testing System V2.0")
# Load DLL into memory.
CATSDll = ctypes.WinDLL ("CATS.dll")
# Set up prototype and parameters for the desired function call.
CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8,ctypes.c_double)
CATSDllApiParams = (1, "p1", 0), (1, "p2", 0),
# Actually map the call (setDACValue) to a Python name.
CATSDllApi = CATSDllApiProto (("setDACValue", CATSDll), CATSDllApiParams)
# Set up the variables and call the Python name with them.
p1 = ctypes.c_uint8 (1)
p2 = ctypes.c_double (4)
CATSDllApi(p1,p2)
但DLL文档显示了setDACValue
函数ChannelId
和DAC Voltage
& Traceback (most recent call last):
File "C:\Users\AEC_FULL\Softwares\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydevd.py", line 2235, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Users\AEC_FULL\Softwares\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydevd.py", line 1661, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Users\AEC_FULL\Saravanan\Workspace\CATS\CATS.py", line 18, in <module>
CATSDllApi = CATSDllApiProto (("setDACValue", CATSDll), CATSDllApiParams)
AttributeError: function 'setDACValue' not found
作为输入。
以上内容基于StackOverflow提供的一段代码。
我也尝试过使用cdll.LoadLibrary&amp;的简单方法。然后调用该函数,但也会出现相同的错误。
有谁能告诉我什么是错的?感谢。
完整追溯:
class func signUp(username : String , completion: (status : NSString)->Void) {
var url: NSURL = NSURL(string: "http://localhost/WhosIn/signup.php")!
var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
var bodyData = "username=Datforis&name=firas&password=123"
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
var status = "error"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
println(response)
if(error != nil){
completion(status: status)
}
else if data == nil {
println("Server could not be reached please check your internet connection")
status = "connectionerror"
completion(status: status)
}
else {
println("i'm here")
let json = JSON(data : data)
var msgint : Int = json["status"].int!
status = String(msgint)
println(status)
completion(status: status)
}
}
}
答案 0 :(得分:2)
当你指定函数的原型时,你不仅要指定参数的类型,还要指定返回类型作为WINFUNCTYPE的第一个arg。因此,行
CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8,ctypes.c_double)
应替换为
CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8, ctypes.c_uint8,ctypes.c_double)
答案 1 :(得分:0)
试试这个。您可能需要使用WinDLL
,但更有可能使用CDLL
:
from ctypes import *
cats = CDLL('CATS')
cats.setDACValue.argtypes = [c_uint8,c_double]
cats.setDACValue.restype = c_uint8
cats.setDACValue(1,4)
如果仍无法找到setDACValue
,则可以使用Microsoft工具dumpbin
列出DLL中导出的函数名称:
dumpbin /exports CATS.dll
Visual Studio安装在安装目录下的VC \ bin目录中。