2017-01-29 12 views
1

Windows 설치에서 일부 정보를 얻으려고합니다. C#에서 아래 코드를 사용하여이 작업을 쉽게 수행 할 수 있었지만 Java 구현을 찾고 있습니다. 여기에 몇 가지 더 많은 연구가 작업을 완료하는 방법 후Java에서 kernel32.dll을 사용하는 방법

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

당신은 [JNI] 중 하나를해야합니다 (http://docs.oracle.com/javase/8/docs를/technotes/guides/jni /) * 또는 * [JNA] (https://github.com/java-native-access/jna). –

+0

감사합니다. 나는 내 자신의 질문에 곧 대답 할 것이다. –

답변

1

:

나는 다음과 같은 변수와 메소드에 액세스 할 수 있어야합니다. 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!"); 
}