0
votes

I am using python34 ctypes to load a DLL that I wrote in C. The purpose of using python to load this is to unit test each function. There exists three functions and two work fine, however, the third function calls malloc and returns a pointer to that new buffer. When I run the python module via py.test it passes the two that return an int/void but fails on the one returning the pointer. Below is the code I am using for both C and python.

C code is compiled with cl.exe cl.exe /DLL /Fetests\test.dll src\msg.c

DLLExport char * func2(char *message, unsigned int message_len) {

    int size = (message_len * 2);
    char *buf;
    buf = (char *)malloc(size);

    if(buf == NULL){
        return NULL;
    }

    return buf;
}

Python code is executed via py.test and below is a function in the test_msg.py module

def test_message():
    lib = cdll.LoadLibrary("./tests/test_dll.dll")
    message = create_string_buffer(b"something")
    size = c_uint(9)
    func = lib.func2
    func.restype = POINTER(c_wchar_p)
    response = func(message, size)
    assert response.contents == b"somethingsomething"

The error I am getting is on the malloc line. If i comment out the malloc I get a pointer back pointing to garbage but it asserts properly in the python code. However, if malloc is in, I get the error below:

OSError: exception: access violation writing 0x00017B52

============================= test session starts =============================
platform win32 -- Python 3.4.3 -- py-1.4.28 -- pytest-2.7.1
rootdir: E:\test-windows, inifile:
collected 3 items

tests\test_func.py .F.

================================== FAILURES ===================================
_________________________________ test_func ___________________________________

    def test_message():
        lib = cdll.LoadLibrary("./tests/test_dll.dll")
        message = create_string_buffer(b"something")
        size = c_uint(9)
        func = lib.func2
        func.restype = POINTER(c_wchar_p)
>       response = func(message, domain)
E       OSError: exception: access violation writing 0x00017B52

tests\test_func.py:18: OSError
===================== 1 failed, 2 passed in 0.09 seconds ======================
1
Are you copying anything to the buffer? Right now, it contains garbage when you return it. You're also passing too many parameters to func(), but this may be a typo. - isanae
I updated the sample and explained the error message. - Kyle
/DLL defines an LL macro. The option to build a DLL is /LD. You probably also want /MD to link dynamically with the CRT. - Eryk Sun
Your restype is wrong. It's a char *, not a wchar_t **. Typically this should be either c_char_p or POINTER(c_char). A simple c_char_p result gets automatically converted to Python bytes. The POINTER(c_char) instance retains the address of the allocated buffer, which you may need to free. - Eryk Sun
Thank you all, eryksun solved the issue. - Kyle

1 Answers

-1
votes

Eryksun in the comments above solved this issue.

I was not compiling the C code properly to a DLL