Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

JNI C++ interface

Thread view: 
Abdur - 26 Jun 2006 12:27 GMT
Hi All,

I am new to Java, I want to access a C++ Class containing function with
variable # arguments (vm_args). Could any one suggest me a solution.

Thanks in advance

Best Regards,
Abdur
Chris Uppal - 26 Jun 2006 13:18 GMT
> I am new to Java, I want to access a C++ Class containing function with
> variable # arguments (vm_args). Could any one suggest me a solution.

You can't directly invoke C functions from Java (and C++ functions are even
less accessible because of C++ name mangling).  What you can do is use JNI to
implement a native method which /calls/ your pre-existing C function.

There /should/ be a useful trail in the Java Tutorial
   http://java.sun.com/docs/books/tutorial/index.html
but it seems to have vanished.  There should also be a good book downloadable
from Sun's site:
   Java Native Interface: Programmer's Guide and Specification
   Sheng Liang
but that seems to have vanished too.  So you'll probably have to buy it unless
you have better luck finding it at java.sun.com than me.

Calling a va_args function creates extra complexity.  Java (pre 1.5) doesn't
support variable-argument lists, so you'd have to pass an array of some sort to
the native method.  In 1.5 variable-argument methods have been added to Java,
but they are just a hack implemented in the compiler, and underneath all that's
happening is that an array is created and filled in before passing that to the
function.  So even in 1.5 your native method would take an array containing the
variable parameters.   You then have the problem of calling (from C or C++)
another varargs function when all you have is an array of the parameters.  If
you are lucky, there will be another version of the same function which takes
an array of parameters (plus a size).  If you are less lucky then there will be
another version which takes an explicit va_list, in which case you can (in a
machine/compiler-dependent way) create a va_list from your array (consult your
nearest C/C++ guru).  Otherwise you'll just have to write a big switch --
something like:

   switch (no_of_params)
   {
   case 1:    variadicMethod(args[0]); break;
   case 2:    variadicMethod(args[0, args[1]]); break;
   case 3:    variadicMethod(args[0], args[1], args[2]); break;
   case 4:    variadicMethod(args[0], args[1], args[2], args[3]); break;
   ....
   }

All a bit messy, I'm afraid...

If you have the budget then you /might/ find JNIWrapper or Xfunction useful.
   http://jniwrapper.com/
   http://www.excelsior-usa.com/xfunction.html
I have never used either of them, and in particular don't know whether either
can deal with varargs functions.

   -- chris
ddimitrov - 26 Jun 2006 23:06 GMT
I have used JNI Wrapper and find it a very good piece of software.

Two free tools which can help with the JNI Wrapper generation is

SWIG (http://www.swig.org/) - I've never used it for Java, but it's
quite easy to wrap a C function in Python

and Jace (http://sourceforge.net/projects/jace/) - abandoned, but
working. Has some quite good ideas.

cheers,
Dimitar


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.