代码之家  ›  专栏  ›  技术社区  ›  MikeRand

在正确值的末尾输出未知值的ctypes

  •  1
  • MikeRand  · 技术社区  · 15 年前

    我有下面的DLL('arrayprint.DLL')函数,我想通过ctypes在Python中使用它:

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

    我的Python脚本如下:

    from ctypes import *
    
    fiveintegers = c_int * 5
    x = fiveintegers(2,3,5,7,11)
    px = pointer(x)
    
    mydll = CDLL('arrayprint.dll')
    mydll.PrintArray(px)
    

    最后的函数调用输出以下内容:

    2
    3
    5
    7
    11
    2226984
    

    2226984是什么?我该怎么处理?它看起来不是DLL、x或px的内存地址的十进制值。

    谢谢,

    (注意:我实际上并没有使用PrintArray做任何事情;这是我能找到的最简单的例子,它生成了与我正在使用的较长函数相同的行为。)

    1 回复  |  直到 15 年前
        1
  •  3
  •   Matthew Flaschen    15 年前
    mydll.PrintArray.restype = None
    mydll.PrintArray(px)
    

    默认情况下,ctypes假定函数返回整数类型,这会导致未定义的行为(读取垃圾内存位置)。