开发者

ctypes outputting unknown value at end of correct values

开发者 https://www.devze.com 2023-01-05 10:10 出处:网络
I have the following DLL (\'arrayprint.dll\') function that I want to use in Python via ctypes: __declspec(dllexport) void PrintArray(int* pArray) {

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 th开发者_如何学JAVAe 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.)


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号