【发布时间】:2018-05-14 07:28:25
【问题描述】:
我正在使用 ctypes 根据描述 DLL 函数参数和返回的描述文件调用 DLL 文件中的函数。这是该 DLL 中的一个函数,称为 InitNetwork。下面是它的描述:
Function:BOOL InitNetwork(char LocalIP[],char ServerIP[],int LocalDeviceID);
Arguments:LocalIP
ServerIP
LocalDeviceID
Return:Success:TRUE;
Faile:FALSE;
我在 Python 中做的事情是这样的:
from ctypes import *
import os
#Load Dll:
cppdll = CDLL("C:\\VS_projects\\MusicServer_Flask\\NetServerInterface.dll")
#Get the function:
initnetwork = getattr(cppdll, "?InitNetwork@@YAHQAD0H@Z") # The function can be successfully accessed.
#specify arguments' types:
initnetwork.argtypes = [c_char_p,c_char_p,c_int]
在此之后,我尝试:
initnetwork("192.168.1.103", "192.168.1.103", 1111)
得到一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
如果我试试这个:
initnetwork(LocalIP = "192.168.1.103", ServerIP = "192.168.1.103", LocalDeviceID = 1111)
我会得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: this function takes at least 3 arguments (0 given)
为什么会出现这些错误?如何成功调用此函数?任何建议表示赞赏。感谢您的关注!
【问题讨论】: