Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Coco Custom Tool for Visual Studio.NET

Rate me:
Please Sign up or sign in to vote.
4.64/5 (34 votes)
29 Oct 2005CPOL4 min read 131.5K   699   53  
Use the award winning Coco compiler's compiler directly from within Visual Studio
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Data;

namespace DotNetRegistration
{
	/// <summary>
	/// Summary description for Register.
	/// </summary>
	public class Register 
	{
		const string dllName = "vsCoco.dll";
		const string generatorName = "Coco";
		const string guid = "{D8231E80-4710-11DA-8CD6-0800200C9A66}";
		const string defaultKeyValue = "Coco generator for Visual Studio 2003";

		static int Main(string[] args)
		{
			try
			{
				return CompleteRegistration(dllName,generatorName,guid,defaultKeyValue);
			}
			catch(Exception e)
			{
				msg("An unespected error occured while registering vsCoco\n" + e.Message,true);
				return 1;
			}			
		}
	
		/// <summary>
		/// Actually performs the registration using regasm.exe and regedit.exe
		/// </summary>
		/// <param name="dllName">Name of the Dll being registered as a VS.NET Custom tool</param>
		/// <param name="generatorName">Name to use as the custom tool</param>
		/// <param name="guid">Guid of the assembly class that is the custom tool</param>
		/// <param name="defaultKeyValue">Description of the custom tool to be entered into the registry</param>
		/// <returns>0 (Zero) for successful registration</returns>
		static private int CompleteRegistration(string dllName, string generatorName,string guid, string defaultKeyValue)
		{
			//This app must be run in the same directory as the Dll to be registered. 
			//Of course, this could be changed, but I haven't seen the need!
			string full = System.Reflection.Assembly.GetExecutingAssembly().Location;
			string dir = Path.GetDirectoryName(full);

			//Set the root path for the .NET Runtime (where you can find regasm.exe)
			string dotNetRoot = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
			string currentDir = System.AppDomain.CurrentDomain.BaseDirectory;

			Process prc= new Process();
			ListViewItem item = new ListViewItem();

			//Make sure regasm.exe is where it's supposed to be!
			if(!File.Exists(dotNetRoot + "regasm.exe"))
			{

				msg("Unable to locate regasm.exe",true);
				throw new ApplicationException("Install Failed");
			}

			//Make sure the Assembly to register is present and accounted for
			if(!File.Exists(currentDir+dllName))
			{
				msg("Unable to locate "+currentDir+dllName + "\n" + "** Installation Failed **",true);
				throw new ApplicationException("Install Failed");
			}

			//Get started

			//Register the Type library for the Assembly
			prc.StartInfo.FileName = dotNetRoot + "regasm.exe";
			prc.StartInfo.Arguments = "/tlb \""+ currentDir+dllName+"\"";
			prc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
			prc.StartInfo.WorkingDirectory = dir;
			prc.Start();
			prc.WaitForExit();

			if(prc.ExitCode != 0)
			{
				msg("Failed to Register Type Library. Error code:" + prc.ExitCode.ToString() + "\n"
					+ "Installation Failed",true);
				return 1;
			}
		

			//Register the Assembly code base
			
			prc.StartInfo.FileName = dotNetRoot + "regasm.exe";
			prc.StartInfo.Arguments = "/codebase \"" +currentDir+dllName+"\"";
			prc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
			prc.StartInfo.WorkingDirectory = dir;
			prc.Start();
			prc.WaitForExit();
			
			if(prc.ExitCode != 0)
			{
				msg("Failed. Error code:" + prc.ExitCode.ToString() + "\n"
					+ "Install Failed",true);
				return 1;
			}
			
			//If we need to perform the registry entry, then do it, otherwise exit.
			prc.StartInfo.FileName = "regedit";
			prc.StartInfo.Arguments = "/s \"" +currentDir+"vsCocoRegistration.reg\"";
			prc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
			prc.StartInfo.WorkingDirectory = dir;
			prc.Start();
			prc.WaitForExit();

			if(prc.ExitCode != 0)
			{
				msg("Failed. Error code:" + prc.ExitCode.ToString() + "\n"
					+ "Install Failed",true);
				return 1;
			}

			msg("Coco/R for visual Studio.net\n\nRegistration succeeded",false);
			return 0;
		}
		
		/// <summary>
		/// Helper method to make the list view item entries
		/// </summary>
		/// <param name="verbage">Text to display</param>
		/// <param name="appendToLast">Whether or not to make a new list item or add this to the last item as a new sub-item</param>
		/// <param name="isError">Flag for an error message (makes the color Red)</param>
		static private void msg(string verbage, bool isError)
		{
			if (isError) 
				MessageBox.Show(verbage,"CocoRegistration",MessageBoxButtons.OK,MessageBoxIcon.Error);
			else 
				MessageBox.Show(verbage,"CocoRegistration",MessageBoxButtons.OK,MessageBoxIcon.Information);
		}

	
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions