Creating a SSCLI C# Template for Visual Studio

In my last post (MSBuild your SSCLI C# projects) I demonstrated how to create an MSBuild targets file for the SSCLI (Rotor).   In this post I'll show you how to make it easier to use Visual Studio to develop SSCLI C# projects.

To build successfully in VS you need to do a few things, notably:

  1. Override assembly resolution so the SSCLI assemblies are targeted (the last post shows you how to do this)
  2. Change the startup action for the project to "Start external program" (in the Debug property page for the project)
  3. Enter the path for clix.exe as the startup program
  4. Put the output binary for you application in as the "Command line arguments"

It's a little tedious to set all of this up by hand.  Thankfully it ends up it's pretty easy to make a template to do most of the dirty work.  With a small additional change to the targets file it makes it completely painless.   (Well, with the one major caveat that you can't debug managed SSCLI code with VS.)

The existing templates are in the VS folder under "Common7\IDE\ProjectTemplates".  Each of the templates are stored in a zip file- perusing a few of these in addition to looking at the documentation will get you up to speed in no time.  As such I'm not going to give a tutorial here, I'm just going to talk briefly about what I did to create my template and share it with you.

I started with the "CSharp\Windows\1033\ConsoleApplication.zip" because I wanted the equivalent of that for SSCLI.  I had to make most of the changes to the .vstemplate file and a few changes to the .csproj file to link in my targets.  Here is what my .vstemplate file looks like:

<?xml version="1.0" encoding="utf-8"?>

<VSTemplate Version="3.0.0" Type="Project" xmlns="https://schemas.microsoft.com/developer/vstemplate/2005">

       <TemplateData>

              <Name>SSCLI Console Application</Name>

              <Description>Console Application for the SSCLI (Rotor) Framework</Description>

              <Icon>SSCLIConsole.ico</Icon>

              <TemplateID>SSCLI.CSharp.ConsoleApplication</TemplateID>

              <ProjectType>CSharp</ProjectType>

              <RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>

              <MaxFrameworkVersion>2.0</MaxFrameworkVersion>

              <CreateNewFolder>true</CreateNewFolder>

              <DefaultName>SSCLIConsoleApplication</DefaultName>

              <ProvideDefaultName>true</ProvideDefaultName>

       </TemplateData>

       <TemplateContent>

              <Project File="ConsoleApplication.csproj" ReplaceParameters="true">

                     <ProjectItem ReplaceParameters="true" TargetFileName="Properties\AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>

                     <ProjectItem ReplaceParameters="true" OpenInEditor="true">Program.cs</ProjectItem>

              </Project>

       </TemplateContent>

</VSTemplate>

Not too hard to wrap your head around.  In the .csproj I changed the import to

<Import Project="$(MSBuildToolsPath)\SSCLI.CSharp.targets" />

Zipped up with a custom icon (had to try it) I dumped it in my "Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual C#" folder, created a few projects off the template to tweak it out and I was almost done.

The other bit that I've already mentioned was setting the project properties to run "clix".  Figuring this bit out isn't too terribly straightforward.  VS project settings that don't go into the .csproj go into a ".csproj.user" file that is created alongside your .csproj whenever you change the defaults.  Change the defaults to create said file and you'll quickly discover what you need to set in your .targets file to establish your own defaults.  (Note that these properties are listed in the Microsoft.Build.Commontypes.xsd, found in the MSBuild directory in your relevant framework directory.  Also note that it doesn't contain valid values for the VS properties.)

Here is what I added to SSCLI.CSharp.targets (which should live in your MSBuild directory):

<!-- VS Specific Properties -->

<PropertyGroup>

       <!-- Set these so VS runs the host exe -->

       <StartAction>Program</StartAction>

       <StartProgram>$(SSCLI_FrameworkPath)\clix.exe</StartProgram>

       <StartArguments>$(TargetFileName)</StartArguments>

       <!-- Might as well turn this on as it is currently the only kind of debugging you can do in VS -->

       <EnableUnmanagedDebugging>true</EnableUnmanagedDebugging>

</PropertyGroup>

That's pretty much it.  Now you can relatively easily create SSCLI projects.   You don't get managed debugging, as mentioned.  You do get the rest of the development enviroment benefits however (intellisense, etc.).

 The complete SSCLI.CSharp.targets file can be found here.  The template can be found here.