⬆️ ⬇️

JNI and Delphi. Examples

Using JNI in Delphi



Greetings to all. This article is designed for Newbies who have just begun to study JNI to work with it in the Delphi environment. And so in this article we will talk how exactly to use JNI in Delphi. And so let's get started.



First you need the JNI component. You can download it HERE . Now we are ready to begin the practical part. I will do everything on RAD Studio 10.1 Berlin



Example number 1:

We receive and change data of type JInt, JBoolean.

')

Suppose that we have a class in which there is a variable I of type JInt, therefore we need to change it.



public int I = 10; 


And there is some handler that uses this variable. For example, the nabiniya event TButton.



  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { System.out.println(i); } 


When you click on the button in the console, the message "10" will appear because I = 10; To change I to any other number, do the following:



 var JNIEnv: PJNIEnv; JC: JClass; JF: JFieldID; Begin JC:= jnienv^.FindClass(JNIEnv, 'example/Main'); JF:= jnienv^.GetFieldID(JNIEnv, JC, 'I', 'I'); jnienv^.SetIntField(JNIEnv, JC, JF, 5); end; 


What is going on here? First we get the JavaClass function FindClass. 'example / Main' this means that the class is located inside the JVM along the path example \ Main.class.



Next, we get JField in this case, this is a type I JInt. GetFieldID we specify Class, Name and Signature.



And finally, we pass our value I, in my case it is equal to 5. SetIntField we specify the Class, JField and value.



The same will be for JBoolean:



  public boolean Stat = false; 




  var JNIEnv: PJNIEnv; JC: JClass; JF: JFieldID; Begin JC:= jnienv^.FindClass(JNIEnv, 'example/Main'); JF:= jnienv^.GetFieldID(JNIEnv, JC, 'Stat', 'Z'); jnienv^.SetByteField(JNIEnv, JC, JF, 1); end; 


Initially Stat was false i.e. 0, and we change it to true, i.e. one;



Example number 2:

Call to the procedure Void ()



Suppose we have a certain static procedure in which a certain script is executed. The procedure will be performed by pressing the button. In order to perform this procedure, we will make an appeal to the Method.



  public static void Push() { System.out.println("Hello"); } 


As we see, this procedure will simply be output to the “Hello” Console. To cause the execution of this procedure, do the following:



 var JNIEnv: PJNIEnv; JC: JClass; JM: JMethodID; Begin JC:= jnienv^.FindClass(JNIEnv, 'example/Main'); JM:=jnienv^.GetStaticMethodID(JNIEnv, JC, 'Push', '()V'); jnienv^.CallStaticVoidMethod(JNIEnv, JC, JM); end; 


What is going on here? We get the static push method. GetStaticMethodID where we specified the Class, Name and Signature.



And we call it on the execution of CallStaticVoidMethod where we specified the Class and Method.



Example number 3

Function call with JInt variable.



Suppose we have a certain function that has a variable I of type JInt. And the result of execution is JInt, and a certain script inside the function gives some messages to the Console.



 public static int Num(int i) { for (int s=0; s<i; s++) { System.out.println("Example"); } return i; } 


In fact, the number of “Example” messages will be output to the console. Let's consider an appeal to such a function.



 var JNIEnv: PJNIEnv; JC: JClass; JM: JMethodID; Begin JC:= jnienv^.FindClass(JNIEnv, 'example/Main'); JM:=jnienv^.GetStaticMethodID(JNIEnv, JC, 'Num', '(I)I'); jnienv^.CallStaticVoidMethodV(JNIEnv, JC, JM, '5'); end; 


What is going on here? We find the Method Needed. And make a call to it, but this one with the JInt indication. In this case, it is 5.



And so the result of the execution of this function will be the output to the Console 5 times the message "Example"

Source: https://habr.com/ru/post/324552/



All Articles