1. calling C function array pointer

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

new topic     » topic index » view message » categorize

2. Re: calling C function array pointer

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: calling C function array pointer

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.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu