Welcome to MSDN Blogs Sign in | Join | Help

Code to install Community Toolbox Controls

I have been able to get some code from one of my colleagues and Extensibility support specialist, Ed Dore, that may help with the Toolbox control installation in Visual Studio 2005. I must remind you that the code that I post here comes with a Disclaimer.

In the code below you need to replace the following with your values:

<Path to the control dll>: Path to the dll that contains the toolbox control

<Toolbox Tabname>: Name of the Toolbox tab on which to add the controls.

HTH,

Chetan

 

using System;

using
System.Collections.Generic;

using
System.Text;

using
System.Runtime.InteropServices;

using
EnvDTE;

using
EnvDTE80;

using
System.IO;

namespace
InstallToolboxControls
{
   

// Definition of the IMessageFilter interface which we need to implement and 

   
// register with the CoRegisterMessageFilter API.

    [ComImport(), Guid("00000016-0000-0000-C000-000000000046"
),
    
InterfaceTypeAttribute(ComInterfaceType
.InterfaceIsIUnknown)]
    
interface IOleMessageFilter
// Renamed to avoid confusion w/ System.Windows.Forms.IMessageFilter

    {
        [
PreserveSig
]
       
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr
lpInterfaceInfo);
        [
PreserveSig
]
        
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int
dwRejectType);
        [
PreserveSig
]
        
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int
dwPendingType);
    }
 



    class Program :
IOleMessageFilter

    {
        [
DllImport("ole32.dll"
)]
        
private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter
oldFilter);
       
 static string ctrlPath = <Path to the control dll>;
        [
STAThread
]
        
static void Main(string
[] args)
        {
            

Program program = new Program
();
            program.Register();
            

if (args[0].Equals("-Install", StringComparison
.CurrentCultureIgnoreCase))
                program.InstallControl();
            

else if (args[0].Equals("-UnInstall", StringComparison
.CurrentCultureIgnoreCase))
                program.UninstallControl();
            program.Revoke();
            


// to ensure the dte object is actually released, and the devenv.exe process terminates.

            GC
.Collect();
            
GC
.WaitForPendingFinalizers();
        }
        

void
InstallControl()
        {
            

// Create an instance of the VS IDE,

            Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0"
);
            
DTE dte = (DTE)System.Activator.CreateInstance(type, true
);
 
            

// create a temporary winform project;

            string tmpFile = Path.GetFileNameWithoutExtension(Path
.GetTempFileName());
            
string tmpDir = string.Format("{0}{1}",Path
.GetTempPath(),tmpFile);
            
Solution2 solution = dte.Solution as Solution2
;
            
string templatePath = solution.GetProjectTemplate("WindowsApplication.zip", "CSharp"
);
            
Project proj = solution.AddFromTemplate(templatePath, tmpDir, "dummyproj", false
);
            
// add the control to the toolbox.

            EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants
.vsWindowKindToolbox);
            EnvDTE.
ToolBox toolbox = (EnvDTE.ToolBox
)window.Object;
            EnvDTE.
ToolBoxTab
myTab = toolbox.ToolBoxTabs.Add(<Toolbox TabName>);
            myTab.Activate();
            myTab.ToolBoxItems.Add(

"MyUserControl", ctrlPath, vsToolBoxItemFormat
.vsToolBoxItemFormatDotNETComponent);
            dte.Solution.Close(
false
);
            
Marshal
.ReleaseComObject(dte);
            
Console.WriteLine("Control Installed!!!"
);
        }


        void
UninstallControl()
        {
            

Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0"
);
            
DTE dte = (DTE)System.Activator.CreateInstance(type, true
);
 
            EnvDTE.

Window window = dte.Windows.Item(EnvDTE.Constants
.vsWindowKindToolbox);
            EnvDTE.
ToolBox toolbox = (EnvDTE.ToolBox
)window.Object;
            EnvDTE.
ToolBoxTab
myTab = toolbox.ToolBoxTabs.Item(<Toolbox Tabname>);
            myTab.Activate();
            myTab.Delete();
            


Marshal
.ReleaseComObject(dte);
            
Console.WriteLine("Control Uninstalled!!!"
);
        }
       

void
Register()
        {
           

IOleMessageFilter
oldFilter;
            CoRegisterMessageFilter(
this, out
oldFilter);
        }
       

