
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

I found the error [HklFactory 0xb7772920,HklFactory 0xb7772a6c,HklFactory 0xb7772a98,HklFactory 0xb7772ac4,HklFactory 0xb7772bf8,HklFactory 0xb7772ef8,HklFactory 0xb77734f0,HklFactory 0xb7773524,HklFactory 0xb77736c0,HklFactory 0xb7773784,HklFactory 0xb77738a8,HklFactory 0xb77738dc,HklFactory 0xb7773908,HklFactory 0xb77739cc] ["TwoC","E4CH","SOLEIL MARS","E4CV","K4CV","E6C","SOLEIL SIRIUS KAPPA","K6C","PETRA3 P09 EH2","SOLEIL SIRIUS TURRET","SOLEIL SIXS MED2+3","SOLEIL SIXS MED1+2","SOLEIL SIXS MED2+2","ZAXIS"] the signature of the C methode was wrong Ptr HklFactory -> HklFactory foreign import ccall unsafe "hkl.h hkl_factory_name_get" c_hkl_factory_name_get :: HklFactory -> IO CString thanks for your help ;)
participants (1)
-
PICCA Frederic-Emmanuel