Re: typedef union
"Mark Tolonen" <[email protected]>
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
"Nils Wagner" <[email protected]> wrote in message news:[email protected]... > On Mon, 15 Mar 2010 20:36:33 -0700 > "Mark Tolonen" <[email protected]> wrote: >> Nils, >> >> Please reply to the mailing list instead of me directly. >> Answers below. >> >> >> >> "Mark Tolonen" <[email protected]> wrote in >>message >> news:[email protected]... >>> >>> "Nils Wagner" <[email protected]> wrote in >>>message >>> news:[email protected]... >>>> Hi all, >>>> >>>> I am newbie to ctypes. >>>> >>>> How can I "translate" the following C-code into Python ? >>>> I don't know how to handle HEAD and memcpy ? >>>> Any pointer would be appreciated. >>>> >>>> Thanks in advance. >>>> >>>> Nils >>>> >>>> >>>> typedef union { >>>> int i; >>>> float f; >>>> char c[4]; >>>> } Word; >>>> >>>> int >>>> I,J,K,N,IDE,IA,IE,IERR,JUNIT,JUNCAT,NDATA,NREC,LREADY,ONE=1; >>>> Word BUF[100],HEAD[30]; >>>> >>>> for (I=5;I<LHEAD;I++) >>>> HEAD[I].i = 0; >>>> HEAD[ 0].i = 1; >>>> HEAD[ 1].i = LHEAD + NDATA*7; >>>> HEAD[ 2].i = LHEAD; >>>> HEAD[ 3].i = NDATA; >>>> HEAD[ 4].i = 7; >>>> memcpy (HEAD[ 7].c,"DSIO",4); >>>> memcpy (HEAD[ 8].c,"TEST",4); >>>> memcpy (HEAD[ 9].c,"NPCO",4); >>>> memcpy (HEAD[10].c," ",4); >>>> memcpy (HEAD[11].c,"DSIO",4); >>>> HEAD[20].i = 1; >>>> HEAD[21].i = NDATA; >>>> STRING = "MM RAD"; >>>> DSEINH(STRING,&HEAD[24].i,&ONE, >>>> strlen(STRING)); >>> >>> Here's a rough translation. Note how the arrays are >>>declared. "c_char*4" >>> is a 4-character array *type*. (Word*100)() is a >>>100-Word array >>> *instance*. >>> To assign to the char array, just use a maximum >>>4-character string. >>> >>> import ctypes >>> >>> class Word(ctypes.Union): >>> _fields_ = [ >>> ('i',ctypes.c_int), >>> ('f',ctypes.c_float), >>> ('c',ctypes.c_char*4)] >>> >>> I=J=K=N=IDE=IA=IE=IERR=JUNIT=JUNCAT=NDATA=NREC=LREADY=ONE=1 >>> BUF = (Word*100)() >>> HEAD = (Word*30)() >>> >>> # not defined in C code >>> LHEAD = 30 >>> >>> for I in range(5,LHEAD): >>> HEAD[I].i = 0 >>> >>> HEAD[ 0].i = 1; >>> HEAD[ 1].i = LHEAD + NDATA*7 >>> HEAD[ 2].i = LHEAD >>> HEAD[ 3].i = NDATA >>> HEAD[ 4].i = 7 >>> HEAD[ 7].c = "DSIO" >>> HEAD[ 8].c = "TEST" >>> HEAD[ 9].c = "NPCO" >>> HEAD[10].c = " " >>> HEAD[11].c = "DSIO" >>> HEAD[20].i = 1 >>> HEAD[21].i = NDATA >>> STRING = "MM RAD" >>> # Not enough info >>> # DSEINH(STRING,&HEAD[24].i,&ONE,strlen(STRING)); >> >>>From your private replies: >> >>> Thank you very much for your help. >>> Here comes information concerning DSEINH. >>> >>> extern void dseinh (char* const,int* const, >>> const int* const,const size_t); >>> >>> How do I call dseinh from python (byref etc.) ? >> >>First load the dynamic library (.dll or .so) using CDLL. >> See (untested) >> example: >> >> dll = ctypes.CDLL('library.dll') >> value1 = ctypes.c_uint() >> value2 = ctypes.c_uint() >> s = "string" >> dll.dseinh(s,ctypes.byref(value1),ctypes.byref(value2),len(s)+1) >> >> Python strings are passed as null-terminated C strings, >>so len(s) + 1 >> accounts for the null. There is also a ctypes.sizeof() >>function that can be >> used with ctypes types and classes derived from >>ctypes.Structure or >> ctypes.Union. >> >> You can (and sometimes have to) declare the parameters >>and return types of a >> function. See the Python ctypes documentation >> (http://docs.python.org/library/ctypes.html). > > Why did you use ctypes.c_uint() and not ctypes.c_int() ? typo. > When shall I use byref() ? When I function takes a pointer to "something", you need to create an instance of "something" and pass a reference to it. So if a function takes an int* in order to return a value in it: void func(int* value); You first create an c_int: v= ctypes.c_int() Then call the function passing a reference: dll.func(ctypes.byref(v)) When the function returns, you get the Python integer result using: v.value -Mark ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev