gpt4 book ai didi

java - 如何在Java中使用kernel32.dll

转载 作者:行者123 更新时间:2023-11-30 06:55:29 46 4
gpt4 key购买 nike

我正在尝试从 Windows 安装中获取一些信息。我可以使用下面的 C# 代码轻松完成此操作,但我正在寻找 Java 实现。

我需要访问以下变量和方法:

    internal struct OSVERSIONINFOEX
{
public Int32 dwOSVersionInfoSize;
public Int32 dwMajorVersion;
public Int32 dwMinorVersion;
public Int32 dwBuildNumber;
public Int32 dwPlatFormId;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public String szCSDVersion;

public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
public byte wProductType;
public byte wReserved;
}

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean GetProductInfo(
[In] Int32 dwOSMajorVersion,
[In] Int32 dwOSMinorVersion,
[In] Int32 dwSpMajorVersion,
[In] Int32 dwSpMinorVersion,
[Out] out Int32 pdwReturnedProductType);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean GetSystemMetrics([In] Int32 nIndex);

最佳答案

经过更多研究,这里是如何完成任务。您必须使用 JNA 库。

public interface Kernel32 extends com.sun.jna.platform.win32.Kernel32 {
// Method declarations, constant and structure definitions go here

Kernel32 INSTANCE = (Kernel32)
Native.loadLibrary("kernel32", Kernel32.class, com.sun.jna.win32.W32APIOptions.DEFAULT_OPTIONS);

boolean GetVersionEx(WinNT.OSVERSIONINFOEX osVersionInfo);

boolean GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
IntByReference pdwReturnedProductType);

boolean GetSystemMetrics(int nIndex);
}

public static boolean GetVersionInfo(WinNT.OSVERSIONINFOEX osVersionInfo) {
return Kernel32.INSTANCE.GetVersionEx(osVersionInfo);
}

要获取信息,请在代码中运行以下命令:

WinNT.OSVERSIONINFOEX osVersionInfo = new WinNT.OSVERSIONINFOEX();

if (!NativeMethods.GetVersionInfo(osVersionInfo)) {
System.out.println("Info failed to load!");
}

关于java - 如何在Java中使用kernel32.dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41924063/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com