![]() |
General Programming »
Macros and Add-ins »
General
Advanced
License: The Code Project Open License (CPOL)
How-to create a custom project template using Visual C++ .NETBy Hugo PEREIRAA tutorial on writing your own project template for Visual C++ .NET. |
C++/CLI, C, Javascript, VC7.1, VC8.0, .NET, Win2K, WinXP, Win2003, Vista, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

Many programmers and students create small projects. These projects are often based on the same project template. In many cases, they need to modify the project settings, and every time they must do that, they lose time on repetitive configuration tasks. For these reasons, I have decided to create this article; it explains Visual Studio .NET template creation, and it can be a good example for creating more complex wizards.
This information is not exhaustive, it explains the most common features of a custom wizard. A typical project wizard template contains the files listed below:
Identifies the wizard engine and provides context and optional custom parameters.
This file provides a routing service between the Visual Studio shell and the items in the wizard project.
Templates.inf is a text file that contains a list of templates to use for the project. Note that Template.inf is a template file itself (you can use directives to indicate which files to include in the project).
The XML file that contains the project type information.
This folder contains files that will be included in the project. Note that the location on the hard drive is "$(ProjectDir)\Template Files\1033".
Contains the HTML file (user interface) used by the wizard, one file per page wizard. The first file is "Default.htm". The location on the hard drive is "$(ProjectDir)\Html\1033".
The Custom Wizard creates a JScript file called "Default.js" for each project. It also includes "Common.js". These files contain JScript functions that give you access to the Visual C++ object models to customize a wizard.
Wait a moment, be patient. Before starting the template creation, you must create a default project. This project must contain your pre-requisites for a default project template. Write down on a text file everything that you do, this information will be useful when the wizard creates the new project.
For this article, I have chosen to create a Win32 Console project for creating a C project. My requisites are:
typedef to create a bool type (optional).argc and argv appear in the main function declaration.To create the template structure is pretty simple; Visual Studio does it for you. First, start Visual Studio .NET and select "New Project" from the "File" menu.

On the project dialog box, choose "Visual C++ Project" and "Custom Templates".
On the Application Setting page of the "Custom Wizard", change the wizard friendly name to "C Console Project", check the "User interface" checkbox, and set the number of wizard page to 2. The base is now created.
Now, we can add the project files. In my example, I only add one file called "main.c".
To add this file, create a file called "main.c" and save it to the "$(ProjectDir)\Templates\1033" folder. After that, right click on the "Template Files" directory on Visual Studio and select "Add existing item", then add the "main.c" file.
Delete the "ReadMe.txt" and the "Sample.txt" files from the "Template Files" directory (from Visual Studio and from the hard drive).
Now, we modify the "template.inf" file to represent the last three modifications. For that, replace all the file content by "main.c".
In this step, I only explain three modifications. All others are technically the same. These modifications are:
On every project, you can find a "default.js" file. This file contains some functions that were called when the output project was created. On the default HTML file, there is a textbox "MAIN_FILE_NAME" that contains the file name; by default, it is "main". To allow Visual Studio to change the name of this file, you must modify the function "GetTargetName" as follows (the function is located on the default.js file).
function GetTargetName(strName, strProjectName)
{
try
{
var strTarget = strName;
if (strName == 'main.c')
strTarget = wizard.FindSymbol('MAIN_FILE_NAME') + '.c';
return strTarget;
}
catch(e)
{
throw e;
}
}
You can define the default value for the HTML controls by adding a "SYMBOL" tag on the HTML file. For example, to set the default value of the "MAIN_FILE_NAME" control to main, add the following line on the HEAD section.
<symbol name="MAIN_FILE_NAME" type="text" value="main"></symbol>
For example, to add the "stdio.h" file into the "main.c" file, add a checkbox control on the "Default.htm" file:
<input id="INCLUDE_STDIO_H" type="checkbox" value="checkbox" name="checkbox">
After that, edit the "main.c" file and modify it according to the example below:
[!if INCLUDE_STDIO_H]
#include <stdio.h>
[!endif]
To view more possibilities, edit the file in the sample.
To modify the project settings, it's a little more complicated. The function that does that is on the "default.js" file, its name is AddConfig().
When you create a project with the custom wizard, the generated AddConfig() function does not contain much information, but it contains the object declaration which we will use to change the project settings.
In my project, I need to change the following settings (config=debug). Below is a table with the settings to change and a sample code to make this change:
Character Set:Use Multi-Byte Character Set
Debug Information Format:Program Database for Edit & Continue (/ZI)
Warning Level:Level 3 (/W3)
Optimization:Disabled (/Od)
Preprocessor Definitions:WIN32;_DEBUG;_CONSOLE
Runtime Library:Single-threaded Debug (/MLd)
Create/Use Precompiled Headers:Not Using Precompiled Headers
Enable Incremental Linking:Yes (/INCREMENTAL)
Generate Debug Info:Yes (/DEBUG)
The code:
config.CharacterSet = charSetMBCS;
CLTool.DebugInformationFormat = debugOption.debugEditAndContinue;
CLTool.WarningLevel = warningLevelOption.warningLevel_3;
CLTool.Optimization = optimizeOption.optimizeDisabled;
CLTool.PreprocessorDefinitions = "WIN32;_DEBUG";_CONSOLE";
CLTool.RuntimeLibrary = rtSingleThreadedDebug;
CLTool.UsePrecompiledHeader = pchNone;
LinkTool.GenerateDebugInformation = true;
LinkTool.LinkIncremental = linkIncrementalYes;
The JavaScript:
var config = proj.Object.Configurations("Debug|Win32");
var CLTool = config.Tools("VCCLCompilerTool");
var LinkTool = config.Tools("VCLinkerTool");
For more information, see the AddConfig() function in the "Default.js" file.
In the setup file, you will find scripts allowing to install this example automatically. If you want to change these scripts for your personal use, you will find the parameters for the installation in the file "config.vbs".
Extract the contents of "VCNetCustomWizard_demo.zip" and use the cmd files (install.cmd, uninstall.cmd, and settings.cmd).
For Visual C++ 2005 or 2005 Express, replace the string "$$WIZARD_VERSION$$" with "8.0", and go to the "config.vbs" file to view the installation paths.
Visual C++ .NET and Visual Studio .NET offer some features to customize your project templates; custom projects can help single developers or developer teams to accelerate their development by automating repetitive tasks. It can also provide a good foundation for creating personal or enterprise standards. Some other Visual Studio features like add-ins or macros can help developers to organize their work. I encourage developers to look at these different possibilities to eliminate the non- interesting things and concentrate on more significant tasks.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 Apr 2006 Editor: Smitha Vijayan |
Copyright 2006 by Hugo PEREIRA Everything else Copyright © CodeProject, 1999-2009 Web12 | Advertise on the Code Project |