Search code examples
dllmodulejava-native-interface32-bitnetbeans-platform

Unable to load dll with JNI outside of Nebeans IDE on XP


I have a Netbeans Platform application with a module with a topComponent that loads a dll using Java Native Interface. The dll was created in Visual Studio 2012 with the v110_xp toolset to communicate with a USB device. It does use some MFC functions and MFC is compiled as a static library from visual studio.

There are no problems with this application or dll when running on a Windows 7 32/64 bit computer. The dll and topComponent do not load at all on Windows XP computers.

To test the problem further I installed Netbeans 7.4 on the XP computer and attempted to load the dll while debugging. No problems found, all USB functions working, topComponent loads with no issues. If I release the program from here by packaging as a ZIP distribution the program does not load the DLL or the topComponent. It only works on the XP 32 bit computer if I run it from inside the Netbeans IDE.

I ran dependencyWalker on the dll file and the only missing dependencies are IESHIMS.dll and WER.dll, from what I've seen these are delay-loaded dll's and should never be needed in my application.

Does anybody have any clue on why this isn't working?

-------------------------Update-------------------------

I've updated the way that I load the dll inside of netbeans:

static{
        File dllPath = org.openide.modules.InstalledFileLocator.getDefault().locate("modules/lib/x86", "com.module.foo", false);
        try {
             System.setProperty("java.library.path", dllPath.getAbsolutePath());
             System.setProperty("jna.library.path", dllPath.getAbsolutePath());
             System.setProperty("jni.library.path", dllPath.getAbsolutePath());
             Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
             fieldSysPath.setAccessible(true);
             fieldSysPath.set(null, null);
        } catch (Exception e) {
             throw new RuntimeException(e);
        }
    }

Then I load the dll as such:

DllInterface INSTANCE = (DllInterface) Native.loadLibrary(
             "My32bitdll" , 
            DllInterface.class);

This works great, again, only inside of the Netbeans IDE.... is there something I'm missing???

-------------------------Update 2-------------------------

I'm now also able to get this working inside of the Netbeans IDE by simply using

System.loadLibrary("My32bitdll");

It also works with no issues on 64 bit machines. I got this working by fixing the method names in my dll file with javah.

Again, I'm using the x86 JDK, x86 JRE, and with a 32bit dll... still doesn't work outside of netbeans.

-------------------------Update 3-------------------------

I've narrowed this down to a problem with Netbeans Platform Applications. I have no trouble loading the dll from a standard java application using

System.load("myDllPath");

I've followed this guide: NetBeans Development 7 - Windows 7 64-bit … JNI native calls ... a how to guide which describes loading a dll within a Netbeans module but I still can't get this working on a 32 bit system outside of Netbeans IDE.


Solution

  • I found a solution to the problem. Still might need to be something that needs to be addressed within the netbeans development crew, or I'm just missing something.

    I was able to get my dll (in /release/modules/lib) with dependencies on winUSB.dll in the system32 folder working with JNA if I created a new netbeans platform application on the 32 bit computer from scratch.

    Any Netbeans Platform Project that I start from the 64 bit computer and then transfer to a 32 bit computer will not load the dll dependencies correctly. You can debug them on the 32 bit computer but it will never work outside of the Netbeans IDE (7.4).