I cheated on JNA while trying to execute C code in a Java program. This is a working example that I found online (JNA is required in the build path):
package core;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class CoreController {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
(Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i = 0; i < args.length; i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
Native.main(args);
}
}
In fact, I'm trying to do three (seemingly ugly) things.
1.) The entry point of the program should be changed to the following C-signature:
void __stdcall RVExtension(char *output, int outputSize, const char *function);
2.) The Java program should be able to set the given parameter output.
3.) The program must be compiled into a DLL.
In C ++, this problem will be solved as follows:
#include "stdafx.h"
extern "C" {
__declspec (dllexport) void __stdcall RVExtension(char *output, int outputSize, const char *function);
}
void __stdcall RVExtension(char *output, int outputSize, const char *function) {
strncpy_s(output, outputSize, "IT WORKS!", _TRUNCATE);
}
So the question is, what is possible with Java? If so, I would be glad to see an example of the code, since I am introducing a lot of new territory here. I don’t even know if JNA is suitable here. If anyone has another idea, please let me know!
Regards,
Jayson