Sunday, October 07, 2007

Implementing custom tool in Visual Studio 2008

I've recently wrote about making tool to generate C# code. We may say that are macros for C# (it fact it is only one defined macro — for fields and properties).

And how to implement this tool? There can be two ways: write tool on scripting language that generates C# code file and is called before compilation. Another way is to use ability of Visual Studio to switch user custom tool for generating files. Firstly I've implemented tool by first way on Python and now I want to make a custom tool for Visual Studio written on C#. But how to plug custom tools in Visual Studio? Now I've got an answer, though I've spent much time to understand this.

Firstly, to develop plug-ins for Visual Studio it is needed to download Visual Studio SDK (for appropriate version of Visual Studio). I have to say that after installing it my Visual Studio 2008 Beta 2 became very unstable: IntelliSense regularly produces errors which crashes the whole Visual Studio.

After that Visual Studio will add templates of its addins. That's a base for future project (although may be addin can be created without all that stuff that is in template).

To make custom tool for generation of files the one must implement IVsSingleFileGenerator interface. I've spent a lot of time trying to make this and fails mainly because if this problem: in some places of documentation it is said that function generate gets a parameter of type out IntPtr[], in some it is said that it is out IntPtr and in fact interface has just IntPtr[]. Not any out! So how should I return value of pointer? Ok, I'm not a great specialist in .NET and arrays a sent be reference. Even if this is not a problem why there is an array not one pointer? Another outgoing parameter of function on exit must contain an amount of bytes in that array but how this will help if I'll allocate a different amount of bytes in different pointers in array. However I don't see a use for an array and just tried to allocate it and use only index 0. But this doesn't helped me and I failed in implementing that interface. May be that a bug in SDK and interface must have out IntPtr[]? Don't know I'm not a guru in Visual Studio.

I've failed to get a proper answer from MSDN documentation and my attempts so I've googled this problem.I've found some solutions but the most notable part is that in Visual Studio 2003 there was an abstract class which covered that horrible interface under much more comfortable interface. However "evil" Microsoft removed that class from version 2005. But good guys placed dll with it in Internet, i.e. here. So It's needed to implement function GetCode from that class and tool is ready to use!

However it is still needed to plug it in Visual Studio. First step is to register it as a package. This can be done by tool regpkg. It is placed in <%MVS SDK Dir%>\Tools\Bin\ directory. Just run it with a path to our compiled tool as a argument. This step can also be done manually,

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\<%MVSVersion%>\CLSID\{<%guid%>}]
@=<%Tool name%>
"InprocServer32"="C:\\WINDOWS\\system32\\mscoree.dll"
"ThreadingModel"="Both" "Class"=<%class name of tool%>
"Assembly"="<%asm name%>, Version=<%asm version%>, Culture=<%culture%>, PublicKeyToken=<%public key%>"


As you may noticed custom tool must be in GAC. May be there is a way to keep it somewhere else but I don't know.

What is next? Registered custom tool need to be registered as a generator. In registry dir of Visual Studio there is a directory Generators. There is some dirs in it: FA... is a C#, 16... is a VB.NET. It is needed to register tool for each language separately. In directory of proper language add directory with name of your tool, i.e. for my tool called CSGen:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\{fae04ec1-301f-11d3-bf4b-00c04f79efbc}\CSGen] @="CSGen tool to generate code on C#"
"CLSID"="{<%clsid registed in \CLSID\%>}"
"GeneratesDesignTimeSource"=dword:00000001


After that start Visual Studio choose file in project and in properties type name of custom tool. After each save file will be regenerated. It is also can be forced manually: right-click on file in Solution Explorer and "Run custom tool".

These articles with examples could be helpful (I'm not a good explainer, just learning):

No comments: