如何在cpp中为扩展项目获取当前版本的Visual Studio?

问题描述:

我目前正在Visual Studio扩展项目中,我需要阅读C++代码中的CurrentTheme注册表值。对于我写了下面的代码如何在cpp中为扩展项目获取当前版本的Visual Studio?

CRegKey RegKey; 
LPCTSTR keyName; // Software\Microsoft\VisualStudio\{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似乎在编译时产生价值。我需要动态创建Software\Microsoft\VisualStudio\{0}\General值,以便我了解我的插件项目在哪个版本的VisualStudio中运行。任何人都可以帮助我吗?

+0

使用dte.Version并查看http://*.com/questions/15920572/how-to-get-current-used-color-theme-of-visual-studio2012 –

我找到了解决方法。我使用当前运行的devenv.exe的处理程序,并导航到它的程序包定义文件devenv.pkgdef并读取该文件中写入的值RegistryRoot。这给我正在寻找的Software\Microsoft\VisualStudio\xx.0值。