c++ - 以编程方式检测已安装的 MSVC 可再发行组件

标签 c++ visual-c++ msvcrt

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

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

最佳答案

也许你最好的选择是 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 '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="175a647e466272656e47657873627463446376637256577a647e39737b7b" rel="noreferrer noopener nofollow">[email protected]</a> 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 的可再发行代码列表:

关于c++ - 以编程方式检测已安装的 MSVC 可再发行组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092126/

相关文章:

c++ - 我正在尝试在 Visual Studio 2010 中编译 C++ 代码,但出现链接器错误

c++ - 如何在 Visual C++ 6.0 中使用 NormalizeString()?

c++ - 将节点插入二叉搜索树

wix - 使用 WiX 自动安装运行时库

c++ - 阻止我的项目调用__CxxFrameHandler3(CRT函数)

c++ - 将枚举传递给 C++ 中的函数

c++ - Opencv 程序退出,代码为 -1073741819

android - 切换到 GCC 4.8 后,Eclipse 未更新 "built-in"包括 Android NDK 项目的路径

c++ - visual studio 已触发断点 Poco NotificationCenter

c - 微软下划线 C 函数的目的是什么?