【问题标题】:Detecting installed MSVC redistributables programmatically以编程方式检测已安装的 MSVC 可再发行组件
【发布时间】:2016-01-29 19:03:55
【问题描述】:

有没有办法在 C++ 中做到这一点?我想这样做:

用户启动应用程序 -> 应用程序检查已安装的 redists -> 应用程序告诉用户安装可再发行 X 以使用应用程序并关闭

【问题讨论】:

    标签: c++ visual-c++ msvcrt


    【解决方案1】:

    你最好的选择可能是MsiQueryProductState

    这里有两篇不错的文章:

    How to programmatically obtain the installation state of Visual Studio .NET or Visual Studio 2005

    Determining if the VC Runtimes Are Installed (德尔福代码示例):

    // API call to msi.dll to determine is a MSI installed packed has been installed.
    // It does this by checking the state of the installed package.
    // Anything other than a return value of INSTALLSTATE_DEFAULT (5) indicates a broken or
    // no installation.
    function MsiQueryProductState(szProduct: String): Integer; external 'MsiQueryProductStateA@msi.dll stdcall';
    
    // String constants to pass to the routine.
    const
       // These constants check for any install of the version specific package
       // regardless the service pack or security update.  There are specific
       // constants for x86, x64 and i64 installations.
       VC2005_ANY_x86        = 'vc2005x86';
       VC2005_ANY_x64        = 'vc2005x64';
       VC2005_ANY_i64        = 'vc2005i64';
       ...
       // All available VC 2005 (version 8.0) installations.
       VC2005_x86            = '{A49F249F-0C91-497F-86DF-B2585E8E76B7}';
       VC2005_x64            = '{6E8E85E8-CE4B-4FF5-91F7-04999C9FAE6A}';
       ...
       // All available VC 2008 (version 9.0) installations.
       VC2008_x86            = '{FF66E9F6-83E7-3A3E-AF14-8DE9A809A6A4}';
       VC2008_x64            = '{350AA351-21FA-3270-8B7A-835434E766AD}';
       ...
    function VCRT_IsInstalled(sPackage: String): Integer;
    var
       nResult: Integer;
    
    begin
       // Check for the existence of msi.dll.  The poor man's way of checking for the
       // installation of the Windows Installation Engine.
       If FileExists(ExpandConstant('{sys}\msi.dll')) then begin
    
          // Default return value of -1, which indicated no installation.  This is only 
          // necessary for the VC*_ANY_* flags as individual return values are not captured.
          Result := -1;
    
          // Check for the package passed ia the sPackage parameter.
          case sPackage of
             VC2005_ANY_x86:
                begin
                   If (MsiQueryProductState(VC2005_x86) = 5) or
                   (MsiQueryProductState(VC2005_SP1_x86) = 5) or 
                   (MsiQueryProductState(VC2005_SP1_SECUP_x86) = 5) then begin
                      Result := 5;
                   end;
                end;
             VC2008_ANY_x86:
                begin
                   If (MsiQueryProductState(VC2008_x86) = 5) or 
                   (MsiQueryProductState(VC2008_SP1_x86) = 5) or 
                   (MsiQueryProductState(VC2008_SP1_SECUP_x86) = 5) then begin
                      Result := 5;
              ...
             VC2010_ANY_i64:
                begin
                   If MsiQueryProductState(VC2010_i64) = 5 then begin
                      Result := 5;
                   end;
                end;
    
             // All speific versions are checked here.  The return value is passed back
             // as the functions return value.
             else 
                begin
                   Result := MsiQueryProductState(sPackage);
                end;
          end;
       end else begin
    
          // MSI not installed.  Pass the specific error number for it.
          Result := INSTALLSTATE_MSIABSENT;
       end;
    end;
    

    以下是 MSVS 2015 的可再分发代码列表:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      相关资源
      最近更新 更多