Friday, January 15, 2010

Creating Project Template in Visual Studio

Visual studio provides us with option to create Project template and show them in the New Project Template Dialog Box.In addition we can define our own logic on opening these templates with help of wizard interface ( IDTWizard ).
In this post i will be showing steps to make it happen.

Staring point for creating our project template is VSDIR and VSZ files .VSDIR is a text file that tells the New Project Dialog box how to display our project template , its display name,order in dialog box and the icon associated with it.VSZ files define the wizard class to invoke for logic and parameters to provide to the wizard class.Relative path of these vsz files are defined in the VSDIR file along with other parameters.
Lets look at the conent of VSDIR file:-


Template.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|My Template|70|Template CS|D:\Helper Projects\Helper Projects\SatelliteAssembly\IconResourceDll.dll|102||


All the parameters are seperated by pipe (|).

  • First parameter define the vsz file , in this case it is in the same folder as the VSDIR file.

  • Next is the product GUID (vc++,c# etc) containing localized resources for the product.

  • Display name of the template as it will be shown in New Project Dialog.

  • Sort priority , 1 being the highest.(Template be shown next to all the template with same priority)

  • Discription of the Template.

  • Path of the associated Icon resource dll.

  • Icon resource Id.

  • Flags

  • Suggested Base name.
We have to create satellite assembly for icon resources and our project template that will open on New Project item in invocation.

Lets see VSZ file.


VSWIZARD 7.0
Wizard={BAA02C12-00F5-4e74-A6AF-AEC6930AC067}
Param="TemplateCS"

First parameter in VSZ file defines the Visual Studio Enviroment template version, next is the guid of the wizard class containg the logic for opening the template, then the parameters to pass to the wizard class.

Now after creating these files we have to put them in specific location , the path is

[WinDrive]:\Program Files\Microsoft Visual Studio 10.0\VC#\CSharpProjects

OR

[WinDrive]::\Program Files\Microsoft Visual Studio 10.0\VB\CSharpProjects

Now if we check by opening the visual studio and opening the new project dialog box , we will find our template there. Next step is to create the wizard class that will handle our project opening logic , remember we have put the guid of this class in the VSZ file. Creating the wizard class start's with adding reference to the Envdte from Envdte.dll that contains the IDTWizard interface .Create a new class library project and define your wizard class.Now we add envdte namespace to our class and implement the IDTWizard .IDTWizard is the interface that makes our class a wizard.There is single method to implement Execute, this is the method that is called when our template is invoked from the New Project Dialog box .

[ComVisible(true)]
[Guid("BAA02C12-00F5-4e74-A6AF-AEC6930AC067")]
public class TemplateWizard : IDTWizard
{

[ComVisible(true)]
public void Execute(object Application, int hwndOwner, ref object[] ContextParams, ref object[] CustomParams, ref wizardResult retval)
{

}

}

We have marked our class ComVisible so that com clients can access our managed class , we have also defined the guid attribute with a guid that we have referenced in the VSZ file.This guid is the way how the VS know which class to call when the template is selected.

Execute method have parameters that define :-

  • The Visual Studio Automation Model object .(DTE)

  • Parent handle of the wizard class.

  • Context parameters array .

  • Custom parameters array.The array of the parameters that we have defined in the vsz file.

We can use the EnvDte.Dte object passed on as parameter to open our project (.csproj / .vbproj)in VS . Method provided by the DTE is AddFromTemplate that takes on parameters

DTE.Solution.AddFromTemplate( param1 , param1,param1 ,param1 )

  • Filename and full path of the template file with extension .(vstemplate file)

  • Destination path where to create this project.

  • Project Name

  • Bool describing whether to open the project in current or new solution.

We can also define logic to find out that if the project already happens to be at destination path and open that project instead of creating new one.

DTE.Solution.AddFromFile(openProjectPath, true);

Now before adding our project to the VS we can define whole set of logic to happen.Most comman of all is altering our class files in project that we are about to open. We can define namespace , class names and all other kind of custom code depending on certain criteria prior to opening the project in VS . We can do a lot of things based on our project need , like call some service, open a dilog box (WPF), add references to project , load controls to the toolbox in VS etc.

After we have created our wizard class , we have to register the assembly so that VS could locate our class when our template is invoked from the New Project Dialog. The way to do this is through Regasm utility with codebase option.

:> Regasm /codebase [path of our wizard dll]

After our type is registered we are all set to create new project from our project template.

No comments:

Post a Comment