Java JNA WinAPI函数在JVM中无声崩溃

Java JNA WinAPI函数在JVM中无声崩溃,java,winapi,jvm,jna,Java,Winapi,Jvm,Jna,我试图用jna从java调用winapi函数 这是我的代码: NativeProcessorPowerInformation[] systemProcessors = new NativeProcessorPowerInformation[getProcessorCount()]; for (int systemProcessorIndex = 0; systemProcessorIndex < systemProcessors.length; systemProcessorIndex++

我试图用jna从java调用winapi函数

这是我的代码:

NativeProcessorPowerInformation[] systemProcessors = new NativeProcessorPowerInformation[getProcessorCount()];
for (int systemProcessorIndex = 0; systemProcessorIndex < systemProcessors.length; systemProcessorIndex++) {
    systemProcessors[systemProcessorIndex] = new NativeProcessorPowerInformation();
}

nativeLibraryPowrprof.CallNtPowerInformation(11, null, new NativeLong(0),
    systemProcessors[0], new NativeLong(systemProcessors.length * systemProcessors[0].size())
);
这是我使用的库接口:

public static interface NativeLibraryPowrprof extends StdCallLibrary {
    public int CallNtPowerInformation(int informationLevel, Pointer lpInputBuffer, NativeLong nInputBufferSize, Structure lpOutputBuffer, NativeLong nOutputBufferSize);

    @ToString
    public static class NativeProcessorPowerInformation extends Structure {
        public ULONG Number;
        public ULONG MaxMhz;
        public ULONG CurrentMhz;
        public ULONG MhzLimit;
        public ULONG MaxIdleState;
        public ULONG CurrentIdleState;

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("Number", "MaxMhz", "CurrentMhz", "MhzLimit", "MaxIdleState", "CurrentIdleState");
        }
    }
}
public静态接口nativelibrarypowrpof扩展StdCallLibrary{
公共int-CallNtPowerInformation(int-informationLevel、指针lpInputBuffer、NativeLong-nInputBufferSize、结构lpOutputBuffer、NativeLong-nOutputBufferSize);
@托斯特林
公共静态类NativeProcessorPowerInformation扩展结构{
公共电话号码;
公共ULONG MaxMhz;
公共图书馆;
公共乌隆MhzLimit;
乌龙公共屋;;
乌龙公共屋;;
@凌驾
受保护列表getFieldOrder(){
返回数组;
}
}
}
这段代码有效(持续10秒),结果是正确的,但有时在10/20秒后,它会无声地使jvm崩溃,我得到退出代码-1073740940(堆损坏)


也许我遗漏了什么?

您正在传递Java数组中第一个
结构的地址,该数组是由完全不同的
结构
实例构建的。被调用者需要一个连续的内存块,而您只传递单个结构大小的块,但告诉被调用者它是N个结构的大小

使用
Structure.toArray()
获取连续分配的内存块。然后,如果需要,可以操纵数组的成员。JNA应该在调用后自动更新数组的所有成员

public static interface NativeLibraryPowrprof extends StdCallLibrary {
    public int CallNtPowerInformation(int informationLevel, Pointer lpInputBuffer, NativeLong nInputBufferSize, Structure lpOutputBuffer, NativeLong nOutputBufferSize);

    @ToString
    public static class NativeProcessorPowerInformation extends Structure {
        public ULONG Number;
        public ULONG MaxMhz;
        public ULONG CurrentMhz;
        public ULONG MhzLimit;
        public ULONG MaxIdleState;
        public ULONG CurrentIdleState;

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("Number", "MaxMhz", "CurrentMhz", "MhzLimit", "MaxIdleState", "CurrentIdleState");
        }
    }
}