Developing Custom Tool (aka Single File Generators) for Visual Studio 2012

I could not find any guides how register your own Custom Tools in Visual Studio 2012.
MSDN guide is wrong

So I have to take Process Monitor and using trial-and-error approach I found out how to develop and register a Custom Tool in VS2012.

1. Install Visual Studio 2012 SDK
2. Create a new Class Library project in Visual Studio 2012.
3. Open Project Properties -> Application -> Assembly Information -> Make assembly COM visible

Make assembly COM visible

4. Add project references to the following assemblies (should be in GAC after step 1)

  • Microsoft.VisualStudio.OLE.Intero
  • Microsoft.VisualStudio.Shell.Interop
  • Microsoft.VisualStudio.TextTemplating.VSHost.11.0

5. Implement your generator. Use code below as an example.
According to documentation it is enough to just implement IVsSingleFileGenerator interface. But that interface is too complex and is too low-level abstraction.
So we will use better high-level abstraction from BaseCodeGeneratorWithSite base class.

using System;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.VisualStudio.TextTemplating.VSHost;

namespace TestCustomTool
{
    [ComVisible(true)]
    // TODO: Replace with your GUID
    [Guid("3B850477-71D3-4916-A3A3-D1BD0749BFAA")]
    public class TestCustomToolGenerator : BaseCodeGeneratorWithSite
    {
        public override string GetDefaultExtension()
        {
            // TODO: Replace with your implementation
            return ".test.txt";
        }

        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            // TODO: Replace with your implementation
            var generatedText = string.Format(@"{1}{0}{0}Filename: {2}{0}{0}Timestamp: {3}", Environment.NewLine, inputFileContent, inputFileName, DateTime.Now);
            return Encoding.UTF8.GetBytes(generatedText);
        }
    }
}

6. Compile the project.

Now our tool is developed and we have to register it

7. Copy resultant dll into some persistent place, e.g. C:\Program Files\TestCustomTool\TestCustomTool.dll
8. Start command prompt as admin
9. Register assembly using the following snippet

# TODO: Replace with your dll file
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe /codebase "C:\Program Files\TestCustomTool\TestCustomTool.dll"

10. Create and execute the following Install.reg file

Windows Registry Editor Version 5.00
 
; TODO: Replace with your GUID
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\11.0_Config\CLSID\{3B850477-71D3-4916-A3A3-D1BD0749BFAA}]
"InprocServer32"="C:\\Windows\\System32\\mscoree.dll"
"ThreadingModel"="Both"
; TODO: Replace with your class
"Class"="TestCustomTool.TestCustomToolGenerator"
; TODO: Replace with your assembly
"Assembly"="TestCustomTool, Version=1.0.0.0, Culture=neutral"
; TODO: Replace with your dll file
"CodeBase"="file:///C:\\Program Files\\TestCustomTool\\TestCustomTool.dll"

; TODO: Replace with your tool name
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0_Config\Generators\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}\TestCustomTool]
; TODO: Replace with your GUID
"CLSID"="{3B850477-71D3-4916-A3A3-D1BD0749BFAA}"
"GeneratesDesignTimeSource"=dword:00000001
; TODO: Replace with your description
@="Test Custom Tool"

11. Now your Custom Tool is registered. Let’s try if it works
12. Restart Visual Studio
13. Open any project
14. Create text file
15. Modify property of this text file and set it to be TestCustomTool
Custom Tool
16. Write some context to that file and save it.
17. If we are lucky we don’t get any errors and will see nested .test.txt file – congratulations! We did it!

Result

You can get all the sources used from repository https://bitbucket.org/mnaoumov/testcustomtool-vs2012/

Advertisements

About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

5 Responses to Developing Custom Tool (aka Single File Generators) for Visual Studio 2012

  1. George says:

    Thank you Michael for this Custom Tool guide. It worked for me. But still there is one issue and please comment if you know how to solve it.

    The issue is that Custom Tool registration works for user level:(HKEY_CURRENT_USER\..\11.0_Config)
    In real life setup should install it on machine level so all the users can see and use it. I have made and deployed a custom tool for Studio .., 2005, 2008, 2010 and registration was on machine level. While migrating to VS2012 I spent several hours trying to register it but failed all combinations I tried did not work:
    HKEY_LOCAL_MACHINE\Software\…\11.0
    HKEY_LOCAL_MACHINE\Software\…\11.0_Config
    HKEY_LOCAL_MACHINE\Wow6432Node\…\11.0
    HKEY_LOCAL_MACHINE\Wow6432Node\…\11.0_Config
    (“…” stands here for “Microsoft\VisualStudio”)
    Well, I could install it for every user automatically when user starts Studio and my code notices there are missing certain registry keys but this is ugly solution and while uninstalling it I wouldn’t be able to clear all possible users’ registry keys.

    Do you know by chance if this is possible at all and how? As you wrote, MSDN for VS2012 contains false info regardin this. Maybe you know if there is a way to load custom tool in runtime while connecting Add-In? For Extenders there is such option to load and unload them dynamically. Would be nice to have it for Custom Tools.

    Thanks in advance,
    George

    • mnaoumov says:

      George, sorry I have no idea about installation for all users. You already tried everything that I would try 🙂

      If you ok to try your idea with auto-registration when user starts VS, I would suggest you to have a custom registry key where you would add all the current user names. You can use this registry key during uninstall to clean all users’ registry keys.

  2. George says:

    Michael, thanks for fast reply. I promise I’ll leave a comment if I find a solution for this.

  3. Hi,

    I also had the problem George had I first found this post and finally succeeded in loading my customTool registering it in HKEY_LOCAL_MACHINE\Wow6432Node\…\11.0:
    The msdn is not wrong accoring to me but there seem to have a cache to purge -> just run a vs2012 command prompt in admin mode and launch the command line devenv /setup
    (see msdn: http://msdn.microsoft.com/en-us/library/ex6a2fad.aspx)

    Hope this will also work for George…

    my custom tool by the way if anybody is interesting in live AOP weaving :-):
    http://interfaceweaver.codeplex.com/
    http://charly-studio.com/blog/aop-customtool-poc/

  4. Martin says:

    Thanks for the guide!
    Maybe one should mention that this only works (for me) when targeting .NET 4.5.
    The Microsoft.VisualStudio.TextTemplating.VSHost.11.0 was not available on 4.0.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s