在python中,无法将字符串传递给dll中函数调用的char*arg

2024-09-24 08:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用ctypes在Python源代码中使用dll。我开始从Python文档中阅读它,因为我对它还不熟悉。在Python中成功加载dll之后,当我试图将一个字符串传递给一个需要char *的函数时,它给了我

"ValueError: Procedure probably called with too many arguments (4 bytes in excess)".

我也试着看其他帖子,但没能解决问题。在

我尝试了不同的方法来pas这个字符串,比如使用byref()pointer(),但它并没有改变结果。我也尝试了WINFUNCTYPE,但失败了。我使用的dll是windll。在

下面是我用python编写的一个测试程序:

from ctypes import *

lpDLL=WinDLL("C:\some_path\myDll.dll")
print lpDLL

IP_ADDR = create_string_buffer('192.168.100.228')
#IP_ADDR = "192.168.100.228"
#IP_ADDR = c_char_p("192.168.100.228")

print IP_ADDR, IP_ADDR.value

D_Init=lpDLL.D_Init
D_InitTester=lpDLL.D_InitTester

#D_InitTesterPrototype = WINFUNCTYPE(c_int, c_char_p)
#D_InitTesterParamFlags = ((1, "ipAddress", None),)
#D_InitTester = d_InitTesterPrototype(("D_InitTester", lpDLL), D_InitTesterParamFlags)

try:
    D_Init()
    D_InitTester("192.168.100.254")
except ValueError, msg:
    print "Init_Tester Failed"
    print msg

下面是如何在一个cpp文件中实现D_InitTester,该文件在dll导出中可用

^{pr2}$

非常感谢你的帮助。在


Tags: 字符串ipinitmsgctypesaddrdllprint
1条回答
网友
1楼 · 发布于 2024-09-24 08:32:11

错误的原因很可能是调用约定不匹配。我猜你的C++ DLL用^ {CD1}}约定导出函数,但是你使用的^ {CD2}}暗示^ {CD3}}。在

像这样创建库以使用cdecl

lpDLL=CDLL("C:\some_path\myDll.dll")

相关问题 更多 >