MikeRand
MikeRand

Reputation: 4838

ctypes outputting unknown value at end of correct values

I have the following DLL ('arrayprint.dll') function that I want to use in Python via ctypes:

__declspec(dllexport) void PrintArray(int* pArray) {
    int i;
    for(i = 0; i < 5; pArray++, i++) {
        printf("%d\n",*pArray);
    }
}

My Python script is as follows:

from ctypes import *

fiveintegers = c_int * 5
x = fiveintegers(2,3,5,7,11)
px = pointer(x)

mydll = CDLL('arrayprint.dll')
mydll.PrintArray(px)

The final function call outputs the following:

2
3
5
7
11
2226984

What is the 2226984 and how do I get rid of it? It doesn't look to be the decimal value for the memory address of the DLL, x, or px.

Thanks,

Mike

(Note: I'm not actually using PrintArray for anything; it was just the easiest example I could find that generated the same behavior as the longer function I'm using.)

Upvotes: 1

Views: 70

Answers (1)

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

mydll.PrintArray.restype = None
mydll.PrintArray(px)

By default ctypes assumes the function returns an integral type, which causes undefined behavior (reading a garbage memory location).

Upvotes: 3

Related Questions