【问题标题】:JNA call works with 32-bit JRE but not with 64-bit JREJNA 调用适用于 32 位 JRE,但不适用于 64 位 JRE
【发布时间】:2013-04-30 23:43:21
【问题描述】:

我正在尝试从我创建的 JNA 接口调用 system32 的 CreateProcessW。问题是当我从 32 位 JRE 运行软件时它运行良好,但是当我转移到 64 位 JRE 时它会导致 JVM 崩溃。

Kernel32 kernel = (Kernel32) Native.loadLibrary("kernel32",
        Kernel32.class, new HashMap<String, Object>() {
            private static final long serialVersionUID = 1L;

            {
                put(Library.OPTION_FUNCTION_MAPPER,
                        W32APIFunctionMapper.UNICODE);
                put(Library.OPTION_TYPE_MAPPER,
                        W32APITypeMapper.UNICODE);
            }
        });

ProcessInformation processInformation = new ProcessInformation();
byte[] startupInfo = new byte[67];

int num2 = BitConverter.toInt32(bytes, 60);
int num = BitConverter.toInt16(bytes, num2 + 6);
IntByReference ptr4 = new IntByReference(BitConverter.toInt32(bytes, num2 + 0x54));
kernel.CreateProcessW(surrogateProcess, null, 0, 0, false, 4, 0,
            null, startupInfo, processInformation);

我的Kernel32 JNA 接口:

public interface Kernel32 extends StdCallLibrary {

    boolean CreateProcessW(String appName, String commandLine, int procAttr,
        int thrAttr, boolean inherit, int creation, int env, String curDir,
        byte[] sInfo, ProcessInformation pInfo);

}

我的ProcessInformation JNA 结构:

public final class ProcessInformation extends Structure implements ByReference {

    public IntByReference hProcess;
    public IntByReference hThread;
    public int dwProcessId;
    public int dwThreadId;

    @Override
    protected List<String> getFieldOrder() {
        return Arrays
                .asList("hProcess", "hThread", "dwProcessId", "dwThreadId");
    }

}

最后,这是我在 64 位 JRE 上运行时出现的错误:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000077a4e711, pid=1888, tid=8968
#
# JRE version: 6.0_43-b01
# Java VM: Java HotSpot(TM) 64-Bit Server VM (20.14-b01 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [ntdll.dll+0x4e711]
#
# An error report file with more information is saved as:
# C:\Users\Thomas\workspace\trident\hs_err_pid1888.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

【问题讨论】:

    标签: jvm java jna


    【解决方案1】:

    您的CreateProcess 映射是伪造的。您从哪里得到可以使用 Java int 来表示指针值的想法?哦,没关系,win32 API 一定教过你。

    您必须使用Pointer 或PointerType 或等价物来表示指针,或者如果您真的坚持使用整数值,至少使用long(64 位) 64 位平台。

    顺便说一句,JNA 包含一个 platform.jar,其中包含 kernel32 映射,包括 CreateProcess。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-03
      • 2013-09-11
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 2015-11-09
      相关资源
      最近更新 更多