0

Here i am calling giveIntGetInt(int a) function from JAVA .
C++ DLL

 int simpleDLL::giveIntGetInt(int a)  
          {
             int result = a *2;
            return result;
           }

JAVA Method to call C++ function using JNA

public class Main {
    public interface simpleDLL extends Library {

        simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary(
            (Platform.isWindows() ? "simpleDLL" : "simpleDLLLinuxPort"), simpleDLL.class);

        int giveIntGetInt(int a);       // int giveIntGetInt(int a);

    }

    public static void main(String[] args) {
        System.loadLibrary("simpleDLL");
        simpleDLL sdll = simpleDLL.INSTANCE;


        int a = 3;
        int result1 = sdll.giveIntGetInt(a); 
        System.out.println("giveIntGetInt("+a+"): " + result1);


    }
}

Output

giveIntGetInt(3): 181972

Whhy am i getting a Garbage value when the result should be 6 ? Am i correctly mapping to the Java type to Native type ? What am i missing here ?

Thank You

  • Java `int` is 32 bits. C++ `int` could be anything over 16 bits. So first question are they the same size? – user4581301 May 11 '17 at 05:58
  • 3
    Possible duplicate of [Java Native Access doesn't do C++, right?](http://stackoverflow.com/questions/2241685/java-native-access-doesnt-do-c-right) – Miles Budnek May 11 '17 at 06:14
  • @user4581301 Yes they are of same size. – Rishabh Mehta May 11 '17 at 10:42
  • 1
    See the above duplicate post. You can't call C++ methods, although you won't get an error that they don't exist, either. The value you are seeing is a random (ok, not random, but useless to you!) pointer. You have to export your methods to make them accessible in JNA. – Daniel Widdis May 11 '17 at 15:43
  • 1
    @DanielWiddis I am able to call c++ methods and When i am returning a number(say 5) or variable from c++ , It will be displayed in my JAVA application but variable that is passed to c++ from java is garbage. – Rishabh Mehta May 12 '17 at 02:24

0 Answers0