2017-01-29 4 views
1

J'essaie d'obtenir quelques informations de l'installation de Windows. J'ai été capable de le faire facilement avec le code ci-dessous en C# mais je cherche une implémentation Java.Comment utiliser kernel32.dll en Java

je dois avoir accès aux variables et méthodes suivantes:

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); 
+2

Vous aurez besoin d'un de [JNI] (http://docs.oracle.com/javase/8/docs/technotes/guides/jni /) * ou * [JNA] (https://github.com/java-native-access/jna). –

+0

Merci, je l'ai compris. Je vais répondre à ma propre question bientôt. –

Répondre

1

Après quelques recherches ici comment accomplir la tâche. Vous devez utiliser la bibliothèque 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); 
} 

Pour obtenir les informations que vous exécutez alors ce qui suit dans votre code:

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

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