[英]Can't pass a string to char * arg of function call in dll in python
我正在嘗試使用ctypes在Python源代碼中使用dll。 我開始從Python文檔中讀取它,因為我是新手。 在Python中成功加載dll之后,當我嘗試將字符串傳遞給期望char *
的函數時,它給了我
“ValueError:過程可能用太多的參數調用(超過4個字節)”。
我也試過看其他帖子但無法解決問題。
我嘗試了不同的方法來使用這個字符串,例如使用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
以下是在dll導出中可用的cpp文件中實現D_InitTester的方法,
D_API int D_InitTester(char *ipAddress)
{
int err = ERR_OK;
if (LibsInitialized)
{
...
some code;
...
else
{
err = hndl->ConInit(ipAddress);
}
if ( 0 < err )
{
err = ERR_NO_CONNECTION;
}
else
{
nTesters = 1;
InstantiateAnalysisClasses();
InitializeTesterSettings();
if(NULL != hndl->hndlFm)
{
FmInitialized = true;
}
}
}
else
{
err = ERR_NOT_INITIALIZED;
}
return err;
}
非常感謝您的幫助。
很可能導致錯誤的原因是調用約定不匹配。 我猜你的C ++ DLL導出函數與cdecl
約定,但你使用WinDLL
意味着stdcall
。
像這樣創建你的庫來使用cdecl
:
lpDLL=CDLL("C:\some_path\myDll.dll")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.