2

I am working on JNA for my application and I am confused about using typedef struct pointer in java. Please check my code and guide me. Thank you in advance!

Below is my c++ code

typedef struct tagHWDeviceInfo
{
USHORT          VendorID;      // vendor id
USHORT          ProductID;     // product id
DWORD           nXExt;         // firmware width
DWORD           nYExt;         // firmware height
DWORD           preesure;      // pressure level
DWORD           penState;      // pen state
}HWDeviceInfo, *PHWDeviceInfo;

and java code

public class HWDeviceInfo extends Structure{
    short VendorID;
    short ProductID;
    int   nXExt;        // firmware width
    int   nYExt;        // firmware height
    int   preesure;     // pressure level
    int   penState;
}

Now my question is: what does it mean by *PHWDeviceInfo in c++ code and how can I use this pointer in my java code?

Boris Schegolev
  • 3,601
  • 5
  • 21
  • 34
  • You might want to [read this about pointers/references](http://softwareengineering.stackexchange.com/a/141838). – Murat Karagöz Nov 03 '16 at 09:46
  • What exactly don't you understand? It's a pointer. See here: http://stackoverflow.com/questions/1543713/c-typedef-of-pointer-to-structure – Claude Martin Nov 03 '16 at 10:08
  • And you can't really use pointers in Java code. What could you do with it? – Claude Martin Nov 03 '16 at 10:09
  • have you worked with the JNA before? we can use Pointer class with the jna.jar. I know we cannot use pointer in java but please understand my question. – Areeba Irfan Ullah Khan Nov 03 '16 at 10:30
  • @СӏаџԁеМаятіи see this article [JNA Overview](https://jna.java.net/javadoc/overview-summary.html) – Areeba Irfan Ullah Khan Nov 03 '16 at 10:41
  • What's there to see? You ask how you could use a pointer in Java. The answer is: you can't. And I want to know why you think you could do anything with a pointer in Java. You can load that data into a java object of type HWDeviceInfo. And you can use points to structs in your C++ code. – Claude Martin Nov 03 '16 at 12:38

1 Answers1

1

JNA automatically converts instances of Structure to struct * in function calls, and to struct in structure field definitions. Structure.getPointer() gives you the pointer that JNA will use.

You can modify this default behavior by tagging your class and/or parameter types with the Structure.ByReference and Structure.ByValue interfaces.

JNA documents this clearly as noted by @areeba-irfan-ullah-khan.

technomage
  • 9,861
  • 2
  • 26
  • 40