using System; using Extensibility; using EnvDTE; using EnvDTE80; using System.Resources; using System.Reflection; using System.Globalization; using Microsoft.VisualStudio.CommandBars; using Microsoft.VisualStudio.Shell.Interop; namespace TempVSIP { /// The object for implementing an Add-in. /// public class Connect : IDTExtensibility2, IDTCommandTarget { /// Implements the constructor for the Add-in object. Place your initialization code within this method. public Connect() { } /// Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded. /// Root object of the host application. /// Describes how the Add-in is being loaded. /// Object representing this Add-in. /// public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; if(connectMode == ext_ConnectMode.ext_cm_UISetup) { object []contextGUIDS = new object[] { }; Commands2 commands = (Commands2)_applicationObject.Commands; string toolsMenuName; try { //If you would like to move the command to a different menu, change the word "Tools" to the // English version of the menu. This code will take the culture, append on the name of the menu // then add the command to that menu. You can find a list of all the top-level menus in the file // CommandBar.resx. string resourceName; ResourceManager resourceManager = new ResourceManager("TempVSIP.CommandBar", Assembly.GetExecutingAssembly()); CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID); if(cultureInfo.TwoLetterISOLanguageName == "zh") { System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent; resourceName = String.Concat(parentCultureInfo.Name, "Tools"); } else { resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"); } toolsMenuName = resourceManager.GetString(resourceName); } catch { //We tried to find a localized version of the word Tools, but one was not found. // Default to the en-US word, which may work for the current culture. toolsMenuName = "Tools"; } //Place the command on the tools menu. //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items: CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"]; //Find the Tools command bar on the MenuBar command bar: CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName]; CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl; //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in, // just make sure you also update the QueryStatus/Exec method to include the new command names. try { //Add a command to the Commands collection: Command command = commands.AddNamedCommand2(_addInInstance, "TempVSIP", "TempVSIP", "Executes the command for TempVSIP", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); //Add a control for the command to the tools menu: if((command != null) && (toolsPopup != null)) { command.AddControl(toolsPopup.CommandBar, 1); } } catch(System.ArgumentException) { //If we are here, then the exception is probably because a command with that name // already exists. If so there is no need to recreate the command and we can // safely ignore the exception. } } } /// Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded. /// Describes how the Add-in is being unloaded. /// Array of parameters that are host application specific. /// public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { } /// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed. /// Array of parameters that are host application specific. /// public void OnAddInsUpdate(ref Array custom) { } /// Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading. /// Array of parameters that are host application specific. /// public void OnStartupComplete(ref Array custom) { } /// Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded. /// Array of parameters that are host application specific. /// public void OnBeginShutdown(ref Array custom) { } /// Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command's availability is updated /// The name of the command to determine state for. /// Text that is needed for the command. /// The state of the command in the user interface. /// Text requested by the neededText parameter. /// public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText) { if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) { if(commandName == "TempVSIP.Connect.TempVSIP") { status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled; return; } } } /// Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked. /// The name of the command to execute. /// Describes how the command should be run. /// Parameters passed from the caller to the command handler. /// Parameters passed from the command handler to the caller. /// Informs the caller if the command was handled or not. /// public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { handled = false; if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { if(commandName == "TempVSIP.Connect.TempVSIP") { handled = true; LaunchProject(); return; } } } private void LaunchProject() { Microsoft.VisualStudio.Shell.ServiceProvider sp = new Microsoft.VisualStudio.Shell.ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_applicationObject); IVsDebugger dbg = (IVsDebugger)sp.GetService(typeof(SVsShellDebugger)); VsDebugTargetInfo info = new VsDebugTargetInfo(); info.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(info); info.dlo = Microsoft.VisualStudio.Shell.Interop.DEBUG_LAUNCH_OPERATION.DLO_CreateProcess; info.bstrExe = @"e:\CosmosProj\Project2\bin\Debug\CosmosKernel.iso"; info.bstrCurDir = System.IO.Path.GetDirectoryName(info.bstrExe); info.bstrArg = null; // no command line parameters info.bstrRemoteMachine = null; // debug locally info.fSendStdoutToOutputWindow = 0; // Let stdout stay with the application. info.clsidCustom = new Guid("{FA1DA3A6-66FF-4c65-B077-E65F7164EF83}"); // Set the launching engine the sample engine guid info.grfLaunch = 0; IntPtr pInfo = System.Runtime.InteropServices.Marshal.AllocCoTaskMem((int)info.cbSize); System.Runtime.InteropServices.Marshal.StructureToPtr(info, pInfo, false); try { dbg.LaunchDebugTargets(1, pInfo); } finally { if (pInfo != IntPtr.Zero) { System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pInfo); } } } private DTE2 _applicationObject; private AddIn _addInInstance; } }