1. calling C function array pointer
- Posted by rlistas <rlistas at gmail.com> Jul 15, 2007
- 543 views
- Last edited Jul 16, 2007
Hi, How do I call a C function in a dll that asks an argument like *buffer ( a pointer to an array) ? (from Pcomm.ew) --(int port, char *buf, int len) global function sio_read(integer port, integer lpRBuf, integer len) return c_func(sio_read_,{port,lpRBuf,len}) end function I try : sequence buffer buffer={'0','0'} sio_read(1,buffer[1],2) >error -8 and sio_read(1,buffer,2) >incorrect type for lpRBuf. Thanks Rubens
2. Re: calling C function array pointer
- Posted by Jason Gade <jaygade at yahoo.c?m> Jul 16, 2007
- 557 views
rlistas wrote: > > Hi, > > How do I call a C function in a dll that asks an argument > like *buffer ( a pointer to an array) ? > > (from Pcomm.ew) > --(int port, char *buf, int len) > global function sio_read(integer port, integer lpRBuf, integer len) > return c_func(sio_read_,{port,lpRBuf,len}) > end function > > > I try : > > sequence buffer > buffer={'0','0'} > > sio_read(1,buffer[1],2) > >error -8 > > and sio_read(1,buffer,2) > >incorrect type for lpRBuf. > > > Thanks > > Rubens > > I'm not the expert on this, but in order to use a C pointer you will have to use allocate() and peek()/poke() lpRBuf will have to be declared as an atom. (untested)
global function sio_read(integer port, atom lpRBuf, integer len) return c_func(sio_read_,{port,lpRBuf,len}) end function atom buffer buffer=allocate(2) if buffer = 0 then puts("Unable to allocate buffer!\n") abort(1) end if sequence bytes_read bytes_read = sio_read(1,buffer,2) free(buffer) -- make sure to free memory when you are done with it. -- then use peek(buffer) and peek(buffer+1) to read back your data.
References: http://www.rapideuphoria.com/lib_a_b.htm#allocate http://www.rapideuphoria.com/lib_e_g.htm#free http://www.rapideuphoria.com/lib_p_r.htm#peek http://www.rapideuphoria.com/lib_p_r.htm#poke -- "Any programming problem can be solved by adding a level of indirection." --anonymous "Any performance problem can be solved by removing a level of indirection." --M. Haertel "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.
3. Re: calling C function array pointer
- Posted by Jason Gade <jaygade at yahoo.?om> Jul 16, 2007
- 575 views
I forgot to say -- don't free your buffer before you've peeked or poked it! It would probably be best to wrap this entire thing up into one function that allocates, pokes what you need in your buffer, calls your c func, peeks what you need from your buffer, frees your buffer, and then returns your data. -- "Any programming problem can be solved by adding a level of indirection." --anonymous "Any performance problem can be solved by removing a level of indirection." --M. Haertel "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.