Archives
-
Use VS2008 to create a VSPackage that runs in VS2008 and VS2005
We all want to use the new features in Visual Studio 2008 during development. When using VS2008 for VsPackage development we need to install the Visual Studio 2008 SDK. The approach taken for building VsPackages in VS2008 is incompatible with the approach taken in VS2005. In this article I explain an approach to use VS2008 to build a compatible VsPackage that can be tested in the experimental hives of both versions of Visual Studio.
About VsPackage
- A VsPackage is a very powerful Visual Studio extensibility mechanism
- Visual studio knows about a VsPackage and its functionality through registry registrations
- To test a VsPackage without messing up your Visual Studio, the Visual Studio SDK created a special experimental “registry hive” containing all required Visual Studio registrations. Visual studio can be started with the name of this hive to test your VsPackage in a “sandbox” environment
- This hive can be reset to its initial state in case of problems or to do a clean test
- In Visual studio 2005 the hive is created under HKEY_LOCAL_MACHINE, this gives issues under Vista where a normal user may not write to this part of the registry
- In Visual studio a “per user” experimental hive is supported, so you can do Run As a Normal User development
In the Visual Studio 2005 SDK the experimental hive was created on installation because the same experimental hive was used for all users. In the Visual studio 2008 SDK the experimental hive is per used, so each user needs to recreate it’s own expirimental hive. You can do this by executing the following command from the Start Menu: Microsoft Visual Studio 2008 SDK -> Tools -> Reset the Microsoft Visual Studio 2008 Experimental hive.
What this command does is executing the following command: VSRegEx.exe GetOrig 9.0 Exp RANU. The RANU argument means: Run As a Normal User.
Referenced assemblies
A VS2008 VsPackage project will normally reference the following assemblies:
<Reference Include="Microsoft.VisualStudio.OLE.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.8.0" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.9.0" />
<Reference Include="Microsoft.VisualStudio.TextManager.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell.9.0" />
If we want to run the VsPackage on VS2005 as well, we may not reference assemblies from VS2008 (the assemblies ending with 9.0). The set of assemblies we should reference are:
<Reference Include="Microsoft.VisualStudio.OLE.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.8.0" />
<Reference Include="Microsoft.VisualStudio.TextManager.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell" />
VsPackage registration
But because Visual Studio uses a tool called RegPkg.exe to generate the required registry entries to make the VsPackage known to Visual Studio, and that the RegPkg.exe application for VS2008 uses the Microsoft.VisualStudio.Shell.9.0 assembly and the RegPkg.exe application for VS2005 uses the the Microsoft.VisualStudio.Shell assembly, we have a problem.
But Microsoft was so kind to provide a howto on migrating VsPackage projects to VS2008. See http://msdn.microsoft.com/en-us/library/cc512930.aspx for more information. If you have followed the steps to migrate an existing VS2005 project to VS2008, there is an interesting section on what to do if you want to use the Microsoft.VisualStudio.Shell assembly instead of the Microsoft.VisualStudio.Shell.9.0 assembly, and in that way stay compatible with VS2005.
The steps are as follows:
A VsPackage project has a project file that is actually an MsBuild file. To open this file in Visual studio do the following:
- Right click on the VsPackage project
- Select Unload Project (save changes if requested)
- See the project named changed from FooBar to FooBar (unavailable)
- Right click again and select Edit FooBar.csproj
- Add the following lines to the last PropertyGroup:
<!-- Make sure we are 2005 compatible, and don't rely on RegPkg.exe
of VS2008 which uses Microsoft.VisualStudio.Shell.9.0 -->
<UseVS2005MPF>true</UseVS2005MPF>
<!-- Don't try to run as a normal user (RANA),
create experimental hive in HKEY_LOCAL_MACHINE -->
<RegisterWithRanu>false</RegisterWithRanu> - Save project file
- Right click on the project and select Reload Project
After these changes the RegPkg.exe for VS2005 is used to generate the registry settings. But this tool can only generate registry settings for a hive in HKEY_LOCAL_MACHINE, and not for a hive in HKEY_CURRENT_USER. This means that development must be done as an administrator.
If we now build the application we get the following error: “Failed to retrieve paths under VSTemplate for the specified registry hive.”, we must register a new registry hive for VS2008 in HKEY_LOCAL_MACHINE with the following command: VSRegEx.exe GetOrig 9.0 Exp.
For more information on the RegPkg.exe utility, see the excellent blog post at http://www.architekturaforum.hu/blogs/divedeeper/archive/2008/01/22/LearnVSXNowPart8.aspx. Don’t forget to read the rest of that blog post series.
Last step to do is changing the way VS2008 is launched when debugging the VsPackage with F5. Right-click the VsPackage project and select properties. Now remove RANU from the command line arguments box:
Because we want to target both VS2005 and VS2008 with the VsPackage, we should also test the package on VS2005. So assuming that VS2005 is installed on the development machine as well, create an experimental hive for VS2005 using the command: VSRegEx.exe GetOrig 8.0 Exp. We don’t need to install this experimental hive if we installed the Visual Studio SDK for VS2005 as well.
To register the VsPackage for VS2005 as well and start VS2005 under the experimental hive, you can add a small batch script to your solution. Add a file TestInVS2005.bat in the root of the solution folder as a solution item. The TestInVS2005.bat file should have the following content:
"%VSSDK90Install%VisualStudioIntegration\Tools\Bin\VS2005\regpkg.exe" /root:Software\Microsoft\VisualStudio\8.0Exp /codebase %~dp0FooBar\bin\Debug\FooBar.dll
"%VS80COMNTOOLS%..\IDE\devenv.exe" /rootSuffix exp
Pause
We can now right-click on the TestInVs2005.bat file, select Open With… and browse to the TestInVs2005.bat file itself to open the file with. It then starts the batch script.