Mar
5
2005

Getting very native with JNIWrapper…

After much delay, I finally bought a copy of JNIWrapper, which is one very cool piece of software. Basically, it’s a set of libraries that gives you access to the Win32 libraries without the need of writing your own C wrapper classes for JNI.

And it’s not just for Win32 either – COM is supported out of the box! Run their nifty swing app, select the COM library you want to use (from the list they prepopulate from your registry!), set where you want it dump the Java wrapper classes, and boom. Java COM Factory wrappers, interfaces, and concrete classes are all there, ready to go – including all those dodgy Win32 BStr classes you need when calling COM. You can even tell your COM factories to instantiate ApartmentThreaded, Multithreaded and all that other Win32 goodness that really only David Chappell understands anyway.

Best of all, the documentation and tutorial examples are first class. By the end of my first session, I had created Java stubs for the COM object I was into, and made a few calls to see all was working as planned. Any that’s with pretty vanilla Win32 skills on my end.

If you’re not into COM, and need to call straight Win32 stuff, types are provided for all the standard Window enums, and you can roll your own if you have to. The exception handling is awesome, Win32 faults get converted to Java exceptions, getLastError() works, and it’s all memory-safe to boot.

They even ship with a “WinPack” library that puts a Java wrapper round a ton of common Win32 things you’re likely to want to do (like the Registry, Filesystem Watches, Window operations like transparency, Tray icons and all that nativey stuff). But if what you want is not in there already, it’s pretty trivial to call *any* Win32 routine if you know the signature.

If Win32 is not your bag, it ships with versions for Linux/Mac too! I am loving the power, and at $US120 for a bundle of *both* Win32 and Unix libraries, it’s an absolute steal – and we don’t have to mix up any C code in our project! Great work, guys!

About the Author: Glen Smith

3 Comments + Add Comment

  • I looked at JNI wrapper and strongly considered using it. It looks like the best of the various commercial alternatives. In the end, I found the shared stubs method in Sun’s JNI book easier to use. It’s really a great starting point for someone just getting started with JNI.

    Since I’m a bit of a type-safety nut, I modified the shared stubs code to be completely package private. Instead, the package exposes a mechanism where users supply an interface and a library to supply the native functions. As long as the method signatures in your interface match the function signatures in the library everything is good. In short, you get type-safety all through the Java code.

    The hard part for someone with little C experience like me, was rewriting the SPARC and x86 assembly language part of shared stubs in portable C. Now, the same C code supports SPARC, x86, and MIPS. Additional processors are hopefully just a recompile away.

    Bug me if you’re interested an I’ll try to get permission to release it as open-source. If you need COM support, com4j may also be worth your while.

    Does anybody know anything about the rumored fast-and-easy JNI Sun developed. Is it just a rumor?

  • I have tried to use Winpack, however there is a problem. Some of Winpack’s classes imports other packages which were not in Winpack itself…
    Example:
    Import com.jniwrapper.util.EnumItem
    import com.jniwrapper.util.Logger
    “util” is not included in Winpack.
    Thank you!

  • You need to include jniwrap.jar in your classpath along with winpack.jar. Winpack relies on jniwrap to perform the actual Win32 calls.

Leave a comment

Glen Smith

About Glen

Co-author Grails in Action