[Haskell-beginners] FFI and opac struct

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Sun Jan 24 09:15:26 UTC 2016


Hello

Here the code I try to use

I get an array of (HklFactory *) via the GetAll method and I want to read for each of these HklFactory the name of the factory with the NameGet method.


-- data HklFactory
newtype HklFactory = HklFactory (Ptr HklFactory) deriving (Show, Storable)

hklFactoryGetAll :: IO [HklFactory]
hklFactoryGetAll = alloca $ \ptr -> do
                 factories <- c_hkl_factory_get_all ptr
                 n <- peek ptr
                 peekArray n factories

foreign import ccall unsafe "hkl.h hkl_factory_get_all"
  c_hkl_factory_get_all :: Ptr Int
                                     -> IO (Ptr HklFactory)

hklFactoryNameGet :: Ptr HklFactory
                                -> IO String
hklFactoryNameGet factory = do
  name <- c_hkl_factory_name_get factory
  peekCString name

foreign import ccall unsafe "hkl.h hkl_factory_name_get"
  c_hkl_factory_name_get :: Ptr HklFactory
                                           -> IO CString

main :: IO ()
main = do
    initGUI

    factories <- hklFactoryGetAll
    names <- mapM hklFactoryNameGet factories
    print factories
    print names


the signature of the C method are:

typedef struct _HklFactory HklFactory;

HKLAPI HklFactory **hkl_factory_get_all(size_t *n) HKL_ARG_NONNULL(1);

HKLAPI const char *hkl_factory_name_get(const HklFactory *self) HKL_ARG_NONNULL(1);


when I run the haskell code, I just get garbage when I try to extract the name of all HklFActoies

here the output

[HklFactory 0xb777b920,HklFactory 0xb777ba6c,HklFactory 0xb777ba98,HklFactory 0xb777bac4,HklFactory 0xb777bbf8,HklFactory 0xb777bef8,HklFactory 0xb777c4f0,HklFactory 0xb777c524,HklFactory 0xb777c6c0,HklFactory 0xb777c784,HklFactory 0xb777c8a8,HklFactory 0xb777c8dc,HklFactory 0xb777c908,HklFactory 0xb777c9cc]
[",v+v<w\STX","2v,vw\EOT","2v|.vw\EOT","2v\DC40vw\EOT","5v\\3v\DC4w\EOT","_8v86v\DC4w\ACK","z at v9v\fw\ACK","@v\EOT<v at w\ACK","EvBvw\ACK","GvEvw\ENQ","yNvGvw\ACK","NvHJvw\EOT","Nv,Lv$w\ENQ","PvNvw\EOT"]

Instead of human readable names I get this garbage.

So I do not understand what is going on and what is wrong in my haskell code.

thanks for your help


Frederic



More information about the Beginners mailing list