Fwd: FFI bindings to prodecure with an in-out parameter

Dear Haskell Beginners, I've gotten stuck developing an FFI binding to libspotify. One of the functions I'm trying to bind to, sp_session_create, uses an in-out parameter. Luckily I have an example of its usage here (in the definition of main): https://developer.spotify.com/technologies/libspotify/docs/12.1.45/jukebox_8... It looks to me like they've allocated a pointer to an sp_session and then passed a pointer to that to sp_session_create. My best attempt tries to do the same but comes across a missing Storable instance of the equivalent of the sp_session data structure. Because the C definition of this struct is not exposed in the header files I can't work out how to do it myself. The alternative of passing in an empty pointer with the assumption that the procedure will allocate memory for us simply segfaults. I've put a source distribution up here (hit "raw" to download): https://github.com/mrehayden1/hmdfm/blob/master/dist/hmdfm-0.1.0.0.tar.gz The project can be built automatically with cabal and GHC with hsc2hs installed. Also if you like libspotify can be downloaded and automatically installed (for *nix users I think at least) from here: https://developer.spotify.com/technologies/libspotify/#download Please, can anyone help? Matthew Hayden

It's been a while since I've done anything with C, but if I'm reading
that right it's expecting a pointer pointer as the second argument. In
other words, it wants the address of a pointer that it will update
with a pointer to the instantiated instance of the session structure.
See if this helps:
http://www.haskell.org/haskellwiki/FFICookBook#Passing_pointer-to-pointer-to...
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Wed, Dec 19, 2012 at 4:49 PM, Matthew Hayden
Dear Haskell Beginners,
I've gotten stuck developing an FFI binding to libspotify. One of the functions I'm trying to bind to, sp_session_create, uses an in-out parameter. Luckily I have an example of its usage here (in the definition of main):
https://developer.spotify.com/technologies/libspotify/docs/12.1.45/jukebox_8...
It looks to me like they've allocated a pointer to an sp_session and then passed a pointer to that to sp_session_create.
My best attempt tries to do the same but comes across a missing Storable instance of the equivalent of the sp_session data structure. Because the C definition of this struct is not exposed in the header files I can't work out how to do it myself. The alternative of passing in an empty pointer with the assumption that the procedure will allocate memory for us simply segfaults.
I've put a source distribution up here (hit "raw" to download): https://github.com/mrehayden1/hmdfm/blob/master/dist/hmdfm-0.1.0.0.tar.gz The project can be built automatically with cabal and GHC with hsc2hs installed.
Also if you like libspotify can be downloaded and automatically installed (for *nix users I think at least) from here: https://developer.spotify.com/technologies/libspotify/#download
Please, can anyone help?
Matthew Hayden
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I'm glad my message finally got through!
I've fixed this one now. It was segfaulting because I was missing some
(undocumented) necessary data in one of the other arguments.
Passing it a Ptr (Ptr a) with alloca does the trick, and no need to
allocate memory for the opaque struct.
Thanks,
Matt
On 20 December 2012 03:11, Kyle Murphy
It's been a while since I've done anything with C, but if I'm reading that right it's expecting a pointer pointer as the second argument. In other words, it wants the address of a pointer that it will update with a pointer to the instantiated instance of the session structure. See if this helps:
http://www.haskell.org/haskellwiki/FFICookBook#Passing_pointer-to-pointer-to...
-R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat.
On Wed, Dec 19, 2012 at 4:49 PM, Matthew Hayden
wrote: Dear Haskell Beginners,
I've gotten stuck developing an FFI binding to libspotify. One of the functions I'm trying to bind to, sp_session_create, uses an in-out parameter. Luckily I have an example of its usage here (in the definition of main):
https://developer.spotify.com/technologies/libspotify/docs/12.1.45/jukebox_8...
It looks to me like they've allocated a pointer to an sp_session and then passed a pointer to that to sp_session_create.
My best attempt tries to do the same but comes across a missing Storable instance of the equivalent of the sp_session data structure. Because the
C
definition of this struct is not exposed in the header files I can't work out how to do it myself. The alternative of passing in an empty pointer with the assumption that the procedure will allocate memory for us simply segfaults.
I've put a source distribution up here (hit "raw" to download):
https://github.com/mrehayden1/hmdfm/blob/master/dist/hmdfm-0.1.0.0.tar.gz
The project can be built automatically with cabal and GHC with hsc2hs installed.
Also if you like libspotify can be downloaded and automatically installed (for *nix users I think at least) from here: https://developer.spotify.com/technologies/libspotify/#download
Please, can anyone help?
Matthew Hayden
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (2)
-
Kyle Murphy
-
Matthew Hayden