va_list in JNI

Discussion in 'Java' started by leonwild@hotmail.com, Jun 23, 2005.

  1. Guest

    Can anyone tell me if it is possible to call a C function which
    contains a va_list parameter from java code. I have a existing library
    that I wish to use by creating a Java wrapper around the code.
    , Jun 23, 2005
    #1
    1. Advertising

  2. On Thu, 23 Jun 2005 06:41:52 -0700, leonwild wrote:

    > Can anyone tell me if it is possible to call a C function which
    > contains a va_list parameter from java code. I have a existing library
    > that I wish to use by creating a Java wrapper around the code.


    Depends on what you want to do. You can call a variadic function from JNI
    code just fine, with the usual caveats. However, you can not use
    C-Style variadic arguments to create a java-style variadic function.

    In C, all arguments are pushed to the stack, and variadic functions
    actually go "beyond" the last named argument further down the stack. In
    java, variadic arguments actually _are_an_array_ of the declared type
    (this is why you need to specify type... instead of just ...) that is
    implicitly generated by the compiler once a variadic function is called.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Jun 23, 2005
    #2
    1. Advertising

  3. Guest

    Basically I have C functions in a library which interface with a
    duplicated database system. I don't have the luxury of modifying these
    C functions. What I wanted to do was provide a bridge with JNI so I
    could call the C functions from my Java code. Obviously when selecting
    data you can have any number of columns and data types. The C functions
    have prototypes like:

    int select(char* table, char*columns, char* where, <va_list of
    variables for data retrieval>)

    Can you provide an example of how to pass the va_list parameters to the
    C code so I can retreive the data? I suppose I could write callback
    functions and seperate data structures but I'd like to stay as close to
    the original code as possible.
    , Jun 23, 2005
    #3
  4. On Thu, 23 Jun 2005 07:09:29 -0700, leonwild wrote:


    > int select(char* table, char*columns, char* where, <va_list of
    > variables for data retrieval>)
    >
    > Can you provide an example of how to pass the va_list parameters to the
    > C code so I can retreive the data? I suppose I could write callback
    > functions and seperate data structures but I'd like to stay as close to
    > the original code as possible.


    How are these variables used? Are they used in scanf style (pointers to
    locations?) or are they used as "rvalues", which can only be read, but
    never assigned? If the former, you probably can't do it without jumping
    through more hoops then it's worth. In the later, you might just be able
    to get away with dirty tricks using the knowledge of how the stack is
    organized. DO NOT DO THIS! Whoever will have to look at your program
    afterwards will curse and all your progeny.

    In general, this is quite a hard problem, since you will muck with
    the stack layout (which is a disaster waiting to happen, and about as
    importable as it gets). Are you sure you can not get away with a static
    version of the function? va_list is, as defined, pretty much a read-only
    structure, and creating one on the fly is not really intended.

    If i where you, i would build an intermediate layer, instead of trying to
    force-fit these parts together. But... your program, your problem, your
    endless grief. ;)

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Jun 23, 2005
    #4
  5. Guest

    Thanks for the info. I think I'll write in an extra layer which will
    pretend to be a bit like the JDBC but call my C library functions
    underneath.

    As you say, my headache. (But I've got plenty of paracetamol in my
    desk, lol)
    , Jun 23, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    837
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    892
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,663
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    610
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    787
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page