Generating GUID Values in Visual Studio for the WiX Toolset


WiX is a wonderful toolset that brings the process of creating setup packages more in line with other development, but it also has its pitfalls. The major annoyance I had while writing a new wxs file (a basic WiX file type) was generating GUID values for each component.

<Fragment>
  <ComponentGroup Id='ComponentGroupId' Directory='Directory'>
    <Component Id='ComponentId1' Guid='PUT-GUID-HERE'>
       <File Id='FileId1' Source='Source1' />
    </Component>
    <Component Id='ComponentId2' Guid='PUT-GUID-HERE'>
       <File Id='FileId2' Source='Source2' />
    </Component>
    <Component Id='ComponentId3' Guid='PUT-GUID-HERE'>
       <File Id='FileId3' Source='Source3' />
    </Component>
    <Component Id='ComponentId4' Guid='PUT-GUID-HERE'>
       <File Id='FileId4' Source='Source4' />
    </Component>
  </ComponentGroup>
</Fragment>

As you can see from the example above, having to specify a value for each Guid attribute is a time consuming process. Visual Studio includes the Create GUID tool for generating GUID values, however, this app is a separate process, so you can’t dock it in Visual Studio.

You will probably end up putting it on a second display and using ALT+TAB to go back and forth for creating each new GUID. What I wanted was something that I could assign to a chord (series of keystrokes) that would generate and insert a new GUID in a configurable format.

I searched through the Visual Studio Extension Gallery to see if there was something already available, but nothing available at the time really looked like what I was after. Because I knew what I wanted and I had the tools available to make an extension for Visual Studio, I set out to do just that. This provided me with an interesting opportunity to learn how to use the Visual Studio Extensibility Tools. The end result of this experiment was a functional extension that provided new menu options in my Tools menu in Visual Studio for creating and formatting the created GUID values. My initial development was for Visual Studio 2010, which we were using at the time, but I’ve since created extensions for Visual Studio 2012, 2013, 2015 and 2017 as well. What you’ll end up with after installing the VSIX package for your respective version is the following menu commands under the Tools menu.

You can see that it provides a format drop down to select which GUID format you want based on the GUID ToString method. It also defaults to the chord Ctrl+Shift+K, Ctrl+Shift+G to allow inserting without going to an outside program like the Create GUID tool.

I ended up creating separate extensions for each version of Visual Studio because it wasn’t as simple to upgrade and maintain compatibility with the previous version of Visual Studio as I’d hoped.

[2017-01-07 Update] It turns out it is a lot easier than I thought to have a single project that serves as the basis for the extension in Visual Studio 2012-2015. By editing the source.extension.vsixmanifest file I was able to adjust the supported Visual Studio versions and the dependencies using the following lines.

<?xml version="1.0" encoding="utf-8"?>
 <PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
   <!-- Other VSIX manifest definitions -->
   <Installation InstalledByMsi="false">
     <InstallationTarget Id="Microsoft.VisualStudio.Pro" Version="[11.0,14.0]" />
   </Installation>
   <Dependencies>
     <Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework"
 d:Source="Manual" Version="[4.5,4.6]" />
    <Dependency Id="Microsoft.VisualStudio.MPF" DisplayName="Visual Studio MPF"
 d:Source="Installed" Version="[11.0,14.0]" />
 </Dependencies>
   <!-- Other VSIX manifest definitions -->
 </PackageManifest>

One of the key changes here is in the InstalltionTarget entry, simply specifying Microsoft.VisualStudio.Pro will work for almost all extension supporting versions of Visual Studio. Then specifying the Visual Studio versions that will be supported, in this case [11.0,14.0] (versions 2012-2015). Next I needed to update the entries in the Dependencies to include Visual Studio MPF for the same Visual Studio versions [11.0,14.0] and.NET Framework versions [4.5,4.6].

[2017-04-11 Update] Since the VSIX package manifest changed from version 2.0 to 3.0 with the release of Visual Studio 2017, I needed to do some work to get the extension to support versions 2012-2017. The big change in version 3.0 is the addition of the Prerequisites node which lets Visual Studio know what prerequisites the extension requires, so the user is notified and they can be installed as well. I only had to add the Visual Studio core editor prerequisite for my extension, as you can see below. I also had to update the InstallationTarget version to [11.0,15.0] (versions 2012-2017) and the Visual Studio MPF Dependency version to [11.0,15.0].

<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
  <!-- Other VSIX manifest definitions -->
  <Installation InstalledByMsi="false">
    <InstallationTarget Id="Microsoft.VisualStudio.Pro" Version="[11.0,15.0]" />
  </Installation>
  <Dependencies>
    <Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework"
d:Source="Manual" Version="[4.5,4.6]" />
    <Dependency Id="Microsoft.VisualStudio.MPF" DisplayName="Visual Studio MPF"
d:Source="Installed" Version="[11.0,15.0]" />
  </Dependencies>
  <!-- Other VSIX manifest definitions -->
  <Prerequisites>
    <Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor"
Version="[15.0.26208.0,16.0)" DisplayName="Visual Studio core editor" />
  </Prerequisites>
</PackageManifest>

Below are links for the VSIX packages for Visual Studio 2012-2017 and 2010. No warranty of any kind, explicit or implied, is given, use at your own risk (I’ve been using this extension for years and haven’t had any issues with it).

Visual Studio 2012-2017

Visual Studio 2010

_______________________

Kyle Bahrke, Interstates Application Developer


RELATED POSTS