void
Revoke()
        {
            

IOleMessageFilter
oldFilter;
            CoRegisterMessageFilter(
null, out
oldFilter);
        }


        #region
IOleMessageFilter Members
        
public int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr
lpInterfaceInfo)
        {
            

return 0;
//SERVERCALL_ISHANDLED

        }
        
public int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int
dwRejectType)
        {
            

if (dwRejectType == 2)
// SERVERCALL_RETRYLATER

            return 200;
// wait 2 seconds and try again

            return -1;
// cancel call

        }

        public int MessagePending(IntPtr hTaskCallee, int dwTickCount, int
dwPendingType)
        {
            

return 2;
//PENDINGMSG_WAITDEFPROCESS

        }

        #endregion

    }
}


 

Published Thursday, January 19, 2006 8:07 PM by ChetanC
Filed under:

Comments

Wednesday, March 01, 2006 10:18 AM by Atanas Korchev

# Adding controls to Visual Studio Toolbox


The Problem
As Rumen said, adding controls to Visual Studio toolbox&amp;nbsp;can be a tedious procedure....
Wednesday, June 07, 2006 10:35 AM by JocularJoe

# re: Code to install Community Toolbox Controls

I tried this and it seems to work, but it looks like black magic.  
Can you point to any resources describing the reasons for the CoRegisterMessageFilter and the creation of a dummy project?
None of that was necessary in VS.NET 2003.
Thursday, June 08, 2006 10:03 AM by JocularJoe

# re: Code to install Community Toolbox Controls

I have adapted the above code with some changes to make it more robust into a couple of classes which can be called from an Installer run from a Custom Action in a Setup project.
One problem I hit is that custom actions in an MSI seem to be run on an MTA thread - and CoRegisterMessageFilter only works on an STA thread.  The fix was to spawn an STA worker thread.  I can supply the source if anyone's interested.
Friday, June 09, 2006 10:54 AM by chimera

# re: Code to install Community Toolbox Controls

This doesn't seem to work with custom web control dll's? Is that correct?

If so, do you know how to install them in the toolkit?
Thursday, July 06, 2006 3:50 PM by BourqueJ

# re: Code to install Community Toolbox Controls

Thanks, that worked great! One question tough, I am having trouble getting the code to work with the Express versions of Visual Studio, any idea?

Thanks.
Tuesday, September 12, 2006 6:05 PM by esala;

# Can i choose tab name when adding from visual studio

Hello,

I'm working with c# .net 2

I'm a part of a gui team, we create and update controls that are used in our projects.

since we update them frequently, we don't want to create deploy each time.

My question is, can i determine the default tab to add my controls when adding them using right mouse button --> add... on the toolbox?

Thanks,
Eran.
Sunday, February 11, 2007 11:17 PM by benoit.drapeau

# re: Code to install Community Toolbox Controls

You made my day. Your code work like a charm, at least with my configuration (Windows XP Professionnal, Visual Studio 2005 Professional with SP1) and adding custom web controls.

Thanks a lot

Benoit

Thursday, March 29, 2007 3:14 AM by karthika

# re: Code to install Community Toolbox Controls

Hello Joe,

I am graduate student trying to create an installer project to install the custom controls and was wondering if you could share some ideas on how you created one. Also, could someone tell me why we need to create a temporary winform project?

Thanks for your help!

Kate

>Thursday, June 08, 2006 10:03 AM by >JocularJoe

># re: Code to install Community Toolbox >Controls

>I have adapted the above code with some >changes to make it more robust into a couple >of classes which can be called from an >Installer run from a Custom Action in a Setup >project.

>One problem I hit is that custom actions in >an MSI seem to be run on an MTA thread - and >CoRegisterMessageFilter only works on an STA >thread.  The fix was to spawn an STA worker >thread.  I can supply the source if anyone's >interested.

Tuesday, November 27, 2007 2:42 AM by sdadept

# re: Code to install Community Toolbox Controls

karthika, I'd like to see that.  I'm having problems calling a program created with this from an msi install custom action.  Email me at sd_adept at the address of hotmail.com.  Or if you could post a link to your work.

Thursday, January 22, 2009 12:34 AM by Compiling projects from your app? | keyongtech

# Compiling projects from your app? | keyongtech

# Chetan Chudasama s Weblog Code to install Community Toolbox Controls | debt consolidator

Anonymous comments are disabled
 
Page view tracker