Platform Invoke in SUN's Java

The most of Java developer does not use (maybe cannot use) JNI. Because this API is very low and complex in use. But in some type applications developers need to call Native functions from system or other libraries. And the only decision is to write JNI code or to use some tools like xFunctions, JNIWrapper, etc. which are not easy. Why SUN does not developed something like Platform Invoke, which main idea is 揹eclare?and "use"? For example, I want to call in C# function GetSystemMetrics I write code:

value class Win32 {

public

[DllImport("User32.dll")]

static int GetSystemMetrics(int);

enum class SystemMetricIndex {

// Same values as those defined in winuser.h.

SM_CXSCREEN = 0,

SM_CYSCREEN = 1

};

};

and call it

int main() {

int hRes = Win32::GetSystemMetrics( safe_cast<int>(Win32::SystemMetricIndex::SM_CXSCREEN) );

int vRes = Win32::GetSystemMetrics( safe_cast<int>(Win32::SystemMetricIndex::SM_CYSCREEN) );

Console::WriteLine("screen resolution: {0},{1}", hRes, vRes);

}

Very easy, semantic and syntax suitable to C#, no low level code like JNI.

So I tried to prove that the same can be done in Java and wrote my Platform Invoke for Java. In my API I can write as follows:

@NativeLibrary(name = "user32.dll")

public class User32Lib extends CNativeLibrary {

public static final int SM_CXSCREEN = 0;

public static final int SM_CYSCREEN = 1;

@NativeFunction(name = " GetSystemMetrics")

public native int GetSystemMetrics(int);

}

and in any Java method I can call this function:

User32Lib user32 = new User32Lib();

int hRes = user32.GetSystemMetrics(user32.SM_CXSCREEN );

int vRes = user32.GetSystemMetrics(user32.SM_CYSCREEN);

System.out.println("screen resolution: ?+ hRes + ?+ vRes);

In my API I added hidden marshal of arrays and simple data structures, explicit marshal (Marshal class) for complex data structures and Delegate class that makes Thunk for Java methods used as callbacks of native functions. I do not claim that in this case JNI will not be used. But the most Java developers will be able to write code without JNI use.

Message was edited by:

vitallis

Message was edited by:

vitallis

Message was edited by:

vitallis

Message was edited by:

vitallis

Message was edited by:

vitallis

[2480 byte] By [vitallisa] at [2007-11-26 19:33:24]
# 1

Hi,

This behaviour is implemented with 2 other free projects :

- nlink (https://nlink.dev.java.net/),

- jna (https://jna.dev.java.net/),

I develop JNative which is not AOP, but has more advanced features.

Merging all them may be a great idea.

--Marc (http://jnative.sf.net)

mdentya at 2007-7-9 22:05:44 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

This behaviour does not implemented in the product mentioned by you. With these products Developer should write Java code for Marshal/Unmarshal. In my API all job (marshaling/unmarshaling all java types) is done inside my native DLL. J/Invoke API binds a native method declaration to J/Invoke native code like JVM does with any native method. The APIs you mentioned does not support callback functions but my own can do this.

Sorry, JNative supports Callback functions in a very primitive way. I developed Java class called CDelegator that makes .NET Delegate like object. In my API Java Developer does know nothing about native memory, native pointer, allocation and deletion native memory. etc. J/Invoke supports pure Java programming style.

vitallisa at 2007-7-9 22:05:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Hi,Is J/Invoke free like the 3 others ?Where can we find it ?BTW : JNative does support the callbacks for both Win32 and Linux.--Marc ( http://jnative.free.fr)
mdentya at 2007-7-9 22:05:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

JNative, See:

http://jnative.free.fr/SPIP-v1-8-3/article.php3?id_article=4

J/Invoke had been finished some day ago and should pass QA. It will be a part of OLEJA (http://www.simtel.net/product.php%5bid%5d93010%5bSiteID%5dsimtel.net) because J/Invoke supports calls to native functions from Virtual Table. That is COM native interfaces can be implemented in Java easy.

vitallisa at 2007-7-9 22:05:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

> JNative, See:

> http://jnative.free.fr/SPIP-v1-8-3/article.php3?id_art

> icle=4

Thank you, but I am the developper of JNative ;)

> J/Invoke had been finished some day ago and should

> pass QA. It will be a part of OLEJA

> (http://www.simtel.net/product.php%5bid%5d93010%5bSite

> ID%5dsimtel.net) because J/Invoke supports calls to

> native functions from Virtual Table. That is COM

> native interfaces can be implemented in Java easy.

So it is a commercial product. Our license terms are incompatible.

--Marc (http://jnative.sf.net)

mdentya at 2007-7-9 22:05:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
How do you (J/Invoke) distinguish yourself from the half dozen or so other commercial offerings that provide the same or similar native access?
twalljavaa at 2007-7-9 22:05:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

The main idea of the product is 揹eclare?and 搖se?a native method in Java code. Show me other products that make such Java programming so simple (usually you should write some code to marshal/unmarshal data while a function call, make a function call indirectly with some specific class methods use, etc.). I did not find other Java product that provides native access with

?implicit marshaling,

?callback function delegate that can be created with a java method at runtime,

?implicit/explicit marshal/unmarshal of C++ structures to/from Java classes that takes into account a structure pack (1,2,4,8,16),

and without

?explicit 搈emory allocation?and 揻ree?

?non-java type definitions like WORD , DWORD, QWORD, etc.

In other words, get .NET Platform Invoke API (I want to repeat this API in Java) and compare it with products you meant.

vitallisa at 2007-7-9 22:05:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...