[Haskell-beginners] FFI, export a ByteString

Kees Bleijenberg k.bleijenberg at lijbrandt.nl
Thu Apr 11 17:59:10 CEST 2013


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

 

 

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130411/b270bd47/attachment.htm>


More information about the Beginners mailing list