概述
- 本文演示坏境: win11 + vs2019 / vs2015
- power shell (管理员)
步骤1
- 打开powerhsell 管理员模式, 查询本机安装的vcredis的版本, 比如, 下面的代码查询本机的vc2019 redis的唯一ID
Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%Visual C++ 2019%'"
- 将 上面代码中的2019换成你想要查询的vc++版本即可, 比如2010, 2012, 2015, 2017 , 2019 2022......
步骤2
不会查询? 再看一个例子
代码中检查是否包含再发行包
1 使用函数  MsiQueryProductState
- 改函数的参数是字符串, 这里可以传递 powershell中查询的唯一ID
头文件
#include 
库文件
#pragma comment(lib, "Msi.lib")
MsiQueryProductState 返回值
	INSTALLSTATE_NOTUSED      = -7,  // component disabled
	INSTALLSTATE_BADCONFIG    = -6,  // configuration data corrupt
	INSTALLSTATE_INCOMPLETE   = -5,  // installation suspended or in progress
	INSTALLSTATE_SOURCEABSENT = -4,  // run from source, source is unavailable
	INSTALLSTATE_MOREDATA     = -3,  // return buffer overflow
	INSTALLSTATE_INVALIDARG   = -2,  // invalid function argument
	INSTALLSTATE_UNKNOWN      = -1,  // unrecognized product or feature
	INSTALLSTATE_BROKEN       =  0,  // broken
	INSTALLSTATE_ADVERTISED   =  1,  // advertised feature
	INSTALLSTATE_REMOVED      =  1,  // component being removed (action state, not settable)
	INSTALLSTATE_ABSENT       =  2,  // uninstalled (or action state absent but clients remain)
	INSTALLSTATE_LOCAL        =  3,  // installed on local drive
	INSTALLSTATE_SOURCE       =  4,  // run from source, CD or net
	INSTALLSTATE_DEFAULT      =  5,  // use default, local or source
用法 范例
	INSTALLSTATE ret = MsiQueryProductState(str);
	/// INSTALLSTATE_DEFAULT- 已经存在, 否则, 不存在
	if (INSTALLSTATE_DEFAULT != ret)
	{
		//str_result = L"false";
		str_result.Format(L"index=%d----%d", index, ret);
	}
	else
	{
		str_result.Format(L"index=%d----true", index);
	}
一个c++代码使用范例
UI示例
核心代码
	static int index = 0;
	CString str;
	// 读取界面输入的唯一ID:{XXXX-XXX-XXX-XXX}
	edit_src.GetWindowText(str);
	if (TRUE == str.IsEmpty())
	{
		return;
	}
	CString str_result;
	///  str - 唯一ID
	INSTALLSTATE ret = MsiQueryProductState(str);
	/// INSTALLSTATE_DEFAULT- 已经存在, 否则, 不存在
	if (INSTALLSTATE_DEFAULT != ret)
	{
		//str_result = L"false";
		str_result.Format(L"index=%d----%d", index, ret);
	}
	else
	{
		str_result.Format(L"index=%d----true", index);
	}
	++index;
	edit_result.SetWindowText(str_result);