Haskell FFI - return updated structure

102 views Asked by At

I have the following C function that I want to call from Haskell:

void read_params_for (property_list_t *props);

The function is supposed to receive some property_list_t and populate some values within it, so the caller then has an updated structure.

I have all the necessary wrappers for property_list_t (like Storable, etc.), but I can't figure out how to wrap this function into something like

readParamsFor :: ForeignPtr PropertyListT -> IO (ForeignPtr PropertyListT)

I tried using C2HS, and I also tried writing FFI bindings manually like:

foreign import ccall "read_params_for"
    readParamsFor' :: Ptr PropertyListT -> IO ()

readParamsFor :: ForeignPtr PropertyListT -> IO (ForeignPtr PropertyListT)
readParamsFor ps = do
    withForeignPtr ps $ \ps' -> do
        res <- readParamsFor' ps'
        pl <- newForeignPtr propertyListDestroy ps'
        return pl

But in both cases, I get back my original "underpopulated" list.

How do I get an updated structure back to Haskell?

1

There are 1 answers

0
Alexey Raga On BEST ANSWER

I realised that there was a bug in a C library that I wanted to use and that indeed, a simple withForeignPtr is sufficient if the bug is not there.