A Single File Generator or “Custom Tool” is a COM “extension” for Visual Studio that takes a project file as input, and spits out some code. (One example is the .settings files) Visual Studio automatically includes the code in your project, and updates it every time you save.
I developed a tool that reads an Office 2010 Ribbon XML file and produces an interface will all the callbacks in your file (because implementing callback signatures is a bear).
Unfortunately, Microsoft’s documentation on the subject is incomplete and obsolete. If you search the web, you will find contradictory information…
I didn’t want my assembly in the GAC, nor did want to have to register the COM object globally. I didn’t want to create a setup project, or use any fancy generated code… I just wanted it to work.
So here are some tips that should get you going.
- First of all, there is no need to use the Visual Studio extensibility project template for this. Just create a standard Class Library in the .NET language of your choice.
- Add a reference to Microsoft.VisualStudio.Shell.10.0 and Microsoft.VisualStudio.Shell.Interop to your project. (If you do not have these, you probably need to download the Visual Studio 2010 SP1 SDK)
- In the Project Properties, in the Application tab, click “Assembly Information…” and check the “Make Assembly COM Visible” box.
- Do NOT select “Register for COM interop” on the Build tab
- On the signing tab, check sign the assembly, and select or create a key
- Add the following using statements to your class file:
using System.Runtime.InteropServices; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio;
- Add the ComVisible attribute
- Add a Guid to the class (use the Tools Menu-> Create GUID and select Item 5 for c#, item 6 for vb.net, hit copy)
- Make your class implement the IVsSingleFileGenerator interface.
- It should look like this:
[ComVisible(true)] [Guid("FAKEGUID-FFF6-4FF3-FFF7-FFFFRFRFRF45")] public class Generator : IVsSingleFileGenerator {
- DO NOT USE THIS GUID. YOU MUST GENERATE YOUR OWN
- Implement the IVsSingleFileGenerator interface’s two methods – Generate and DefaultExtension (see the documentation for more details)
- Build it!
- The easiest way to register this is using the Visual Studio local registry at HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0
Note: on 32-bit systems, this may be at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0 – I am using the 64-bit
There should be plenty of sub-keys under this key. If there are not, perhaps you need to look in a different version, or bit-ness - First, you need to add the COM information like so
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\CLSID\{FAKEGUID-FFF6-4FF3-FFF7-FFFFRFRFRF45}] "InprocServer32"="C:\\Windows\\SysWOW64\\mscoree.dll" "ThreadingModel"="Both" "Class"="DemoSFGen.Generator" "Assembly"="DemoSFGen, Version=1.0.0.0, Culture=neutral, PublicKeyToken=faketokenfffff88" "CodeBase"="file:///C:\\Users\\username\\Documents\\Visual Studio 2010\\Projects\\sfgen\\sfgen\\bin\\Debug\\sfgen.dll"
Replace the GUID after CLSID with the GUID you generated earlier; replace the Class with your Namespace.ClassName; replace Assembly with the information from your dll (I used ILSpy – I’m sure there’s an easier way); replace CodeBase with the full path to your dll.
- Next, add the registry info to tell Visual Studio about the Generator:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\Generators\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}\DemoSFGen] "CLSID"="{FAKEGUID-FFF6-4FF3-FFF7-FFFFRFRFRF45}" "GeneratesDesignTimeSource"=dword:00000001 @="Nice Name For your Generator"
The GUID after \Generators\ corresponds to the languages in Visual Studio. See the docs for registering single file generators for a couple (or have a look in the registry).
The Key name under the language guid (DemoSFGen in this case) is (I believe) how you will refer to the Custom Tool
The value of CLSID needs to match your generated GUID - Run Visual Studio, open a (different) project, and select an appropriate input file (text, xml, etc) In the properties for the file, add the name of the Custom Tool (“DemoSFGen”).
- Right click on the file, and select “Run Custom Tool”
- If everything worked, you will now be able to expand the file, and see your generated source underneath it. If there was a problem, Visual Studio may or may not display an error…
Now for the part where the documentation falls apart – registering and using this bad boy.
I know that Custom Tools aren’t the most often created things in the world, but hopefully these steps can at least get you up and running when you do try.