I am trying to use a dll in Python source using the ctypes. I began reading it from Python documentation as I am new to this. After successfully loading the dll in Python, when I try to pass a string to a function which expects a char *, it gives me
“ValueError: Procedure probably called with too many arguments (4
bytes in excess)”.
I also tried looking at other posts but couldn’t resolve the problem.
I tried different approaches to pas this string such as using byref() and pointer() but it didn’t change the outcome. I also tried with WINFUNCTYPE but failed. The dll that I’m using is windll.
Here’s a test program I wrote in 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
Here’s how the D_InitTester is implemented in a cpp file which is available in dll exports,
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;
}
Your help is greatly appreciated.
Most likely the cause of the error is a mismatch of calling conventions. I’m guessing your C++ DLL exports functions with
cdeclconvention but your use ofWinDLLimpliesstdcall.Create your library like this to use
cdecl: