Search code examples
pythoncshared-librariesexecution

Where does C function execution stop


In the project folder I have:

libtest.so
test.h

I have imported ctypes in my python code (not sure if it was necessary) and I loaded .so file like following:

lib = cdll.LoadLibrary('./libtest.so')

In "test.h" C header file I have:

long TEST_API test( 
                        ___OUT_ char text[41],
                        ___OUT_ char* pcReceiptSignature,

                        );

I call "test" from python code like following:

class StructA(Structure):
    _fields_ = [("text", c_char*41),
                 ("pcReceiptSignature",c_char*1),
                ]



sampleStruct = StructA("","")

def test2():

    res = lib.test(sampleStruct.text,                               sampleStruct.pcReceiptSignature)
return res

but execution fails somewhere and I never get to "return res" line.

For the lengths specified for c_chars defined in the structured I also tried giving them no lengths (no change).

What am I doing wrong here. Is there anyway to see where the execution fails?


Solution

  • You really should run the code under debugger, compiling the .so with -g etc.

    But one obvious error in your original code would be that char* pcReceiptSignature is declared an out parameter? thus I guess you want to reserve some space for it, to be filled by the function, but none is available (you are using c_char_p instead of an array!). The pcRpcReceiptSignature is a pointer to the constant string "" and thus overwriting it would not be very safe.