来自一个例程的产品版本字符串

时间:2016-08-01 20:06:20

标签: nsis

我想从给定的可执行文件中读取产品版本(optional string)(实际上来自我尝试创建的安装程序,如果它有任何区别),如果可以在运行时。该字符串将进一步用于从链接下载文件。

非常感谢!

1 个答案:

答案 0 :(得分:0)

NSIS不支持阅读VS_FIXEDFILEINFO ->dwFileVersion以外的任何内容,因此您必须直接调用Windows API:

; Add some version information so we have something to test
VIProductVersion 1.2.3.4
VIAddVersionKey "ProductVersion" "One Two Three Four"
VIAddVersionKey "FileVersion" "Whatever"
VIAddVersionKey "FileDescription" "Whatever"
VIAddVersionKey "LegalCopyright" "(C) Whatever"

!include LogicLib.nsh
Function GetFileVerFirstLangProductVersion
System::Store S
pop $3
push "" ;failed ret
System::Call 'version::GetFileVersionInfoSize(t"$3",i.r2)i.r0'
${If} $0 <> 0
    System::Alloc $0
    System::Call 'version::GetFileVersionInfo(t"$3",ir2,ir0,isr1)i.r0 ? e'
    pop $2
    ${If} $0 <> 0
    ${AndIf} $2 = 0 ;a user comment on MSDN said you should check GLE to avoid crash
        System::Call 'version::VerQueryValue(i r1,t "\VarFileInfo\Translation",*i0r2,*i0)i.r0'
        ${If} $0 <> 0
            System::Call '*$2(&i2.r2,&i2.r3)'
            IntFmt $2 %04x $2
            IntFmt $3 %04x $3
            System::Call 'version::VerQueryValue(i r1,t "\StringFileInfo\$2$3\ProductVersion",*i0r2,*i0r3)i.r0'
            ${If} $0 <> 0
                pop $0
                System::Call *$2(&t$3.s)
            ${EndIf}
        ${EndIf}
    ${EndIf}
    System::Free $1
${EndIf}
System::Store L
FunctionEnd

Section
Push "$ExePath" ; Read our own version information in this example
Call GetFileVerFirstLangProductVersion
Pop $0
DetailPrint "ProductVersion=$0"
SectionEnd