【问题标题】:Inno setup: detect installation based on product codeInno setup:根据产品代码检测安装
【发布时间】:2015-06-17 13:13:19
【问题描述】:

我想实现类似于Inno setup - skip installation if other program is not installed的东西

但我确实有 msiexec 产品代码(如 D3AA40C4-9BFB-4640-88CE-EDC93A3703CC)。那么如何根据这个产品代码检测是否安装了其他程序呢?

【问题讨论】:

    标签: inno-setup


    【解决方案1】:

    为此有MsiQueryProductState 函数。以下是它的导入,其中包含用于您的任务的辅助函数:

    [Code]
    #IFDEF UNICODE
      #DEFINE AW "W"
    #ELSE
      #DEFINE AW "A"
    #ENDIF
    type
      INSTALLSTATE = Longint;
    const
      INSTALLSTATE_DEFAULT = 5;
    
    function MsiQueryProductState(szProduct: string): INSTALLSTATE; 
      external 'MsiQueryProductState{#AW}@msi.dll stdcall';
    
    function IsProductInstalled(const ProductID: string): Boolean;
    begin
      Result := MsiQueryProductState(ProductID) = INSTALLSTATE_DEFAULT;
    end;
    

    及其可能的用法:

    if IsProductInstalled('{D3AA40C4-9BFB-4640-88CE-EDC93A3703CC}') then
      MsgBox('The product is installed.', mbInformation, MB_OK);
    

    【讨论】:

    • 谢谢。 INSTALLSTATE_DEFAULT 是未知的,所以简单地使用'5':MsiQueryProductState(ProductID) = 5; // from msi.h
    • 不客气!但是不,不要那样做。不要使用神奇的常量。有一天,您或其他人会来到您的脚本并想知道这到底是什么 5。对不起,这是我的copy paste failure
    • 如果其他人也有同样的问题:我总是得到 -2 (INSTALLSTATE_INVALIDARG) 作为 MsiQueryProductState 的返回值。将{} 添加到通话中很重要,所以IsProductInstalled('{696C9E52-ADB6-42C8-B6E3-026EF21D369A}')
    • 天啊!又一个错误。对不起。