# Questions about FFI, ForeignPtr, and Opaque Types

**URL:** <https://discourse.haskell.org/t/questions-about-ffi-foreignptr-and-opaque-types/6914>\
**Category:** Learn\
**Created:** [July 8, 2023, 11:22pm UTC](https://discourse.haskell.org/t/questions-about-ffi-foreignptr-and-opaque-types/6914 "2023-07-08T23:22:14Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![glguy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/glguy/32/3359_2.png) [@glguy](https://discourse.haskell.org/u/glguy)\
**Post date:** [July 12, 2023, 6:31pm UTC](https://discourse.haskell.org/t/questions-about-ffi-foreignptr-and-opaque-types/6914/14 "2023-07-12T18:31:36Z")

</div>

Here’s the cleaned up version

```haskell
module Help where

import Data.Word (Word32)
import Foreign.C (CString, CInt(..), withCString)
import Foreign.ForeignPtr (ForeignPtr, newForeignPtr, FinalizerPtr)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (Ptr, FunPtr)
import Foreign.Storable (peek)

data BotanHashStruct

-- int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags)
foreign import ccall unsafe botan_hash_init :: Ptr (Ptr BotanHashStruct) -> CString -> Word32 -> IO CInt

-- int botan_hash_destroy(botan_hash_t hash)
foreign import ccall "&botan_hash_destroy" botan_hash_destroy_funptr :: FinalizerPtr BotanHashStruct
-- this is a cheat because it ignores the 'int' return on hash_destroy

newtype BotanHash = BotanHash (ForeignPtr BotanHashStruct)

throwBotan :: CInt -> IO a
throwBotan x = fail ("Botan error: " ++ show x) -- do something better here

newBotanHash :: String -> IO BotanHash
newBotanHash name =
  alloca $ \outPtr ->
  withCString name $ \namePtr ->
  botan_hash_init outPtr namePtr 0 >>= \ret ->
  if 0 /= ret then throwBotan ret else
  peek outPtr >>= \out ->
  newForeignPtr botan_hash_destroy_funptr out >>= \fp ->
  pure (BotanHash fp)

```

---

_[View the full topic](https://discourse.haskell.org/t/questions-about-ffi-foreignptr-and-opaque-types/6914)._
