Sending a two dimensional array to a C plus plus DLL
Andy Nercessian <[email protected]> Fri, 9 Feb 2018 11:48:09 +0200
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CAJWczd96Af4H+8uBwm2BbUbqugcowNqyrb2Lz_ZPeWo+cvJp1Q@mail.gmail.com> |
--===============5192524299801420531==
Content-Type: multipart/alternative; boundary="f403045eb0ea0ebae20564c46d4e"
--f403045eb0ea0ebae20564c46d4e
Content-Type: text/plain; charset="UTF-8"
Dear All,
I'm new to ctypes and was wondering if anyone can help me with the
following:
I have a DLL function as follows:
-----------------------------
int __declspec(dllexport) **test1(int ** image_data);
int ** test1(int ** image_data) {
int i;
i=image_data[0][0];
//image_data[0][1] = 7;
return image_data;
}
------------------------------
and some python code that sends a two dimensional array to it:
------------------------------
from ctypes import *
mydll=cdll.LoadLibrary("c:/.../win32project3.dll")
mydll.test1.argtypes = [POINTER(POINTER(c_int * 3)*5)]
mydll.test1.restype = POINTER(POINTER(c_int * 3)*5)
inner_type=c_int*3
array_type=inner_type*5
image_data=array_type(inner_type(1,1,1),inner_type(2,2,2),inner_type(3,3,3),inner_type(4,4,4),inner_type(5,5,5))
pointer_to_array=POINTER(POINTER(c_int*3)*5)
pi=cast(image_data,pointer_to_array)
pi2=mydll.test1(pi)
array=(pi2[0][0][0])
------------------------------
The program returns an error on the penultimate line of Python and the
i=image_data[0][0]
line of the DLL:
WindowsError: exception: access violation reading 0x000000001
Likewise writing to the array (uncomment the following line) results in the
an access violation writing.. error.
Any ideas?
Many thanks in advance,
--
Andy N
--f403045eb0ea0ebae20564c46d4e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Dear All,<div>I'm new to ctypes and was wondering if a=
nyone can help me with the following:</div><div><br></div><div>I have a DLL=
function as follows:</div><div>-----------------------------</div><div><di=
v><span style=3D"white-space:pre"> </span>int<span style=3D"white-space:pre=
"> </span>__declspec(dllexport)<span style=3D"white-space:pre"> </span>**te=
st1(int ** image_data);</div><div><span style=3D"white-space:pre"> </span>i=
nt<span style=3D"white-space:pre"> </span>** test1(int ** image_data) {</di=
v><div><span style=3D"white-space:pre"> </span>int i;</div><div><span styl=
e=3D"white-space:pre"> </span>i=3Dimage_data[0][0];</div><div><span style=
=3D"white-space:pre"> </span>//image_data[0][1] =3D 7;</div><div><span sty=
le=3D"white-space:pre"> </span></div><div><span style=3D"white-space:pre">=
</span>return image_data;</div><div><span style=3D"white-space:pre"> </sp=
an>}</div><div><br></div><div>------------------------------</div><div>and =
some python code that sends a two dimensional array to it:</div><div><br></=
div><div>------------------------------</div><div><div>from ctypes import *=
</div><div><br></div><div>mydll=3Dcdll.LoadLibrary("c:/.../win32projec=
t3.dll")</div><div><br></div><div>mydll.test1.argtypes =3D [POINTER(PO=
INTER(c_int * 3)*5)]</div><div>mydll.test1.restype =3D POINTER(POINTER(c_in=
t * 3)*5)</div><div><br></div><div>inner_type=3Dc_int*3</div><div>array_typ=
e=3Dinner_type*5</div><div>image_data=3Darray_type(inner_type(1,1,1),inner_=
type(2,2,2),inner_type(3,3,3),inner_type(4,4,4),inner_type(5,5,5))</div><di=
v>pointer_to_array=3DPOINTER(POINTER(c_int*3)*5)</div><div>pi=3Dcast(image_=
data,pointer_to_array)<br></div><div>pi2=3Dmydll.test1(pi)</div><div>array=
=3D(pi2[0][0][0])</div></div><div>------------------------------</div><div>=
<br></div><div>The program returns an error on the penultimate line of Pyth=
on and the=C2=A0</div><div>i=3Dimage_data[0][0]=C2=A0</div><div>line of the=
DLL:</div><div><br></div><div>WindowsError: exception: access violation re=
ading 0x000000001</div><div><br></div><div>Likewise writing to the array (u=
ncomment the following line) results in the an access violation writing.. e=
rror.</div><div>Any ideas?</div><div>Many thanks in advance,</div><div><br>=
</div>-- <br><div class=3D"gmail_signature"><div dir=3D"ltr">Andy N<br></di=
v></div>
</div></div>
--f403045eb0ea0ebae20564c46d4e--
--===============5192524299801420531==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
--===============5192524299801420531==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
--===============5192524299801420531==--