我正在尝试使用jna从 java调用 winapi 函数CallNtPowerInformation。
这是我的代码:
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())
);
dll 是这样实例化的:
nativeLibraryPowrprof = Native.loadLibrary("powrprof", NativeLibraryPowrprof.class, W32APIOptions.DEFAULT_OPTIONS);
这是我使用的库接口:
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");
}
}
}
此代码有效(持续 10 秒)结果正确,但有时在 10/20 秒后它会静默地使 jvm 崩溃,我得到退出代码 -1073740940(堆损坏)。
也许我错过了什么?
陪伴而非守候
相关分类