Search in sources :

Example 1 with VS_FIXEDFILEINFO

use of com.sun.jna.platform.win32.VerRsrc.VS_FIXEDFILEINFO in project jna by java-native-access.

the class VersionUtil method getFileVersionInfo.

/**
     * Gets the file's version number info
     * 
     * @param filePath
     *            The path to the file
     * @return The VS_FIXEDFILEINFO structure read from the file.<br>
     *         Use the getFileVersionMajor(), getFileVersionMinor(),
     *         getFileVersionRevision(), and getFileVersionBuild()
     * @throws UnsupportedOperationException
     *             if VerQueryValue fails to get version info from the file.
     */
public static VS_FIXEDFILEINFO getFileVersionInfo(String filePath) {
    IntByReference dwDummy = new IntByReference();
    int versionLength = Version.INSTANCE.GetFileVersionInfoSize(filePath, dwDummy);
    // throw a Win32Exception with GetLastError()
    if (versionLength == 0) {
        throw new Win32Exception(Native.getLastError());
    }
    // buffer to hold version info
    Pointer lpData = new Memory(versionLength);
    // pointer to pointer to location in aforementioned buffer
    PointerByReference lplpBuffer = new PointerByReference();
    if (!Version.INSTANCE.GetFileVersionInfo(filePath, 0, versionLength, lpData)) {
        throw new Win32Exception(Native.getLastError());
    }
    // here to make VerQueryValue happy.
    IntByReference puLen = new IntByReference();
    // this does not set GetLastError, so no need to throw a Win32Exception
    if (!Version.INSTANCE.VerQueryValue(lpData, "\\", lplpBuffer, puLen)) {
        throw new UnsupportedOperationException("Unable to extract version info from the file: \"" + filePath + "\"");
    }
    VS_FIXEDFILEINFO fileInfo = new VS_FIXEDFILEINFO(lplpBuffer.getValue());
    fileInfo.read();
    return fileInfo;
}
Also used : VS_FIXEDFILEINFO(com.sun.jna.platform.win32.VerRsrc.VS_FIXEDFILEINFO) IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) Win32Exception(com.sun.jna.platform.win32.Win32Exception)

Example 2 with VS_FIXEDFILEINFO

use of com.sun.jna.platform.win32.VerRsrc.VS_FIXEDFILEINFO in project jna by java-native-access.

the class VersionUtilTest method testGetFileVersionNumbers.

public void testGetFileVersionNumbers() {
    File file = new File(System.getenv("SystemRoot"), "regedit.exe");
    assertTrue("Test file with version info in it should exist: " + file, file.exists());
    VS_FIXEDFILEINFO version = VersionUtil.getFileVersionInfo(file.getAbsolutePath());
    assertNotNull("Version info should have been returned.", version);
    assertTrue("The major file version number should be greater than 0 when pulling version from \"" + file + "\"", version.getFileVersionMajor() > 0);
    assertTrue("The minor file version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getFileVersionMinor() >= 0);
    assertTrue("The revision file version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getFileVersionRevision() >= 0);
    assertTrue("The build file version number should be greater than or equal to 0  when pulling version from \"" + file + "\"", version.getFileVersionBuild() >= 0);
    assertTrue("The major product version number should be greater than 0 when pulling version from \"" + file + "\"", version.getProductVersionMajor() > 0);
    assertTrue("The minor product version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getProductVersionMinor() >= 0);
    assertTrue("The revision product version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getProductVersionRevision() >= 0);
    assertTrue("The build product version number should be greater than or equal to 0  when pulling version from \"" + file + "\"", version.getProductVersionBuild() >= 0);
}
Also used : VS_FIXEDFILEINFO(com.sun.jna.platform.win32.VerRsrc.VS_FIXEDFILEINFO) File(java.io.File)

Aggregations

VS_FIXEDFILEINFO (com.sun.jna.platform.win32.VerRsrc.VS_FIXEDFILEINFO)2 Memory (com.sun.jna.Memory)1 Pointer (com.sun.jna.Pointer)1 Win32Exception (com.sun.jna.platform.win32.Win32Exception)1 IntByReference (com.sun.jna.ptr.IntByReference)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1 File (java.io.File)1