File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes C++'s out parameter object creation and JNA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "C++ Watch "C++ New topic
Author

C++'s out parameter object creation and JNA

Nathan Barraille
Greenhorn

Joined: Mar 01, 2011
Posts: 6

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nathan Barraille
Greenhorn

Joined: Mar 01, 2011
Posts: 6

Thank you Rob.

Using a PointerByReference did the trick!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: C++'s out parameter object creation and JNA