[Haskell-beginners] FFI, export a ByteString
Antoine Latter
aslatter at gmail.com
Sun Jun 2 16:53:28 CEST 2013
The function 'castStablePtrToPtr' does not actually return a pointer
you can usefully de-reference in C - it just gives you a value that
will fit into a 'void*' in C-land.
A StablePtr is, conceptually, just a pointer-sized index into an array
maintained by the Haskell runtime, you can only de-reference it in
Haskell. But you can pass it around in C because it fits into a
pointer-sized variable or struct member.
On Thu, Apr 11, 2013 at 10:59 AM, Kees Bleijenberg
<k.bleijenberg at lijbrandt.nl> wrote:
> My function in Haskell takes a CString and returns a ByteString (of Word8).
>
> I tried to put this function in a Haskell made Windows dll. I want to use
> this function in a program written in C.
>
>
>
> The most simple program that compiled was:
>
>
>
> Import qualified B.ByteString as B
>
> import Foreign.StablePtr
>
> import Foreign.C.String
>
> import Foreign.Ptr
>
>
>
> foreign export stdcall parseRuitType :: CString -> IO (Ptr ())
>
> parseRuitType s = do
>
> p <- (newStablePtr ( B.pack [10,11,12]))
>
> return $ castStablePtrToPtr p
>
>
>
> For testing I ignore s.
>
>
>
> In the C-program :
>
> hs_init(NULL,NULL)
>
> res = parseRuitType (“5” )
>
> No errors, but the returned value res (or the dereferenced res) makes no
> sense (not 10,11,12).
>
>
>
> My idea was: the result of the function(a ByteString of Word8) must be
> locked. No garbage collection. But newStablePtr does not return a pointer.
> So I have to convert the newStablePtr to a real pointer and then coerce this
> in the C program to a pointer to a array of word8…?
>
> I’ve tried to return the ByteString in the Haskell function. But now the
> compiler complains: Unacceptable result type in foreign declaration :
> B.ByteString
>
>
>
> I’am missing the big picture. Any ideas?
>
>
>
> Greetings
>
> Kees
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
More information about the Beginners
mailing list