I am porting visual c++ 6 application to python using ctypes.
It uses same windows library dll. and I am only porting windows forms to python application.
and I am stucked when I am sending cpp structure to dll in python.
I have tried to find solution on the struct differeces. from cpp code, there is a struct tagging(tagTest) but there is no equivalent in python code. is there any solution for that?
this is cpp that I want to port from.
========================cpp part================================
BOOL CSMessage(HWND hWnd, const char* string1, const char* string2, const char* ltagTest, int structlen);
typedef struct tagTest
{
char item1 [ 8]; char _item1;
char item2 [ 1]; char _item2;
} TcTest, *LPTest;
void Initiator()
{
TcTest ctest ={0};
memset(&ctest,0x20,sizeof TcTest);
move(ctest.item1,"TESTSTRI");
move(ctest.item2,"1");
mhandler.CSMessage(GetSafeHwnd(),"String","Call",(char*)&ctest,sizeof TcTest);
}
=========================pythonpart============================
csMessageProto=ctypes.WINFUNCTYPE(BOOL, HWND, c_char_p, c_char_p, c_char_p, c_int)
csMessageParams=((1,"hWnd",0),(1,"string1",0),(1,"string2",0),(1,"ltagTest",0),(1,"structlen",0))
CSMessage=csMessageProto(("CSMessage",testDll), csMessageParams)
class tagTest(ctypes.Structure):
_fields_ = [
('item1', c_char*8),('_item1', c_char),
('item2', c_char*1),('_item2', c_char)
]
PtagTest=ctypes.POINTER(tagTest)
utestblock=tagTest()
utestblock.item1=str("TESTSTRI")
utestblock.item2=str(1)
CSMessage(hwnd, "String", "Call", ctypes.byref(utestblock), ctypes.sizeof(utestblock));
revision history
- added declaration of dll function both cpp and python
- revised with @avl_sweden suggestions
=====================result==============================
after applying @avl_sweden’s suggestion,
After test, the error msg is this.
ctypes.ArgumentError: argument 4: <type 'exceptions.TypeError'>: wrong type
and for the type
print "Is Type : "+str(ctypes.byref(utestblock))
Is Type : <cparam 'P' (023e5968)>
=========================================================
so it’s looks like types are different in def and instance.
in first line in python code
1: csMessageProto=ctypes.WINFUNCTYPE(BOOL, HWND, c_char_p, c_char_p, c_char_p, c_int)
the fourth c_char_p is not matching
16: CSMessage(hwnd, "String", "Call", ctypes.byref(utestblock), ctypes.sizeof(utestblock));
to match the differences.
I changed code to
1: csMessageProto=ctypes.WINFUNCTYPE(BOOL, HWND, c_char_p, PtagTest, c_char_p, c_int)
and code returns no error. but I cannot find if it is sent through. becuase there was no return from dll.
do you think this is the right solution?
=====================================================================================
i have been tested and it seems it is right solution.
Thank you again.
I see four things wrong here:
First, your python Structure definition doesn’t match the one in the C-code snippet. The python one should be:
The second thing I see, is that you’re sending a pointer to a pointer to the CSMessage function.
Could you try:
The third issue is less severe, and this is that the string
"TESTSTRING"does not fit into 8 bytes.The fourth issue I see, is that the types of the default parameters is wrong.
Try this instead: