Haskell FFI - 返回更新的结构

时间:2017-08-29 14:02:34

标签: haskell c2hs haskell-ffi

我想从C调用以下Haskell函数:

void read_params_for (property_list_t *props);

该函数应该接收一些property_list_t并在其中填充一些值,因此调用者具有更新的结构。

我拥有property_list_t所有必需的包装器(如Storable等),但我无法弄清楚如何将此函数包装成类似

的内容
readParamsFor :: ForeignPtr PropertyListT -> IO (ForeignPtr PropertyListT)

我尝试使用C2HS,我也尝试手动编写FFI绑定,如:

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

但在这两种情况下,我都会找回原来“人口稀少”的名单。

如何将更新的结构返回给Haskell?

1 个答案:

答案 0 :(得分:0)

我意识到我想要使用的C库中存在一个错误,如果错误不存在,那么简单的withForeignPtr就足够了。