java-native-access/jna · error · Win32Exception

Win32Exception from native GetLastError after EnumResourceTy

Error message

Win32Exception from native GetLastError after EnumResourceTypes failed

What it means

Kernel32Util.getResourceNames throws this Win32Exception when Kernel32.INSTANCE.EnumResourceTypes returns false while enumerating the resource types of a module loaded as a datafile. The native GetLastError code indicates why enumeration failed (e.g. ERROR_INVALID_HANDLE 6 if the module handle is invalid, ERROR_NO_MORE_ENTRIES in benign cases).

Source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java:1117

                } else {
                    typeName = type.getWideString(0);
                }

                if (Pointer.nativeValue(name) < 65535) {
                    result.get(typeName).add(Pointer.nativeValue(name) + "");
                } else {
                    result.get(typeName).add(name.getWideString(0));
                }

                return true;
            }
        };


        Win32Exception err = null;
        try {
            if (!Kernel32.INSTANCE.EnumResourceTypes(target, ertp, null)) {
                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
            }

            for (final String typeName : types) {
                result.put(typeName, new ArrayList<String>());

                // simulate MAKEINTRESOURCE macro in WinUser.h
                // basically, if the value passed in can be parsed as a number then convert it into one and run with that.
                // otherwise, assume it's a string and construct a pointer to said string.
                Pointer pointer = null;
                try {
                    pointer = new Pointer(Long.parseLong(typeName));
                } catch (NumberFormatException e) {
                    pointer = new Memory(Native.WCHAR_SIZE * (typeName.length() + 1));
                    pointer.setWideString(0, typeName);
                }

                boolean callResult = Kernel32.INSTANCE.EnumResourceNames(target, pointer, ernp, null);

View on GitHub (pinned to d036ad9781)

Solutions

  1. Check the Win32Exception errorCode: handle 6 (invalid handle) by re-loading the module
  2. Ensure no other thread frees the module handle during enumeration
  3. If errorCode is ERROR_NO_MORE_ENTRIES-like, treat the module as having no resources and return an empty map
  4. Scan the binary with a resource viewer (e.g. Resource Hacker) to confirm it has resources at all

Example fix

// before
Map<String, List<String>> names = Kernel32Util.getResourceNames(path);
// after
try {
    Map<String, List<String>> names = Kernel32Util.getResourceNames(path);
} catch (Win32Exception e) {
    if (e.getErrorCode().intValue() == WinError.ERROR_NO_MORE_ENTRIES) {
        return Collections.emptyMap(); // module exposes no resource types
    }
    throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

HANDLE h = Kernel32.INSTANCE.LoadLibraryEx(path, null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
if (h == null) throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
// keep h alive for the whole getResourceNames call

Type guard

boolean isValidModule(HMODULE h) { return h != null; }

Try / catch

try {
    Map<String, List<String>> names = Kernel32Util.getResourceNames(path);
} catch (Win32Exception e) {
    if (e.getErrorCode().intValue() == WinError.ERROR_NO_MORE_ENTRIES) {
        return Collections.emptyMap(); // module has no resource types
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling Kernel32Util.getResourceNames(path) where LoadLibraryEx succeeded but EnumResourceTypes(target, ertp, null) fails — invalid module handle, module unloaded concurrently, or a resource tree the enumerator cannot walk.

Common situations: FreeLibrary race conditions from other threads, resource sections stripped by packers/obfuscators, corrupted binaries, or exotic resource layouts in packed executables.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/4b3db6de2151a3d6. Report an issue: GitHub.