通过MsiQueryProductState检测VC运行库是否安装

MsiQueryProductState

通过MSI的API来查询可判断MSI安装程序是否已被安装(例如VC++运行库)。

MsiQueryProductState的输入参数为产品ID,其返回值为产品安装状态,若返回值为 5(INSTALLSTATE_DEFAULT)则表示已安装。

返回值及安装状态

INSTALLSTATE               |   value  |    产品的安装状态
----------------------------------------------------------------
INSTALLSTATE_UNKNOWN       |    -1    |    该产品未被广告或安装
INSTALLSTATE_ADVERTISED    |     1    |    该产品已广告但未安装
INSTALLSTATE_ABSENT        |     2    |    该产品为不同的用户安装
INSTALLSTATE_DEFAULT       |     5    |    该产品已为当前用户安装

NSIS检测代码示例

下面是基于安装包制作工具NSIS的检测代码示例。

NSIS检测代码示例

OutFile ".\msi.exe"
InstallDir $TEMP

XPStyle on
InstallColors /windows
ShowInstDetails show
Section MsiQueryProductState
    System::Call 'msi::MsiQueryProductState(t"{1D8E6291-B0D5-35EC-8441-6616F567A0F7}")i.R0'
    DetailPrint "返回值:$R0"
    IntCmp $R0 5 INSTALLSTATE_DEFAULT STATE_OTHER

    STATE_OTHER:
    IntCmp $R0 1 INSTALLSTATE_ADVERTISED INSTALLSTATE_ABSENT INSTALLSTATE_UNKNOWN

    INSTALLSTATE_UNKNOWN:
    DetailPrint "安装状态:该产品未被广告或安装"
    Goto Done

    INSTALLSTATE_ADVERTISED:
    DetailPrint "安装状态:该产品已广告但未安装"
    Goto Done

    INSTALLSTATE_ABSENT:
    DetailPrint "安装状态:该产品为不同的用户安装"
    Goto Done

    INSTALLSTATE_DEFAULT:
    DetailPrint "安装状态:该产品已为当前用户安装"

    Done:
SectionEnd

附:最新VC运行库GUID

最新VC运行库GUID

VCredist_2005x64 8.0.61000
{AD8A2FA1-06E7-4B0D-927D-6E54B3D31028}

VCredist_2005x86 8.0.61001
{710F4C1C-CC18-4C49-8CBF-51240C89A1A2}

VCredist_2008x64 9.030729.6161
{5FCE6D76-F5DC-37AB-B2B8-22AB8CEDB1D4}

VCredist_2008x86 9.030729.6161
{9BE518E6-ECC6-35A9-88E4-87755C07200F}

VCredist_2010x64 10.0.40219
{1D8E6291-B0D5-35EC-8441-6616F567A0F7}

VCredist_2010x86 10.0.40219
{F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}

VCredist_2012x64 11.0.61030.0
Additional-Runtime:
{37B8F9C7-03FB-3253-8781-2517C99D7C00}
Minimum Runtime:
{CF2BEA3C-26EA-32F8-AA9B-331F7E34BA97}

VCredist_2012x86 11.0.61030.0
Additional Runtime:
{B175520C-86A2-35A7-8619-86DC379688B9}
Minimum Runtime:
{BD95A8CD-1D9F-35AD-981A-3E7925026EBB}

VCredist_2013x64 12.0.40664.0
Additional Runtime:
{010792BA-551A-3AC0-A7EF-0FAB4156C382}
Minimum Runtime:
{53CF6934-A98D-3D84-9146-FC4EDF3D5641}

VCredist_2013x86 12.0.40664.0
Additional Runtime:
{D401961D-3A20-3AC7-943B-6139D5BD490A}
Minimum Runtime:
{8122DAB1-ED4D-3676-BB0A-CA368196543E}

VCredist_2015x64 14.0.24516.0
Additional Runtime:
{6B66663C-055F-3A2E-A09D-168840A82362}
Minimum Runtime:
{EE6E34BF-D825-384C-AFF5-305DF5CFAF5A}

VCredist_2015x86 14.0.24516.0
Additional Runtime:
{B4EB15A2-6582-346E-8501-B6E907F23B80}
Minimum Runtime:
{7B82F823-A226-3463-B438-AF4DDDE2B810}

VCredist_2017x64 14.14.26429.4
Additional Runtime:
{B12F584A-DE7A-3EE3-8EC4-8A64DBC0F2A7}
Minimum Runtime:
{03EBF679-E886-38AD-8E70-28658449F7F9}

VCredist_2017x86 14.14.26429.4
Additional Runtime:
{6F0267F3-7467-350D-A8C8-33B72E3658D8}
Minimum Runtime:
{7753EC39-3039-3629-98BE-447C5D869C09}
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注