pass parameters to function of dll in python leads to access violation writing 0x00000014

-3

win 7

python 2.7

In python, I call a function in dll using ctype to read flash value in a MCU. But I got the access violation writing 0x00000014 error. dll function:

int get_flash_value(int addr, int readlen, char *data)
{
unsigned  char read_data[4096];
int ret;
int i;
const unsigned int rlen = 4096;
int restlen = 0;
const unsigned int readtimes = (readlen / 4096);
int writelen = 0;
ret = bulk_read_cmd(addr, 32, data);
if (ret)
{
    TRACE("try to dump failure,addr is 0x%x.", addr);
    return -1;
}
for (i = 0; i < readtimes; i++) {
    memset(data, 0x0, 4096);
    ret = bulk_read_cmd(addr + (rlen*i), rlen, data);
    if (ret)
    {
        TRACE("dump failure,addr is 0x%x.", addr + (rlen*i));
        return -1;
    }

    ret = bulk_read_cmd(addr + (rlen*i), restlen, data);
    if (ret)
    {
        TRACE("dump failure,addr is 0x%x.", addr + (rlen*i));
        return -1;

    }



    memcpy(data, read_data, readlen);
    return 0;
}
}







int send_request(const unsigned char *buf, unsigned int len)
{
unsigned int send_cnt;
int exitCode;

EnterCriticalSection(&csSend);
send_cnt = send_req_cnt++;
LeaveCriticalSection(&csSend);

//TRACE("[%u] Enter send_request", send_cnt);

exitCode = push_send_queue(buf, len);
if (exitCode) {
    TRACE("[%u] push_send_queue failed", send_cnt);
} else {
    //TRACE("[%u] push_send_queue succeeded", send_cnt);
}

return exitCode;
}

I got the same error when I tried to use the function to read the flash in the MCU:

Exception thrown at 0x7787EBCB (ntdll.dll) in transferdll.exe: 0xC0000005: Access violation writing location 0x00000014.

error in vs when debug

python
c
asked on Stack Overflow Sep 18, 2018 by include • edited Sep 18, 2018 by include

1 Answer

0

You should declare the argument types first, makes things easier:

maindll.get_flash_value.argtypes = [c_int, c_int, c_char_p]
maindll.get_flash_value.restype = c_int

this needs only be done once for the CDLL instance.

Then of course you need to have an array of sufficient size to call it

# array of char of readlen, initialized to zero
data = (c_char * readlen)()

# call
maindll.get_flash_value(address, readlen, data)

# to get the data as bytes, say:
read_data = bytes(data)
answered on Stack Overflow Sep 18, 2018 by Antti Haapala

User contributions licensed under CC BY-SA 3.0