A DLL provides a pointer to a 1D array in my C code like this:
JavaScript
x
5
1
__int16 *data = Msg_RowAt(surfaceMsg, rowIdx);
2
3
//access the values like this
4
data[iterator]
5
I’m building a Python project where I need to access the values of the same array. I’ve tried the following but get an access violation when I try iterate surfaceData.
JavaScript
1
6
1
surfaceDataPtr = Msg_RowAt(surfaceMsg, row)
2
ptr = ctypes.POINTER(ctypes.c_int16)
3
surfaceData = ctypes.cast(surfaceDataPtr, ptr)
4
print("ptr: " + str(surfaceData))
5
print("val: " + str(surfaceData[1]))
6
I’m accessing the wrong memory location but I’m not too sure what I’ve done wrong. Can anyone see what I’m doing wrong?
Advertisement
Answer
You can define restype
:
JavaScript
1
3
1
c.Msg_RowAt.restype = ctypes.POINTER(ctypes.c_int16)
2
surfaceDataPtr = Msg_RowAt(surfaceMsg, row)
3
Then, you can randomly access to each element:
JavaScript
1
4
1
print(surfaceDataPtr[0])
2
print(surfaceDataPtr[1])
3
4