Hello,
I am writing a EXE launcher for a Java application using AutoIt. It works fine when starting Java through ShellExecute, but for several reasons I would like to call it through the DLL (so that I don't create a separate process)
But I can't figure out how to provided the needed DLL struct for the call. The problem is, that it's a nested struct, where the nested element is an array of char* pointers.
The JDK documentation contains an example: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html#JNI_CreateJavaV
JavaVMInitArgs vm_args;
JavaVMOption options[4];
// Defining the array and filling the elements is the part I don't know how to do with AutoIt
// (Don't mind the content here)
options[0].optionString = "-Djava.compiler=NONE"; /* disable JIT */
options[1].optionString = "-Djava.class.path=c:\myclasses"; /* user classes */
options[2].optionString = "-Djava.library.path=c:\mylibs"; /* set native library path */
options[3].optionString = "-verbose:jni"; /* print JNI-related messages */
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = TRUE;
res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);
The two structs are defined this way:
typedef struct JavaVMOption {
char *optionString;
void *extraInfo;
} JavaVMOption;
typedef struct JavaVMInitArgs {
jint version;
jint nOptions;
JavaVMOption *options;
jboolean ignoreUnrecognized;
} JavaVMInitArgs;
This is what I came up with so far:
Local $optionArray[2]
$optionArray[0] = "-cp MyApp.jar"
$optionArray[1] = "MyMain"
$env = 0
$option = DllStructCreate("struct;char* optionString;ptr extraInfo;endstruct")
$vmInitArgs = DllStructCreate("struct;LONG version;LONG nOptions;ptr options;BOOLEAN ignoreUnrecognized;endstruct")
; Not sure if this is correct
DllStructSetData($option, "optionString", $optionArray)
DllStructSetData($vmInitArgs, "version", 0x00010008)
DllStructSetData($vmInitArgs, "nOptions", 2)
DllStructSetData($vmInitArgs, "options", DllStructGetPtr($option))
DllStructSetData($vmInitArgs, "ignoreUnrecognized", 1)
Local $handle = DllOpen($dllPath)
$res = DllCall($handle, "INT", "JNI_CreateJavaVM", "ptr*", $env, "ptr", DllStructGetPtr($vmInitArgs))
ConsoleWrite("*** Error: " & @error & @CRLF)
ConsoleWrite("*** Result: " & $res[0] & @CRLF)
DllClose($handle)
I also tried calling DllStructSetData with an index number rather than the "member name". I also tried calling DllStructCreate without the "struct/endstruct" markers and without the member names.
The @error code is always 0, but $res[0] contains an error code (-3) which in theory indicates that the JNI version is incorrect, but most answers I found indicate that this can also mean the JavaVMInitArgs struct was not populated correctly.
I have no experience with C/C++ programming so I am at a loss with mapping the information from the Java docs to AutoIt
Any ideas?
Thanks in advance