0
我有以下C
功能,我想從Haskell
撥打:Haskell的FFI - 回報更新結構
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
但是在這兩種情況下,我找回原來的「underpopulated」列表。
我如何獲得更新結構回Haskell?
那麼'Foreign.Storable.poke'? – arrowd
爲什麼你返回另一個指針? 'ForeignPtr PropertyListT - > IO(ForeignPtr PropertyListT)'讀作'property_list_t * read_params_for(property_list_t * props)'。由於後者返回'void',我會使用'ForeignPtr PropertyListT - > IO()'來代替。你不應該需要'newForeignPtr'。 – chi
你的函數可以簡單地寫成'readParamsFor p = withForeignPtr p readParamsFor'>> return p';如果這不起作用,那麼你的實現在其他地方存在問題(並且由於沒有給出可驗證的例子,所以不可能確定它在哪裏)。這與在C函數本身中返回指定爲輸入的指針沒有區別。 – user2407038