Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. FFI and opac struct (PICCA Frederic-Emmanuel)
2. Re: FFI and opac struct (PICCA Frederic-Emmanuel)
----------------------------------------------------------------------
Message: 1
Date: Sun, 24 Jan 2016 09:15:26 +0000
From: PICCA Frederic-Emmanuel
<[email protected]>
To: "[email protected]" <[email protected]>
Subject: [Haskell-beginners] FFI and opac struct
Message-ID:
<a2a20ec3b8560d408356cac2fc148e53b301e...@sun-dag3.synchrotron-soleil.fr>
Content-Type: text/plain; charset="us-ascii"
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@v9v\fw\ACK","@v\EOT<v@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
------------------------------
Message: 2
Date: Sun, 24 Jan 2016 10:18:34 +0000
From: PICCA Frederic-Emmanuel
<[email protected]>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell" <[email protected]>
Subject: Re: [Haskell-beginners] FFI and opac struct
Message-ID:
<a2a20ec3b8560d408356cac2fc148e53b301e...@sun-dag3.synchrotron-soleil.fr>
Content-Type: text/plain; charset="us-ascii"
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 ;)
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 91, Issue 28
*****************************************