FFI question #2: Getting the address of a C function 
Author Message
 FFI question #2: Getting the address of a C function

Hi,

I'm trying to fill in a C struct with C function pointers.  I want to do
something like:

  callbacks.function-1 := address-of-c-function("foo");

where foo is the name of C function that is being linked in with the
Dylan executable as a C library, and "address-of-c-function" is some
magic compiler functionality that I am hoping that the FFI has.

I know I can get the address of a C function via this:

  define c-address foo-pointer :: <c-function-pointer>
    c-name: "foo";
  end;

but I would prefer something shorter though because I have many
callbacks, and  I don't need for them to be named in Dylan.  I think
that define c-address macro must use a function that does what I want,
but I don't know how to get at it.

thanks in advance...

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 07 Jan 2003 03:00:00 GMT  
 FFI question #2: Getting the address of a C function

[...]

Quote:
> I'm trying to fill in a C struct with C function pointers.  I want to do
> something like:

>   callbacks.function-1 := address-of-c-function("foo");

 This function cannot exist since such functionality is not part
of the C runtime.

Quote:
>   define c-address foo-pointer :: <c-function-pointer>
>     c-name: "foo";
>   end;

 This works because it uses the linker to make sure the address of
foo is placed in a linkage entry.

[...]

Quote:
>[...]  I think
> that define c-address macro must use a function that does what I want,
> but I don't know how to get at it.

 Nope.

 Now part of what you want could be done in C by using dynamic loader
functionality (via dlsym() or shl_findsym() on HP-UX), and probably
in Dylan via the proper C-FFI binding, but it requires the function
to be exported from the library in the first place.

 Regards - Eric

--



Tue, 07 Jan 2003 03:00:00 GMT  
 FFI question #2: Getting the address of a C function

Quote:


> [...]
> > I'm trying to fill in a C struct with C function pointers.  I want
to do
> > something like:

> >   callbacks.function-1 := address-of-c-function("foo");

>  This function cannot exist since such functionality is not part
> of the C runtime.

It doesn't have to be a part of the C runtime.  The argument
specifying the function name is a string constant (in this case "foo").
I think the address of the C function can be resolved before the Dylan
program starts executing.

My hunch is that the FFI has the functionality I am describing, but it
is not documented.  But I could very well be wrong.  What does the
"define c-function" macro expand to?

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 08 Jan 2003 03:00:00 GMT  
 FFI question #2: Getting the address of a C function

Quote:
> My hunch is that the FFI has the functionality I am describing, but it
> is not documented.  But I could very well be wrong.  What does the
> "define c-function" macro expand to?

C-function-definer can't be defined as an ordinary macro, and is
probably a magic hook into the compiler's innards.

In Gwydion Dylan, what you want to do would be possible using
c-expr("&foo"), mainly because we're compiling via C.  Without knowing
what the FD compiler's hooks are, there is no way to know whether it's
possible or not there.




Wed, 08 Jan 2003 03:00:00 GMT  
 FFI question #2: Getting the address of a C function

Quote:

> My hunch is that the FFI has the functionality I am describing, but it
> is not documented.  But I could very well be wrong.  What does the
> "define c-function" macro expand to?

Although I haven't tried any of it, look at the source code for
x86-win32-operating-system.dylan in the sources/lib/operating-system
directory. There you will see code like:

  %call-c-function ("GetVersionExA", c-modifiers: "__stdcall")
    (lpOSVersionInfo :: <raw-c-pointer>) => (success? :: <raw-c-signed-int>)
        (primitive-cast-raw-as-pointer(primitive-string-as-raw(buffer)))

Perhaps you can work out how to call these functions. Possible in the
dylan-direct-c-ffi library. Searching this library using the browser
might highlight other useful functions.

Let me know if you get it going as I'd be interested.

Cheers,
Chris.
--
http://www.double.co.nz/dylan



Wed, 08 Jan 2003 03:00:00 GMT  
 FFI question #2: Getting the address of a C function

Quote:
>Hi,

>I'm trying to fill in a C struct with C function pointers.  I want to do
>something like:

>   callbacks.function-1 := address-of-c-function("foo");

>where foo is the name of C function that is being linked in with the
>Dylan executable as a C library, and "address-of-c-function" is some
>magic compiler functionality that I am hoping that the FFI has.

>I know I can get the address of a C function via this:

>   define c-address foo-pointer :: <c-function-pointer>
>     c-name: "foo";
>   end;

>but I would prefer something shorter though because I have many
>callbacks, and  I don't need for them to be named in Dylan.  I think
>that define c-address macro must use a function that does what I want,
>but I don't know how to get at it.

>thanks in advance...

Here's the expansion of the definition of foo-pointer.

   define variable foo-pointer
     = make(<c-function-pointer>,
            address: primitive-wrap-machine-word
                       (primitive-cast-pointer-as-raw
                          (%c-variable-pointer("foo", #f))));

You'll need to use the dylan-direct-c-ffi module from
functional-dylan to access the definitions above.  (The second
argument to %c-variable-pointer is the value of the import: option to
c-address-definer.)

   - Gary Palter,
     Functional Objects, Inc.



Wed, 08 Jan 2003 03:00:00 GMT  
 FFI question #2: Getting the address of a C function
With the info that Gary supplied, I was able to write a macro to get the
address of a C function.  It seems to compile and link fine.

use dylan-direct-c-ffi;  // in fuctional-dylan library

define macro address-of-c-function
  { address-of-c-function (?c-function-name:expression, import:
?import:expression) }
    => { make(<c-function-pointer>,
           address: primitive-wrap-machine-word
                     (primitive-cast-pointer-as-raw
                       (%c-variable-pointer(?c-function-name,
?import)))); }
  { address-of-c-function (?c-function-name:expression) }
    => { make(<c-function-pointer>,
           address: primitive-wrap-machine-word
                     (primitive-cast-pointer-as-raw
                       (%c-variable-pointer(?c-function-name, #f)))); }
end;

It can be used as follows:

address-of-c-function("foo", import: #t);

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 13 Jan 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Getting address of C function from assembler

2. Initializing DX to Code Segment(CS) Memory Address...?!?

3. Initializing DX to Code Segment(CS) Memory Address…?!?

4. Calling Haskell function from C (FFI) causes seg-fault (GHC,linux)

5. CMU CL FFI and variadic C functions

6. some clear examples for FFI call to a Windows api function in Franz Allegro for Windows

7. to CS: or not to CS: in F-PC assembler

8. Fun-O Win32 FFI Question

9. c-ffi question involving c-struct

10. C FFI question

11. FFI question: Setting array elements

12. FFI questions

 

 
Powered by phpBB® Forum Software