⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 native_dpr.html

📁 JNI(java本地接口)之delphi版
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html>
<BODY BGCOLOR="#e0e0e0">
<pre class="sourcecode"><code><font color="#003399"><i>(****************************************************************************
 *
 *  Native.dpr
 *
 *  This file implements the native routines declared in Native.java. The
 *  code is commented and fairly straight-forward. Note that there is
 *  almost no error checking in this code, however, a few functions do
 *  implement some error checks as a way of demonstrating how and where
 *  to check.
 *
 *  This code was a port from Native.cpp, a C++ implementation, also
 *  developed by Matthew Mead.
 *
 *  HISTORY
 *    7-22-2000    Created.
 *   10-14-2000   Modified to reflect JEDI standard of coding.
 *
 *
*)</i></font>
<b>library</b> Native;

<b>uses</b>
  SysUtils,
  Classes,
  Windows,
  JNI;

<font color="#003399"><i>(****************************************************************************
 *  Java declaration:
 *     native public void displayHelloWorld();
 *
 *  The canonical first method.
 *
 *  Class:     Native
 *  Method:    displayHelloWorld
 *  Signature: ()V
 *)</i></font>
<b>procedure</b> Java_Native_displayHelloWorld(PEnv: PJNIEnv; Obj: JObject); <b>stdcall</b>;
<b>begin</b>
  WriteLn(<font color="#9933CC">'Hello world!'</font>);
<b>end</b>;


<font color="#003399"><i>(****************************************************************************
 *  Java declaration:
 *     native public void initializeByteArray(byte[] byteArray, int count, byte value);
 *
 *  Initializes an array 'Count' times with the Value 'Value'. This function
 *  is mainly used to test the performance of array accesses in native code.
 *
 *  Class:     Native
 *  Method:    initializeByteArray
 *  Signature: ([BIB)V
 *)</i></font>
<b>procedure</b> Java_Native_initializeByteArray(PEnv: PJNIEnv; Obj: JObject; ByteArray: JByteArray; Count: JInt; Value: JByte); <b>stdcall</b>; overload;
<b>var</b>
  Elements: PJByte;
  PE: PJByte;
  Len: DWORD;
  I, J: Longint;
  IsCopy: JBoolean;
  JVM: TJNIEnv;
<b>begin</b>
    <font color="#003399"><i>// Create an instance of the Java environment</i></font>
  JVM := TJNIEnv.Create(PEnv);

    <font color="#003399"><i>// Get elements of the array</i></font>
  Elements := JVM.GetByteArrayElements(ByteArray, IsCopy);

    <font color="#003399"><i>// Get length of the array</i></font>
  Len := JVM.GetArrayLength(ByteArray);

    <font color="#003399"><i>// Loop through the array 'Count' times, assigning 'Value' to each</i></font>
    <font color="#003399"><i>// element.</i></font>
  <b>for</b> I := 0 <b>to</b> Count - 1 <b>do</b>
  <b>begin</b>
    PE := Elements;
    <b>for</b> J := 0 <b>to</b> Len - 1 <b>do</b>
    <b>begin</b>
      PE^ := Value;
      Inc(PE);
    <b>end</b>;
  <b>end</b>;
<b>end</b>;


<font color="#003399"><i>(****************************************************************************
 *  Java declaration:
 *     native public String printLine(String text);
 *
 *      Prints a Java string, and returns a Java string
 *
 *  Class:     Native
 *  Method:    printLine
 *  Signature: (Ljava/lang/String;)Ljava/lang/String;
 *)</i></font>
<b>function</b> Java_Native_printLine(PEnv: PJNIEnv; Obj: JObject; Value: JString): JString; <b>stdcall</b>;
<b>var</b>
  Str: <b>string</b>;
  JVM: TJNIEnv;
<b>begin</b>
    <font color="#003399"><i>// Create an instance of the Java environment</i></font>
  JVM := TJNIEnv.Create(PEnv);

    <font color="#003399"><i>// Convert the JString into a string</i></font>
  Str := JVM.JStringToString(Value);

    <font color="#003399"><i>// Print the string to the screen</i></font>
  WriteLn(<font color="#9933CC">'In Native code, string is: '</font>, Str);

    <font color="#003399"><i>// Create a new string and return it</i></font>
  Result := JVM.NewStringUTF(<font color="#9933CC">'String from Native code'</font>);
  JVM.Free;
<b>end</b>;


<font color="#003399"><i>(****************************************************************************
 *  Java declaration:
 *     native public void printWXYZ();
 *
 *  Prints out four members of the Native object, w, x, y, and z. This
 *  function acts sort of like the traditional 'toString' method in Java.
 *  The members are declared in Native.java as such:
 *
 *  public String w;       // public Object
 *  public int x;          // public
 *  private int y;         // private (no protection here)
 *  public static int z;   // public static
 *
 *  The return Value from each GetFieldID call should be checked.
 *  I don't check because I'm trying to keep the focus on the calls.
 *
 *  Class:     Native
 *  Method:    printWXYZ
 *  Signature: ()V
 *)</i></font>
<b>procedure</b> Java_Native_printWXYZ(PEnv: PJNIEnv; Obj: JObject); <b>stdcall</b>;
<b>var</b>
  X, Y, Z: JInt;
  W: JString;
  FID: JFieldID;
  Cls: JClass;
  Str: <b>string</b>;
  JVM: TJNIEnv;
<b>begin</b>
    <font color="#003399"><i>// Create an instance of the Java environment</i></font>
  JVM := TJNIEnv.Create(PEnv);

    <font color="#003399"><i>// Get the class associated with this object</i></font>
  Cls := JVM.GetObjectClass(Obj);

    <font color="#003399"><i>// w is a String</i></font>
  FID := JVM.GetFieldID(Cls, <font color="#9933CC">'w'</font>, <font color="#9933CC">'Ljava/lang/String;'</font>);

    <font color="#003399"><i>// Get the Object (String) w.</i></font>
  W := JVM.GetObjectField(Obj, FID);

    <font color="#003399"><i>// x is a non-static public field</i></font>
  FID := JVM.GetFieldID(Cls, <font color="#9933CC">'x'</font>, <font color="#9933CC">'I'</font>);

    <font color="#003399"><i>// Get the int</i></font>
  X := JVM.GetIntField(Obj, FID);

    <font color="#003399"><i>// y is a non-static private field, same as public here</i></font>
  FID := JVM.GetFieldID(Cls, <font color="#9933CC">'y'</font>, <font color="#9933CC">'I'</font>);

    <font color="#003399"><i>// Get the int</i></font>
  Y := JVM.GetIntField(Obj, FID);

    <font color="#003399"><i>// z is a _static_ public field, so call different method</i></font>
  FID := JVM.GetStaticFieldID(Cls, <font color="#9933CC">'z'</font>, <font color="#9933CC">'I'</font>);

    <font color="#003399"><i>// Get static int</i></font>
  Z := JVM.GetStaticIntField(Cls, FID);

    <font color="#003399"><i>// Convert Java string into Delphi string</i></font>
  Str := JVM.JStringToString(W);

    <font color="#003399"><i>// Sort of like the traditional 'toString' output</i></font>
  WriteLn(Format(<font color="#9933CC">'[w = %s, x = %d, y = %d, z = %d]'</font>, [Str, X, Y, Z]));

  JVM.Free;
<b>end</b>;

<font color="#003399"><i>{**********************************
Same as above but without the wrapper.

procedure Java_Native_printWXYZ(PEnv: PJNIEnv; Obj: JObject); stdcall;
var
  X, Y, Z: JInt;
  W: JString;
  FID: JFieldID;
  Cls: JClass;
  Str: string;
  IsCopy: JBoolean;  // added for GetStringChars
  Chars: PJChar;     // added for GetStringChars
begin
    // Get the class associated with this object
  Cls := PEnv^.GetObjectClass(PEnv, Obj);

    // w is a String
  FID := PEnv^.GetFieldID(PEnv, Cls, 'w', 'Ljava/lang/String;');

    // Get the Object (String) w.
  W := PEnv^.GetObjectField(PEnv, Obj, FID);

    // x is a non-static public field
  FID := PEnv^.GetFieldID(PEnv, Cls, 'x', 'I');

    // Get the int
  X := PEnv^.GetIntField(PEnv, Obj, FID);

    // y is a non-static private field, same as public here
  FID := PEnv^.GetFieldID(PEnv, Cls, 'y', 'I');

    // Get the int
  Y := PEnv^.GetIntField(PEnv, Obj, FID);

    // z is a _static_ public field, so call different method
  FID := PEnv^.GetStaticFieldID(PEnv, Cls, 'z', 'I');

    // Get static int
  Z := PEnv^.GetStaticIntField(PEnv, Cls, FID);

    // Convert Java string into Delphi string
  Chars := PEnv^.GetStringChars(PEnv, W, IsCopy);
  Str := string(Chars);
  PEnv^.ReleaseStringChars(PEnv, W, Chars);

    // Sort of like the traditional 'toString' output
  WriteLn(Format('[w = %s, x = %d, y = %d, z = %d]', [Str, X, Y, Z]));
end;
}</i></font>

<font color="#003399"><i>(****************************************************************************
 *  Java declaration:
 *     native public String toStringWithPrint();
 *
 *  This is the traditional 'toString' method with a twist. It calls back
 *  into Java to do the real work. There is a Native method called toString
 *  which formats the object's members for printing. This function simply
 *  calls it.
 *
 *  The net effect is this: The Java object calls a native method which,
 *  in turn, calls back to the Java object. The Java could have simply
 *  made a call to it's own 'toString' method. This of course is for
 *  demonstration purposes only. Don't do this at work!!
 *
 *  Class:     Native
 *  Method:    toStringWithPrint
 *  Signature: ()Ljava/lang/String;
 *)</i></font>
<b>function</b> Java_Native_toStringWithPrint(PEnv: PJNIEnv; Obj: JObject): JString; <b>stdcall</b>;
<b>var</b>
  Cls: JClass;
  Mid: JMethodID;
  S: JString;
  Str: <b>string</b>;
  JVM: TJNIEnv;
<b>begin</b>
    <font color="#003399"><i>// Create an instance of the Java environment</i></font>
  JVM := TJNIEnv.Create(PEnv);

  <font color="#003399"><i>// This just simply tests the non-existence of ExceptionCheck in JDK 1.1</i></font>
  <font color="#003399"><i>// Not used in this example for any &quot;real&quot; purpose.</i></font>
  <font color="#003399"><i>{ TODO: Move this into a separate function }</i></font>
  <b>try</b>
    JVM.ExceptionCheck;
  <b>except</b>
    <b>on</b> e: EJNIUnsupportedMethodError <b>do</b>
      WriteLn(e.Message);
  <b>end</b>;

    <font color="#003399"><i>// Get the class associated with this object</i></font>
  Cls := JVM.GetObjectClass(Obj);

    <font color="#003399"><i>// Get the ID for its 'String toString()' method</i></font>
  Mid := JVM.GetMethodID(Cls, <font color="#9933CC">'toString'</font>, <font color="#9933CC">'()Ljava/lang/String;'</font>);

    <font color="#003399"><i>// Call the method, which returns a String</i></font>
  S := JVM.CallObjectMethodA(Obj, Mid, <b>nil</b>);

    <font color="#003399"><i>// Convert the JString to a string and print it to the screen</i></font>
  Str := JVM.JStringToString(S);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -