[Haskell-cafe] Quickest way to pass Text to C code

James Cook mokus at deepbondi.net
Wed Mar 21 12:35:43 CET 2012


On Mar 21, 2012, at 4:35 AM, Yves Parès wrote:

> Hello,
> 
> I have to interact with a C++ library that accepts as string types (putting c++ strings aside) pointers of wchar_t (CWString in Haskell) or unsigned 32-bit int (Ptr Word32 for UTF-32 codepoints).

The vector package has "storable" vectors, which are essentially raw C arrays.  It provides the function:

	Data.Vector.Storable.unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO b

This is probably the simplest way to do what you're describing.  You can also manually allocate and poke data into raw memory using Foreign.Marshall.Alloc and Foreign.Storable, if you're feeling particularly masochistic ;)

-- James


More information about the Haskell-Cafe mailing list