Resumé

Visual Studio 2005 Extensibility and Custom Tools

Cristian Merighi () 0.00

A teasing project since long time, recently the occasion to dedeicate myself to it and now the huge satisfaction for the success: creating a custom tool "Code Generator" for Visual Studio 2005.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

Wow! ...errr ...It's not about Vista, it's about Visual Studio Extensibility!
In my constant seek for code reusability and easeability (do you like this neologism of mine?), I've been figuring out how to generate code using a simple config file (an xml which is subject to a precise schema). Just to be clearer, the way it happens when you use Resource files (.resx), or Typed Datasets (.xsd) in your projects.

The goal to reach was challenging - I'm a total newbie in Visual Studio Extensibility and the documentation about is pretty poor actually - so I tried...

Preannouncing my big success :), I'm not going to put code for this article but something more important: a to-do list in order to get your codegenerator registered and useable in Visual Studio 2005.

  1. First of All. Create a Class Library project and add the following references:
    • EnvDTE;
    • EnvDTE80
    • Microsoft.VisualStudio.OLE.Interop;
    • Microsoft.VisualStudio.Shell;
    • Microsoft.VisualStudio.Shell.Design;
    • Microsoft.VisualStudio.Shell.Interop;
    • Microsoft.VisualStudio.Shell.Interop.8.0;
    • VSLangProj;
    • VSLangProj2;
    • VSLangProj80.
  2. The fooCodeGenerator class you're going to develop must implement the following interfaces:
    • Microsoft.VisualStudio.Shell.Interop.IVsSingleFileGenerator
    • Microsoft.VisualStudio.OLE.Interop.IObjectWithSite
    This could be a very resource spending task to accomplish but, fortunately, you can lean on Microsoft's Visual Studio SDK which provides the nugget sample project "SingleFileGenerator. You can use the base implementation you find there.
  3. Once you have successfully built your project, do not forget to assign a strong name to the assembly, because it has to be put into the global assembly cache (GAC).
    So use the sn.exe tool and sign it!
  4. To register your assembly into the GAC, you can use the gacutil.exe via commandline:
    c:\> gacutil /i e:\mypath_to_the_assembly\myassembly.dll
            
    gacutil.exe
  5. Now you have to register the same assembly in order to have COM visibility.
    I forgot to mention that you must mark the main class in your project with the System.Runtime.InteropServices.ComVisibleAttribute set to true.
    You have the regasm.exe tool for that:
    c:\> regasm e:\mypath_to_the_assembly\myassembly.dll
            
    regasm.exe
  6. Last, but not least, the registry merging of VisualStudio keys related to the Generators section:
    You must mark your class with a randomly generated System.Runtime.InteropServices.GuidAttribute. Then use the same Guid, the chosen Custom Tool name, and the fully qualified assembly name of your GAC registered assembly to write the following .reg file:
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\CLSID\{1ee90719-1d96-4f16-b1cb-2abbb6fd2ac8}]
    @="Pacem.VisualStudio.Generators.AdaptxCodeGenerator"
    "InprocServer32"="C:\\WINDOWS\\system32\\mscoree.dll"
    "Class"="Pacem.VisualStudio.Generators.AdaptxCodeGenerator"
    "Assembly"="Pacem.VisualStudio, Version=1.0.0.0, Culture=neutral, PublicKeyToken=659a547d5fd6a604"
    "ThreadingModel"="Both"
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Generators\{164B10B9-B200-11D0-8C61-00A0C91E29D5}\AdaptxCodeGenerator]
    @="Pacem.Data.Adapters Class Generator"
    "CLSID"="{1ee90719-1d96-4f16-b1cb-2abbb6fd2ac8}"
    "GeneratesDesignTimeSource"=dword:00000001
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Generators\{E6FDF8B0-F3D1-11D4-8576-0002A516ECE8}\AdaptxCodeGenerator]
    @="Pacem.Data.Adapters Class Generator"
    "CLSID"="{1ee90719-1d96-4f16-b1cb-2abbb6fd2ac8}"
    "GeneratesDesignTimeSource"=dword:00000001
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Generators\{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}\AdaptxCodeGenerator]
    @="Pacem.Data.Adapters Class Generator"
    "CLSID"="{1ee90719-1d96-4f16-b1cb-2abbb6fd2ac8}"
    "GeneratesDesignTimeSource"=dword:00000001
            

...Et voila!
Look at the followin screenshots:

adaptx codegenerator

Hope it helped.

Take care. Bye.

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds