The Java Native Interface (JNI) is the native programming interface for Java that is part of the JDK. By writing programs using the JNI, you ensure that your code is completely portable across all platforms.
The JNI allows Java code that runs within a Java Virtual Machine (VM) to operate with applications and libraries written in other languages, such as C, C++, and assembly. In addition, the Invocation API allows you to embed the Java Virtual Machine into your native applications.
Programmers use the JNI to write native methods to handle those situations when an application cannot be written entirely in the Java programming language. For example, you may need to use native methods and the JNI in the following situations:
?The standard Java class library may not support the platform-dependent features needed by your application.
?You may already have a library or application written in another programming language and you wish to make it accessible to Java applications.
?You may want to implement a small portion of time-critical code in a lower-level programming language, such as assembly, and then have your Java application call these functions.
Programming through the JNI framework lets you use native methods to do many operations. Native methods may represent legacy applications or they may be written explicitly to solve a problem that is best handled outside of the Java programming environment.
The JNI framework lets your native method utilize Java objects in the same way that Java code uses these objects. A native method can create Java objects, including arrays and strings, and then inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code. A native method can even update Java objects that it created or that were passed to it, and these updated objects are available to the Java application. Thus, both the native language side and the Java side of an application can create, update, and access Java objects and then share these objects between them.
Native methods can also easily call Java methods. Often, you will already have developed a library of Java methods. Your native method does not need to "re-invent the wheel" to perform functionality already incorporated in existing Java methods. The native method, using the JNI framework, can call the existing Java method, pass it the required parameters, and get the results back when the method completes.
The JNI enables you to use the advantages of the Java programming language from your native method. In particular, you can catch and throw exceptions from the native method and have these exceptions handled in the Java application. Native methods can also get information about Java classes. By calling special JNI functions, native methods can load Java classes and obtain class information. Finally, native methods can use the JNI to perform runtime type checking.
as i know, no HWND in java.
and even object can be passed, how about struct???
but i suggest you to use stream.
it can be reconstructed to everything that you want.
Thanks a lot . Can you tell me is there HWND in Java ?
And if I want pass a region to my DLL wtitten by C++ ,
is Java able to pass a struct such as "RECT" into the
DLL written by C++ .
first,i have to say sorry that I have no Chinese Input Methods here.
so,i must write in English as well.
:-(
You can use JNI to dispatch these problems.
I think if you really wanna do so,
you may define your native methods' name first,
then, use Javah.exe to create the header file for c/c++
in your c/c++ program, you can copy the names and paramers of methods in that .h file(and you have to do so) as the definitions.
Then , fill your methods, and make a .dll,
in java program, use System.load("name of dll")
now, you can use them
attention:you must use key word "native" to declare your java methods like this
public native void myMethod(type parameter)
and you must think about the difference of types between java and c++,
if I were you, I would use stream as return type and parameter.
in java and c++,the standard streams are the same.