From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29459 invoked by alias); 27 Nov 2011 04:05:43 -0000 Received: (qmail 29451 invoked by uid 22791); 27 Nov 2011 04:05:42 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW,TW_BF X-Spam-Check-By: sourceware.org Received: from mail-vw0-f41.google.com (HELO mail-vw0-f41.google.com) (209.85.212.41) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 27 Nov 2011 04:05:21 +0000 Received: by vbbfn1 with SMTP id fn1so4435157vbb.0 for ; Sat, 26 Nov 2011 20:05:21 -0800 (PST) Received: by 10.52.89.3 with SMTP id bk3mr38502809vdb.92.1322366720429; Sat, 26 Nov 2011 20:05:20 -0800 (PST) Received: from [192.168.1.139] (bas3-toronto06-1177890648.dsl.bell.ca. [70.53.47.88]) by mx.google.com with ESMTPS id p17sm12261791vdj.4.2011.11.26.20.05.18 (version=SSLv3 cipher=OTHER); Sat, 26 Nov 2011 20:05:19 -0800 (PST) Message-ID: <4ED1B703.4050708@moxielogic.com> Date: Sun, 27 Nov 2011 04:05:00 -0000 From: Anthony Green User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: libffi-discuss@sourceware.org Subject: Re: Passing function pointer to fcall References: <198099805.151376.1322352353393.JavaMail.open-xchange@ox.hosteurope.de> In-Reply-To: <198099805.151376.1322352353393.JavaMail.open-xchange@ox.hosteurope.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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/msg00220.txt.bz2 On 11/26/2011 7:05 PM, wp1068189-ssc wrote: > Please take a look at this piece of code: > > #include > #include > #include > > int msg(void (*a) (char *)) { > return 23; > // but this doesn't work :-( > a("Hi, there !\n"); > } > > int main(int argc, char ** argv) > { > > > ffi_cif cif; > ffi_abi abi; > ffi_status status; > int nargs = 1; > ffi_type *rtype =&ffi_type_uint32; > ffi_type **atypes; > void **avalues; > void *result; > > atypes = malloc(sizeof(ffi_type)); > atypes[0] =&ffi_type_pointer; > > avalues = malloc(sizeof(ffi_type_pointer)); > // I want to call msg with a pointer to printf > *(void**)avalues[0] = printf; > > status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, rtype, atypes); > > result = (ffi_type *) malloc(sizeof(rtype->size)); > > if(status != FFI_OK) > printf("ffi_prep_cif failed (%i)\n",status); > > ffi_call(&cif,FFI_FN(msg),result,avalues); > > return(*(int *)result); > } > > As you might imagine, I'd like to call a function "msg" and send it a pointer to > printf, > but that does not work. > I guess it has to do with the pointer me is constructing. > > But I have absolutely no idea what I'm doing wrong. > > Can anyone help, please ? Let's get the obvious one of out the way... you are returning from msg() before calling the function. I'm going to assume that's a typo or something. What platform are you working on? Also, you should declare "a" in msg() to have the same prototype as printf. Also, there's an oddity in the libffi API, and that is that "result" needs to be the largest native integral type on your system. So, use a long for result instead of mallocing the exact return type size. You can pass it into ffi_call as &result and simply cast it to an int at the end. Anthony Green