JNI
With JNI, you implement Java methods (class static members or object instance members) in C or C++. The C or C++ code uses JNI functions to transfer parameters from Java to native, call native functions and then use JNI functions to transfer the return value from native to Java. Simple types that have identical representations in both type systems, of course, don't need any conversion. You can also create Java objects, call methods, access fields and throw and handle exceptions using the JNI functions.
Essential references:
- The Java™ Native Interface Programmer’s Guide and Specification, Sheng Liang (the designer of JNI)
- Your JVM vendor's current JNI documentation. E.G., Java™ Native Interface, Oracle
JNA
With JNA, you call DLL functions in Java. JNA uses JNI internally, but instead of writing C or C++ yourself, you write Java code to describe how the JNA libraries should call the DLL functions and convert parameters and return values.
Cool tool to help understand JNA:
- JNAerator (use with simplified C headers for DLL functions and structures you need)
GetAuthorizationHeader
This appears to be a member of a C++ class. If it's not static, and you need to keep a reference to an instance of it between Java native method calls, then your problem is much larger. If it is static (or can be made so), you could use it directly if you go the JNI route. However, the actual functionality of the GetAuthorizationHeader seems pretty simple so it could be reimplemented inline, or in Java with the JNA route.
Strings
Every string has a character set and encoding (aka code page). Java uses Unicode UTF-16LE or UTF-16BE (platform dependent). Windows uses UTF-16LE. Your code is written with the ambidextrous _T() macro and calls the ambidextrous Win32 API functions. Win32 has FunctionA functions and FunctionW functions. W is for Unicode. Most likely you can drop support for versions of Windows that don't support Unicode. If you call an A function on modern Windows, it has to convert to Unicode and back again so you might as well call the W function anyway.
String literals in Visual C++ are written as L"string" for Unicode and are typed wchar_t
and are UTF-16LE. In C++, wchar_t
is a platform dependent character type. Your version of C++ might not support the new-ish C++ char16_t
so you can substitute wchar_t
.
When using JNI with strings, you can call the GetStringChars
and NewString
functions to get UTF-16LE strings to and from the Win32 API W functions. No conversion needed; just be sure the keep track of code unit counts and/or byte counts (and use the proper one for the function being called). If you are using C++'s STL, then the std::wstring
class might help.
If your algorithm needs a particular encoding then you have to find out what that is. You could then use the Java String and Charset classes to convert encodings to/from Java byte arrays and convert Java byte arrays to/from native byte arrays to use with your DLL functions.
It could be that your algorithm doesn't care but you might need to use a particular encoding anyway if two systems are exchanging string data.