0

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?

Alexey Raga
  • 7,457
  • 1
  • 31
  • 40
  • Well, `Foreign.Storable.poke`? – arrowd Aug 29 '17 at 14:11
  • 1
    Why do you return another pointer? `ForeignPtr PropertyListT -> IO (ForeignPtr PropertyListT)` reads like `property_list_t *read_params_for(property_list_t *props)`. Since the latter returns `void`, I'd use `ForeignPtr PropertyListT -> IO ()` instead. You shouldn't need `newForeignPtr` for this. – chi Aug 29 '17 at 14:28
  • Your function can be written simply `readParamsFor p = withForeignPtr p readParamsFor' >> return p`; if this doesn't work, there is a problem with your implementation somewhere else (and since there is no verifiable example given, it's impossible to determine where that is). This is no different from returning the pointer given as input in the C function itself. – user2407038 Aug 29 '17 at 17:18
  • @user2407038 That's where I started from, but it didn't work and I assumed that it wasn't right. Looks like I have to check the actual C implementation. – Alexey Raga Aug 29 '17 at 23:15

1 Answers1

0

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.

Alexey Raga
  • 7,457
  • 1
  • 31
  • 40