java-native-access / jna

Java Native Access
Other
8.47k stars 1.67k forks source link

Is ERROR_SUCCESS an actual error? #517

Closed Mgamerz closed 8 years ago

Mgamerz commented 8 years ago

Is ERROR_SUCCESS an actual error? Some of my users of my program have the app crash at startup because it is throwing a java.lang.error (instead of exception). This is the stacktrace.

Exception in thread "main" java.lang.Error: The operation completed successfully
    at com.sun.jna.Native.open(Native Method)
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:171)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
    at com.sun.jna.Library$Handler.<init>(Library.java:147)
    at com.sun.jna.Native.loadLibrary(Native.java:412)
    at com.sun.jna.platform.win32.Advapi32.<clinit>(Advapi32.java:47)
    at com.sun.jna.platform.win32.Advapi32Util.registryGetStringValue(Advapi32Util.java:549)
    at com.me3tweaks.modmanager.ModManagerWindow.getInitialBiogameDirText(ModManagerWindow.java:1476)
    at com.me3tweaks.modmanager.ModManagerWindow.setupWindow(ModManagerWindow.java:468)
    at com.me3tweaks.modmanager.ModManagerWindow.initializeWindow(ModManagerWindow.java:193)
    at com.me3tweaks.modmanager.ModManagerWindow.<init>(ModManagerWindow.java:130)
    at com.me3tweaks.modmanager.ModManager.main(ModManager.java:312)

The user is running Java 8 Update 60 32-bit on Windows 7 64bit. I am unable to reproduce this issue but he gets it 100% of the time. I am unsure what kind of error the operation completed successfully is... I know its' a standard return code, but why is it handled like an error in this library?

The registry key I am querying in code, through the registry editor resolves to this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\BioWare\Mass Effect 3]
"DisplayName"="Mass Effect™ 3"
"Install Dir"="H:\\Games\\Mass Effect 3\\"
"GDFBinary"="H:\\Games\\Mass Effect 3\\GDFBinary_en_US.dll"
"Locale"="en_US

At present I can just bypass this by catching throwable but I am wondering more why this error is being thrown at all for ERROR_SUCCESS.

dblock commented 8 years ago

That looks incorrect, ERROR_SUCCESS is a success code.

dblock commented 8 years ago

The implementation in https://github.com/java-native-access/jna/blob/101c9964de258ea134ded756ea1f7564dc578e25/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java#L576 looks correct though, and checks against ERROR_SUCCESS as it should be. Can you reproduce?

motofix commented 8 years ago

We have the same issue on several installations, with both advapi32.dll and kernel32.dll.

The java version is 1.8.0_60 32 bits on a 64bits Windows platform. The following simple java code:

import com.sun.jna.platform.win32.Kernel32;
import java.util.Map;

public class Kernel32Test {
    public static void main(String[] args) throws Exception {
        logSystemInfo();
        int pid = Kernel32.INSTANCE.GetCurrentProcessId();
        System.out.println("Current process id:" + pid);
    }

    private static void logSystemInfo() {
        System.out.println("System properties :");
        for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        System.out.println("Environment variables :");
        for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }
}

Reproduce the problem on all affected desktops, with the following error trace (french installation):

Exception in thread "main" java.lang.Error: L?opération a réussi.

        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:171)
        at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
        at com.sun.jna.Library$Handler.<init>(Library.java:147)
        at com.sun.jna.Native.loadLibrary(Native.java:412)
        at com.sun.jna.platform.win32.Kernel32.<clinit>(Kernel32.java:32)
        at Kernel32Test.main(Kernel32Test.java:7)

Our current assumption is that Norton Security may be the cause of the issue as it is installed on all desktops where the issue occurs, but we have no clue on how to circumvent it...

motofix commented 8 years ago

I've modified a bit the test case by catching throwable on the first call, and then call again:

import com.sun.jna.platform.win32.Kernel32;
import java.util.Map;

public class Kernel32Test {
    public static void main(String[] args) throws Exception {
        logSystemInfo();
        int pid = 0;
        try {
            pid = Kernel32.INSTANCE.GetCurrentProcessId();
        } catch(Throwable t) {
            t.printStackTrace();
        }
        pid = Kernel32.INSTANCE.GetCurrentProcessId();
        System.out.println("Current process id:" + pid);
    }

    private static void logSystemInfo() {
        System.out.println("System properties :");
        for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        System.out.println("Environment variables :");
        for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }
}

Then I have the following output (e.g. it is not possible to use the kernell32.dll):

java.lang.Error: L?opération a réussi.

        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:171)
        at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
        at com.sun.jna.Library$Handler.<init>(Library.java:147)
        at com.sun.jna.Native.loadLibrary(Native.java:412)
        at com.sun.jna.platform.win32.Kernel32.<clinit>(Kernel32.java:32)
        at Kernel32Test.main(Kernel32Test.java:9)
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.sun.jna.platform.win32.Kernel32
        at Kernel32Test.main(Kernel32Test.java:13)
dblock commented 8 years ago

You have a debugger here? Walk the code, find out what returns this and where it's not handled.

motofix commented 8 years ago

We have the issue with version JNA 4.0.0 Upgrading to 4.2.0 solve the problem...

dblock commented 8 years ago

I remember now some issues like this fixed in 4.1 or 4.2 but couldn't find a reference, @Mgamerz would you please check your JNA version?

Mgamerz commented 8 years ago

On 4.1 it was having this issue. On 4.2 it was fixed. I thought I was originally on 4.2 as a computer I was on was building with 4.2 but then I pushed and pulled to a machine configured for 4.1 and used that instead.

However I did not see anything in the changelog for 4.2 that would indicate this was fixed either.

dblock commented 8 years ago

There were some library loading changes in the code, so probably got fixed as a side effect. I didn't dig up. Lets just close this and call it a day, if you're motivated to, you could git bisect your way to the fix and we could all learn something!