[C2hs] How to use withCStringLen?

Thomas DuBuisson thomas.dubuisson at gmail.com
Tue Nov 24 10:50:46 EST 2009


This isn't part of c2hs but part of Haskells FFI.

I suggest you see:
http://haskell.org/ghc/docs/latest/html/libraries/base/Foreign-C-String.html#v:withCStringLen

Basically, if you need to call a C routine but your argument for it is
a Haskell String then you can convert the Haskell String to a C string
before passing:

withCStringLen "c2hs has nothing to do with this" ( \(strPtr, strLen) ->
            some_imported_c_function strPtr strLen)

As for the code being different in c2hs - its the same except where
you are using special c2hs macros {# ... #}.  If you haven't already
seen the tutorial [1], I suggest you read that as a start.  Also, its
easier to help if you include the code you mention.

Cheers,
Thomas

[1] http://www.cse.unsw.edu.au/~chak/haskell/c2hs/docu/

On Tue, Nov 24, 2009 at 3:23 AM, Magicloud Magiclouds
<magicloud.magiclouds at gmail.com> wrote:
> Hi,
>  I am new to c2hs, while learning and trying it out, I am confused on
> how to use withCStringLen.
>  Well, I mean how to use it in .chs file, since the code style is a
> little different from .hs.
> Thanks.
> --
> 竹密岂妨流水过
> 山高哪阻野云飞
> _______________________________________________
> C2hs mailing list
> C2hs at haskell.org
> http://www.haskell.org/mailman/listinfo/c2hs
>


More information about the C2hs mailing list