2

I have a class in c++ called kitten

class kitten
{
public:
 void ChangeColor(int newColor);
 int GetColor();
}

Now using JNI I want to give my java program the ability to use this class.

I know how to use JNI to give my java program the ability to call C style functions, but this is a member function.

I thought about something like add a C style function called kitten* CreateNewKitten();, which will have one line : return new kitten();

And then somehow return this native kitten object to JAVA and then the JAVA code will call C style function :

ChangeColor(kitten* myKitten, int newColor)
{
   myKitten->changeColor(color)
}

To make things clear, my question is : Is it possible to return my native kitten to java code ? how ? is it the right approach.

OopsUser
  • 4,642
  • 7
  • 46
  • 71

1 Answers1

1

The generic problem is how to pass a pointer from C++ to Java and receive it back, there is nothing special in this pointer. One of the best articles is an old post by Roman Kennke, but the short answer can be found on SO:

you can use Java long to get and set pointers, unless in your natie architecture pointers are more than 64 bit.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307