From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4255 invoked by alias); 27 Nov 2011 00:06:18 -0000 Received: (qmail 4236 invoked by uid 22791); 27 Nov 2011 00:06:17 -0000 X-SWARE-Spam-Status: No, hits=1.3 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from wp102.webpack.hosteurope.de (HELO wp102.webpack.hosteurope.de) (80.237.132.109) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 27 Nov 2011 00:05:56 +0000 Received: from app02.ox.hosteurope.de ([92.51.170.9]); authenticated by wp102.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.0:RSA_ARCFOUR_MD5:16) id 1RUSFl-0003gF-Df; Sun, 27 Nov 2011 01:05:53 +0100 Date: Sun, 27 Nov 2011 00:06:00 -0000 From: wp1068189-ssc Reply-To: wp1068189-ssc To: "libffi-discuss@sourceware.org" Message-ID: <198099805.151376.1322352353393.JavaMail.open-xchange@ox.hosteurope.de> Subject: Passing function pointer to fcall MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact libffi-discuss-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libffi-discuss-owner@sourceware.org X-SW-Source: 2011/txt/msg00219.txt.bz2 Please take a look at this piece of code: =C2=A0 #include #include #include int msg(void (*a) (char *)) { =C2=A0=C2=A0=C2=A0 return 23; =C2=A0=C2=A0=C2=A0 // but this doesn't work :-( =C2=A0=C2=A0=C2=A0 a("Hi, there !\n"); } int main(int argc, char ** argv) =C2=A0=C2=A0=C2=A0 { =C2=A0=C2=A0=C2=A0 ffi_cif cif; =C2=A0=C2=A0=C2=A0 ffi_abi abi; =C2=A0=C2=A0=C2=A0 ffi_status status; =C2=A0=C2=A0=C2=A0 int nargs =3D 1; =C2=A0=C2=A0=C2=A0 ffi_type *rtype =3D &ffi_type_uint32; =C2=A0=C2=A0=C2=A0 ffi_type **atypes; =C2=A0=C2=A0=C2=A0 void **avalues; =C2=A0=C2=A0=C2=A0 void *result; =C2=A0=C2=A0=C2=A0 atypes =3D malloc(sizeof(ffi_type)); =C2=A0=C2=A0=C2=A0 atypes[0] =3D &ffi_type_pointer; =C2=A0=C2=A0=C2=A0 avalues =3D malloc(sizeof(ffi_type_pointer)); =C2=A0=C2=A0=C2=A0 // I want to call msg with a pointer to printf =C2=A0=C2=A0=C2=A0 *(void**)avalues[0] =3D printf; =C2=A0=C2=A0=C2=A0 status =3D ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, rt= ype, atypes); =C2=A0=C2=A0=C2=A0 result =3D (ffi_type *) malloc(sizeof(rtype->size)); =C2=A0=C2=A0=C2=A0 if(status !=3D FFI_OK) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 printf("ffi_prep_cif failed (%i)= \n",status); =C2=A0=C2=A0=C2=A0 ffi_call(&cif,FFI_FN(msg),result,avalues); =C2=A0=C2=A0=C2=A0 return(*(int *)result); =C2=A0=C2=A0=C2=A0 } =C2=A0 As you might imagine, I'd like to call a function "msg" and send it a point= er to printf, but that does not work. I guess it has to do with the pointer me is constructing. =C2=A0 But I have absolutely no idea what I'm doing wrong. =C2=A0 Can anyone help, please ?=C2=A0