FFI and foreign function returning a structure

Hi, If I have a C function returning a structure (not a pointer, but structure itself), can this be accomodated via FFI? I re-read the FFI Addendum, and my conclusion is most likely "No". I am asking just to make sure this is not only my finding. Dimitry Golubovsky Middletown, CT

Dimitry Golubovsky wrote:
Hi,
If I have a C function returning a structure (not a pointer, but structure itself), can this be accomodated via FFI?
No. The way data is organised in memory is dramatically different in Haskell when compared with C. You need to write functions to read in each field in turn and then "reconstruct" the structure on the Haskell side. It's a tedious process. My advice is that if you have a lot of structures to read, write a (simple) preprocessor to generate the marshalling code.. that's what I did. Ben.

I think this is relation to my problem.
I wnant to write Haskell interface to FFmpeg. So first, I try to port
output_example.c to Haskell. But output_example.c's wants to initialize
structure like this,
void write_audio_frame(AVFormatContext *oc, AVStream *st)
{
int out_size;
AVCodecContext *c;
AVPacket pkt;
av_init_packet(&pkt);
c = &st->codec;
get_audio_frame(samples, audio_input_frame_size, c->channels);
pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size,
samples);
pkt.pts= c->coded_frame->pts;
pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.data= audio_outbuf;
/* write the compressed frame in the media file */
if (av_write_frame(oc, &pkt) != 0) {
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
}
then I need to return a structure.
But I know that :
On Wed, 02 Mar 2005 14:45:54 +1100, Ben Lippmeier
No. The way data is organised in memory is dramatically different in Haskell when compared with C. You need to write functions to read in each field in turn and then "reconstruct" the structure on the Haskell side.
It's a tedious process. My advice is that if you have a lot of structures to read, write a (simple) preprocessor to generate the marshalling code.. that's what I did.
so I wrote a code like this,
(This use hsc2hs to write "read and write each field".)
-----------------------------------------------------------------------------
-- -*- mode: haskell -*-
{-# OPTIONS -fglasgow-exts #-}
#include
participants (3)
-
Ben Lippmeier
-
Dimitry Golubovsky
-
shelarcy