Roycefer :
Thank you for your suggestion. Unfortunately, to solve issue that I've got, I have to use
WinAPI GetEnvironmentVariable function.
So - I decide to play with
JNA usage and was able to implement native
WinAPI Kernel32.dll GetEnvironmentVariable function
call from
B4J application using
JNA project libs. It seems that solution was easier than I thought initially. Please note that provided
example is quick workaround solution and for sure
probably not the best implementation - but it's working O.K. for me so...
Anyway please use with care:
1. You need
jna-4.1.0.jar and
jna-platform-4.1.0.jar files from
JNA project site:
https://github.com/twall/jna#download
2. Not sure is that was needed or not - but I've corrected "
Implementation-Version: 4.1.0 " in
MANIFEST.MF file inside
.jar archive
for
both files according to recommendation of
agraham . Please see
his post for more details why.
3. Created simple
Java library (
nativeutils.java) for compilation using
B4J_LibraryCompiler.exe from
SimpleLibraryCompiler
package (Thanks
Erel !):
Spoiler: nativeutils.java
package NativeUtils;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
@Version(1.0f)
@ShortName("NativeUtils")
@Author("Alexey Bogdanov")
public class nativeutils {
/**
* Get the value of an environment variable.
* @param name
* Name of the environment variable.
* @return
* Value of an environment variable.
*/
public static String GetEnvironmentVariable(String name) {
char[] result = new char[256];
Kernel32.INSTANCE.GetEnvironmentVariable(name, result, result.length);
return Native.toString(result);
}
}
4. Create following folder structure for compilation with
B4J_LibraryCompiler.exe:
Top level folder:
NativeUtils
Inside that folder create two folders:
src - where you should put provided above example of Java library:
nativeutils.java
and:
libs - where you should put
jna-4.1.0.jar and
jna-platform-4.1.0.jar files.
5. Compile library using
SimpleLibraryCompiler for
B4J (!!!) and don't forget to
refresh library list in
B4J or restart
B4J before usage of that lib.
That's all about creation of
B4J library that would use
WinAPI Kernel32 function
GetEnvironmentVariable using
JNA
To use that library in your B4J application:
1. Don't forget to select it (
NativeUtils ) in B4J
Libraries Manager tab.
2. In Project Attributes section you would have to add two following lines:
Spoiler
#AdditionalJar: jna-4.1.0
#AdditionalJar: jna-platform-4.1.0
3. In Process_Globals section use following definition:
Public gev As NativeUtils
4. Use this function in your application i.e.:
Log("NativeUtils result :"&gev.GetEnvironmentVariable("JAVA_HOME"))
That's all ! Quite simple for WinAPI - don't you think ?
P.S. Please note that provided library example is just result of
quick experiment - for production environment additional error checking
of cause have to be implemented. But... anyway it's working example !
P.P.S. I've used
Windows 8.1 64 bit and
Oracle Java JDK 1.8.0_51 64 bit