如何在CPP中获得当前版本的Visual Studio进行扩展项目

How do I get the current version of Visual studio in cpp for an extension project?

本文关键字:Studio Visual 项目 扩展 当前版本 CPP      更新时间:2023-10-16

我当前正在从事Visual Studio扩展项目,我需要在C 代码中读取 CurrentTheme 注册表值。为此,我写了以下代码

CRegKey RegKey;
LPCTSTR keyName; // SoftwareMicrosoftVisualStudio{0}General
//LPTSTR keyValue;
#if _MSC_VER >= 1600 && _MSC_VER < 1700 // VS 2010
keyName = _T("Software\Microsoft\VisualStudio\10.0\General");
#elif _MSC_VER >= 1700 && _MSC_VER < 1800 // VS 2012
keyName = _T("Software\Microsoft\VisualStudio\11.0\General");
#elif _MSC_VER >= 1800 && _MSC_VER < 1900 // VS 2013
keyName = _T("Software\Microsoft\VisualStudio\12.0\General");
#elif _MSC_VER >= 1900 && _MSC_VER < 2000 // VS 2015
keyName = _T("Software\Microsoft\VisualStudio\14.0\General");
#endif
LONG lResult = RegKey.Open( HKEY_CURRENT_USER, keyName, KEY_READ );
MessageBox(NULL, _MSC_VER , _T("Msg"), MB_OK | MB_ICONERROR);
if( ERROR_SUCCESS != lResult )
{
    return false;
}
ULONG chars;
CString keyValue;
if (RegKey.QueryStringValue(L"CurrentTheme", 0, &chars) == ERROR_SUCCESS)
{
    RegKey.QueryStringValue(L"CurrentTheme", keyValue.GetBuffer(chars), &chars);
    keyValue.ReleaseBuffer();
    MessageBox(NULL, keyValue , _T("Msg"), MB_OK | MB_ICONERROR);
}
RegKey.Close();

但是_MSC_VER似乎在编译时产生值。我需要动态创建SoftwareMicrosoftVisualStudio{0}General值,以便我知道在哪个版本的Addin Project正在运行的VisualStudio中。有人可以帮我吗?

我为此找到了解决方法。我使用了当前运行的devenv.exe的处理程序,并导航到其包装定义文件devenv.pkgdef并读取该文件中写入的RegistryRoot值。这给出了我要搜索的SoftwareMicrosoftVisualStudioxx.0值。