Haskell FFI Question

18 views
Skip to first unread message

ye yan

unread,
Sep 19, 2014, 12:32:29 AM9/19/14
to haskel...@googlegroups.com
I have been play around with Haskell FFI. My question is for ByteString (Strict) is there a trailing null append to it when pass to it C functions?

ye yan

unread,
Sep 19, 2014, 2:08:09 AM9/19/14
to haskel...@googlegroups.com
Another question is how to pass a raw buffer (binary content) from Haskell to C functions? Assume we are using ByteString as the content container in Haskell.

Is there a way to convert ByteString to C void * ?

Aycan iRiCAN

unread,
Sep 19, 2014, 3:00:34 AM9/19/14
to ye yan, haskel...@googlegroups.com
You may want to take a look at Data.ByteString.Unsafe.

_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
http://www.google.com/profiles/iricanaycan

Erik Hesselink

unread,
Sep 19, 2014, 3:20:57 AM9/19/14
to ye yan, haskell
Yes, see the documentation for useAsCString [0].

[0] http://hackage.haskell.org/package/bytestring-0.10.4.0/docs/Data-ByteString.html#v:useAsCString

Erik

On Fri, Sep 19, 2014 at 6:32 AM, ye yan <haskell...@gmail.com> wrote:
> I have been play around with Haskell FFI. My question is for ByteString
> (Strict) is there a trailing null append to it when pass to it C functions?
>

ye yan

unread,
Sep 20, 2014, 3:40:14 AM9/20/14
to haskel...@googlegroups.com, haskel...@haskell.org
Thank you guys, I finally make the FFI working.

But I found a interesting behavior with in my C++ side of code.  I have made a simple thumbnail generation function in C++ side which include the following snippet of code

```
Blob blob;

try {

ye yan

unread,
Sep 20, 2014, 3:47:54 AM9/20/14
to haskel...@googlegroups.com
Sorry, accidentally clicked post;

in haskell FFI  c++ side

    Blob db;

    try {
        sb.update(src, src_len);
    } catch (Magick::Exception &error) {
//do something with error
}   


if I compile my program in to c++ program this code works perfectly fine. The exception can be handled correctly. But if link this piece of code into haskell (main is in haskell side), the exception handle code will never be triggered. However if I change the code to

   Blob db;

    try {
        sb.update(src, src_len);
    } catch (...) {
//can  really do any thing but tell user this function failed
}   

the exception handle part can be triggered in this way. My question is : how can I catch exception without using (...) in Haskell FFI?




On Friday, September 19, 2014 2:02:29 PM UTC+9:30, ye yan wrote:
Reply all
Reply to author
Forward
0 new messages