One of the main reasons I never liked JAVA is because of it’s cross-platform philosophy. The idea is that everything needs to be so generic and portable, that you can’t get anything actually done. Running applications in the real world means integrating with the operating system. In my own experience I needed to get the username of the user logged into Windows, so I’ve generated everything from JNI monstrosities, C# libraries with COM wrappers invoked via Jacob to making remote service calls from CXF to WCF. Can someone please provide a solution that doesn’t make me feel retarded debugging SOAP messages for a local call to GetUserNameW?!
Thank you. The answer is called JNA.
Wrapping a Win32 function is a trivial exercise for any C/C++ developer who dealt with Win32. GetUserNameW takes a wchar_t buffer and a pointer to a length. In JNA this looks like this.
In order not to feel too much like a Win32 developer, we ought to wrap this thing with a usable method.
GetUserName returns a boolean for success or failure and can give us an additional error when the buffer isn’t sufficiently large.
Note how JNA implements those nice methods that convert
char buffers to
String. That’s service!