B4J Question Windows OS - GetEnvironmentVariable function

agb2008

Member
Licensed User
Longtime User
Are there any alternatives to built in function "GetEnvironmentVariable" ? Probably as WinAPI similar function call ? I am testing software protection system that require access to certain environment variables set by protection application. If I create simple application using FreeBasic (they use native
WinAPI GetEnvironmentVariable function) - then it's working as expected. But if I try to use built in
B4J function from Core library - I can't get value of the same environment variable...
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Use the jShell library or the jAWTRobot library to run this command:
B4X:
echo %JAVA_HOME% > javahome.txt
This example will send the JAVA_HOME environment variable to the text file javahome.txt (in the working directory of the jar that called it). To get a different environment variable, just replace %JAVA_HOME% with whatever.
 
Upvote 0

agb2008

Member
Licensed User
Longtime User
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 !):
B4X:
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:
B4X:
    #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
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
As a side note, if you use the @DependsOn attribute in your Java code, you won't have to include the #AdditionalJar attributes in your B4J code.
 
Upvote 0
Top