• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C++'s out parameter object creation and JNA

 
Nathan Barraille
Greenhorn
Posts: 6
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Disclaimer: I ignore pretty much everything about C++, so I hope I'm not saying stupid things here, if I am, please feel free to correct me.

As a Java developer, when I want to create a new object, I use a constructor method that will allocate this object in memory and return a handle on it for me, and I will store this handle in a variable, I do it like this.



But in C++, I've been given to understand, that despite the fact it is possible to do so



I can also define a handle by myself, and then call a initializer on it that will allocate memory and bind the handle on it, like this:



So my question is, what happens when we want to use those C++ methods in Java, with JNA?

Let's suppose I have the following C++ header:



As I don't have any idea on what a Foo is, or what the Foo structure contains, I'm just going to create a JNA PointerType to declare my structure:



Using the createFoo method should be pretty easy as well:



Right?

But my question is, how can I use the initializeFoo function??? I suppose I would have to create a Pointer and give it to the function, but how do I create a non NULL pointer in JNA? I tried the following code, but it results in an EXCEPTION_ACCESS_VIOLATION.



Any idea?

Thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You Java Foo matches a Foo*. Your method requires a Foo**. So you must get a Pointer to the Foo instance. I think that requires a PointerByReference.

If not then perhaps this piece of the Javadoc of Structure.getPointer() may help you out:
Note that if you use the structure's pointer as a function argument, you are responsible for calling write() prior to the call and read() after the call. These calls are normally handled automatically by the Function object when it encounters a Structure argument or return value.
 
Nathan Barraille
Greenhorn
Posts: 6
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob.

Using a PointerByReference did the trick!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic