C FFI question 
Author Message
 C FFI question

Hi,

In Functional Developer, I want to pass a buffer of unsigned chars to a
C function, but I can't figure out how to do it.  There doesn't seem to
be any documention on how to do this task.

define C-function sendpacket
  parameter pdu :: <C-unsigned-char*>;
  parameter length :: <C-unsigned-short>;
  c-name: "sendpacket";
end;

  let data = "hel\<00>lo";
  sendpacket(data, data.size);  // should send out six bytes

Can someone give me a hint?

thanks

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 04 Feb 2003 03:00:00 GMT  
 C FFI question
The string passed to the function must be a <c-string> for this to
work. This can be done using as():

  let c-data = as(<c-string>, data);
  sendpacket(c-data, c-data.size);
  destroy(c-data);

Or a macro to have destroy() called automatically:

  with-c-string(c-data = data)
    sendpacket(c-data, c-data.size);
  end;

Cheers,
Chris.
--
http://www.double.co.nz/dylan



Wed, 05 Feb 2003 08:20:15 GMT  
 C FFI question


Quote:
> The string passed to the function must be a <c-string> for this to
> work. This can be done using as():

>   let c-data = as(<c-string>, data);
>   sendpacket(c-data, c-data.size);
>   destroy(c-data);

Thanks Chris, but a <c-string> is a zero terminated string, and that is
not what I want.  The buffer that I want to pass may have \0's in it.

I'm suspect the streams library is doing what I want to do internally
for writes involving a <byte-vector>, but I don't have access to that
source code to the streams library, so I can't see how they are doing
it.

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 05 Feb 2003 09:32:08 GMT  
 C FFI question

Quote:

> Thanks Chris, but a <c-string> is a zero terminated string, and that
> is not what I want.  The buffer that I want to pass may have \0's in
> it.

What about something like:

  let data = as(<byte-string>, "hel\<00>lo");
  sendpacket(c-type-cast(<c-unsigned-char*>, data), data.size);

I haven't tried it but it may work. I don't even know if the cast is
necessary.

Chris.
--
http://www.double.co.nz/dylan



Thu, 06 Feb 2003 10:15:01 GMT  
 C FFI question


Quote:

> What about something like:

>   let data = as(<byte-string>, "hel\<00>lo");
>   sendpacket(c-type-cast(<c-unsigned-char*>, data), data.size);

> I haven't tried it but it may work. I don't even know if the cast is
> necessary.

Unfortunately your suggestion doesn't work.

This should be pretty basic functionality for a C FFI.  Surely the
streams library must do what I want for reading/writing files and
sockets.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 08 Feb 2003 10:26:48 GMT  
 C FFI question

Quote:


> Unfortunately your suggestion doesn't work.

> This should be pretty basic functionality for a C FFI.  Surely the
> streams library must do what I want for reading/writing files and
> sockets.

I've tested the following and it works, so maybe you can use something
like that:

  let data = as(<byte-string>, "hel\<00>lo");
  with-c-string(s = data)
    doit(s, data.size);
  end;

Note that the size passed is data.size, not s.size.

Chris.
--
http://www.double.co.nz/dylan



Sun, 09 Feb 2003 14:46:47 GMT  
 C FFI question
Thanks Chris... your suggestion works!  I can write to C buffers and
read from them.

    let data = "hel\<00>lo";

    with-c-string(s = data)
      send(s, data.size);
    end;

    // some code omitted

    // after doing a receive
    let eek = make(<string>, size: mystruct.length);
    with-c-string(destination = eek)
      copy-bytes!(destination, mystruct.pdu, mystruct.length);
    end;

    // In the code above mystruct.pdu is a <C-signed-char*> in a
C-struct

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 11 Feb 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. to CS: or not to CS: in F-PC assembler

2. Fun-O Win32 FFI Question

3. c-ffi question involving c-struct

4. FFI question #2: Getting the address of a C function

5. FFI question: Setting array elements

6. FFI questions

7. C-FFI: GC questions, wchar_t, Melange redesign

8. C-FFI: GC questions, wchar_t, Melange redesign

9. FFI Question

10. ACL FFI question

11. FFI question in ACL/W 2.0

12. Basic CS question from R4RS

 

 
Powered by phpBB® Forum Software