Calling a C++ dll from Java

Discussion in 'Java' started by Samagna, Sep 7, 2004.

  1. Samagna

    Samagna Guest

    I need to call a function that is there in a C++ dll. The arguments to
    the function are STRUCT and string. The return type is void. Also I
    donot have any rights to change the dll and I have to use as it is....
    So here goes my questions: 1. How to pass a struct variable from Java
    to a function in DLL. 2. The function returns void and the second
    argument is the output variable which I need to use. So is there
    anyway, that call by reference can be used from Java? 3. Where should
    I place my dll to compile it?

    Quick reply would be appreicated.
    Thanks in advance.
    Samagna
    Samagna, Sep 7, 2004
    #1
    1. Advertising

  2. Samagna

    Chris Uppal Guest

    Samagna wrote:

    > I need to call a function that is there in a C++ dll. The arguments to
    > the function are STRUCT and string.


    You can't, I'm afraid. Java does not have the ability to call arbitrary code
    from an arbitrary DLL, nor does it understand 'structs'.

    So you have to be a bit more indirect. What you will have to do is write your
    own C or C++ code which follows the JNI conventions, and which therefore /can/
    be called from Java, and then call the other DLL from that.

    The Sun JNI tutorial at:

    http://java.sun.com/docs/books/tutorial/native1.1/index.html

    should get you started on JNI programming. It's not difficult if you keep it
    simple, and it doesn't sound as if you need to do anything very complicated for
    these purposes.

    -- chris
    Chris Uppal, Sep 7, 2004
    #2
    1. Advertising

  3. (Samagna) writes:

    > I need to call a function that is there in a C++ dll. The arguments to
    > the function are STRUCT and string. The return type is void. Also I
    > donot have any rights to change the dll and I have to use as it is....


    You need to make an intermediate layer in JNI that convert Java
    arguments into the necessary C++ arguments. Since Java does not have
    "out" parameters, you need to use a return value of some sort from
    your Java native method.
    Tor Iver Wilhelmsen, Sep 7, 2004
    #3
  4. Samagna

    Stian Bakken Guest

    There are commercial software that will wrap .dll's for you. Here are two:
    http://www.excelsior-usa.com/xfunction.html
    http://www.jniwrapper.com/index.jsp

    Disclaimer: I have not tried any of these, but they might suit your needs.

    Good luck,
    Stian

    "Samagna" <> wrote in message
    news:...
    > I need to call a function that is there in a C++ dll. The arguments to
    > the function are STRUCT and string. The return type is void. Also I
    > donot have any rights to change the dll and I have to use as it is....
    > So here goes my questions: 1. How to pass a struct variable from Java
    > to a function in DLL. 2. The function returns void and the second
    > argument is the output variable which I need to use. So is there
    > anyway, that call by reference can be used from Java? 3. Where should
    > I place my dll to compile it?
    >
    > Quick reply would be appreicated.
    > Thanks in advance.
    > Samagna
    Stian Bakken, Sep 8, 2004
    #4
  5. Depending on what you are trying to do, the IBM Java-COM bridge can be
    a MUCH MUCH easier way of moving data between the two than hand
    building JNI stuff. I have used this with Microsoft MapPoint and some
    other MS-ish API's quite successfully.
    http://www-106.ibm.com/developerworks/library/j-intbridge/
    Robert kebernet Cooper, Dec 19, 2004
    #5
  6. Samagna

    Tilman Bohn Guest

    [f'up2 cljp]

    In message <>,
    Robert kebernet Cooper wrote on 19 Dec 2004 15:35:19 -0800:

    [...]
    > http://www-106.ibm.com/developerworks/library/j-intbridge/


    This looks very interesting, I hadn't known about that! Could come in
    very handy indeed in an upcoming project, so thanks from me for that
    pointer!

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Dec 20, 2004
    #6
    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. Anushi
    Replies:
    5
    Views:
    8,630
  2. Naresh Agarwal

    jvm.dll vs java.dll

    Naresh Agarwal, Jun 14, 2005, in forum: Java
    Replies:
    5
    Views:
    3,782
  3. Capsule

    calling c# dll from c++ dll

    Capsule, Oct 18, 2005, in forum: C++
    Replies:
    3
    Views:
    947
    Victor Bazarov
    Oct 18, 2005
  4. Replies:
    2
    Views:
    502
    Larry Bates
    Mar 9, 2007
  5. H. Simpson
    Replies:
    4
    Views:
    244
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page