Thursday, February 22, 2007

Extending visual studio using VS addins

hi,
visual studio 2003 can be extended by using addins. By saying extending visual studio, i mean adding some new features (like customized Tool bar, menus etc having some fucntionality) to the visual studio development environment.
addins can also be used to extend many of the MS office products like MS word, Ms outlook etc.
i have used addins to extend Visual studio, MS word and also MS outlook.
Addins can be developed in any language - vb.net or C#.
Addins have to be registered, once they are registered, they can be launched by using the addin manager(we will discuss all these in detail in the later sections).

let me discuss how to develop a sample addin for visual studio 2003. This addin, will add a menu called "SmartCoder ".This menu will have a menu item called "Insert try catch" which will insert try catch block into the code window.

launch Visual studio 2003 and select file->new project, a window(as in figure below) opens up
expand "other projects" node and select "extensibility projects" in the project types.
on the right side we get the templates, since we are developing a sample addin for Visual studio,we have selected VS.net addin template. But if the addin is for other MS office products like word, excel, outlook etc, we have to select "shared addin".let me name my addin as "MyAddin1".


the addin solution will get created, this solution will have two projects - MyAddin1 (the actual addin) and myAddin1Setup (the installer for the addin).
MyAddin1 project will have a class called "Connect.cs". This is how the class will appear

namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
using System.Windows.Forms;



[GuidAttribute("259C3898-1FF7-4323-BECD-2AD5DD02103E"), ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2, IDTCommandTarget
{

public Connect()
{
}


public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;

}


public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
}

public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{

}


public void OnBeginShutdown(ref System.Array custom)
{
}

public void QueryStatus(string commandName, EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
{
if(neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
}
}
}

public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
handled = true;
return;
}
}
}
private _DTE applicationObject;
private AddIn addInInstance;

}



the above given code is the most basic one. this is got when a new addin is created.
but addins are created for the purpose of extending visual studio,so the next question is how/where are we extending VS? well to be honest we have still not extended VS, but we will now. we will just add a commandbar and then add some buttons to that command bar.


to do so, we will have to add some code to OnStartupComplete, so that after modification,the function looks like this


public void OnStartupComplete(ref Array custom)
{
CommandBar cmd = (CommandBar)((CommandBars)_applicationObject.CommandBars).Add("test",System.Reflection.Missing.Value,
System.Reflection.Missing.Value,System.Reflection.Missing.Value);
cmd.Name = "Test";


CommandBarButton BtnCtrl = (CommandBarButton) cmd.Controls.Add(MsoControlType.msoControlButton, "addin", "Button1", System.Reflection.Missing.Value,System.Reflection.Missing.Value);
BtnCtrl.Caption = "Test";

//adding event handler

BtnCtrl.Click+=new _CommandBarButtonEvents_ClickEventHandler(BtnCtrl_Click);



}

commandbars.add is function which expect the following parameters
object name, object position, object menubar, object temporary

name is very much need as for the rest they can have reflection.missing.value.

then we can add a control to the new commandbar which we have created.

commandbar.controls.add is a function which expect the following parameters

object type,object id,object parameter, object before, object temporary

"type" will be specified by the enum MSOControlType.
"id" and "parameter" are used to uniquely identify the control.
if two controls have the same parameter, then their associated eventhandlers may be interchanged at runtime,. this can lead to unexpected behaviour.so its advisable to have parameter unique for each control.

"before" is the control which is before this control in command bar.This is useful when arranging controls in the bar in an order.

"temporary" can have a value of system.reflection.missing.value.

adding eventhandler for these controls is very much same as the normal event handler adding mechanism.

in dot net 2003, there will be an installer along with the addin.
so just run the addin using the F5 and addin installs itself and runs a new instance of VS.
open some project in VS and then click on view->Toolbars.
in the list of toolbars shown, the new toolbar will be present in teh last with the name of "test".
click on it and then the commandbar opens up.
lick on the button and it pops up a message "button clicked".


this is just the basic commandbar example.
we can do the same for menu and menubars too.

addins are not meant only for visual Studio IDE, they can be used to extend word, powerpoint, access, outlook, excel, frontpage, visio etc.

so happy coding .

any discussions with me on any technical topics in dot net are welcome.
you can mail me at nandank81@gmail.com

Tuesday, February 20, 2007

saurav ganguly - the man,his attitude and his comeback

some call him "dada", some call him "Bengal tiger".
watching him bat in full flow is like listening to some soothing music. he has the balance of a ballet dancer. But what intrigues every cricket enthusiast is ganguly's attitude about the game.
The whole of cricketing sodality except for a few here and there felt that his career was over and out. Many felt that his time had come and he should bid adieu to the game of cricket, that he was no longer in the game, that he was not a younger chap who could dive while fielding to curb the runs.
yes, his form was very bad so was his luck. he used to get out in the most bizarre fashion.
he could have said that his days in the cricket field was over,but instead he choose to work hard on his weakness and get back. He did make a comeback and what a comeback it has been. there have been exuberant amount of runs flowing from that blade of his. his batting has been nothing short of a stupendous display of aggression and stroke play. His trademark Terpsichore down the track is mind blowing. His prodigious talent to read the length of the ball immediately after it is discharged is really mind boggling. His bowling too has been handy at times.
but most important is his "never say die" attitude which can be compared with that of the great Australian captain steve waugh's attitude.

Hope the tiger roars in this world cup and provides an overwhelming entertainment to his fans.

VSIP - package load failure

hi,
some times VSIP 8.0(VS SDK 2005) throws a package load failure even if the PLK got is proper.
the reason may be that there is a DLL which will be loaded by VSIP (may be in VSPKG.cpp class).
is that dll is not loadable , then Visual Studio throws this error.

solution : run regasm.exe on the dll (which has to be loaded by VSIP).

hope this helps people who want to develop new extensible stuff to VS 2005.