public virtual VSADDRESULT RunWizard(HierarchyNode parentNode, string itemName, string wizardToRun, IntPtr dlgOwner) { Debug.Assert(!String.IsNullOrEmpty(itemName), "The Add item dialog was passing in a null or empty item to be added to the hierrachy."); Debug.Assert(!String.IsNullOrEmpty(this.ProjectFolder), "The Project Folder is not specified for this project."); // We just validate for length, since we assume other validation has been performed by the dlgOwner. if (this.ProjectFolder.Length + itemName.Length + 1 > NativeMethods.MAX_PATH) { string errorMessage = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.PathTooLong, CultureInfo.CurrentUICulture), itemName); if (!Utilities.IsInAutomationFunction(this.Site)) { string title = null; OLEMSGICON icon = OLEMSGICON.OLEMSGICON_CRITICAL; OLEMSGBUTTON buttons = OLEMSGBUTTON.OLEMSGBUTTON_OK; OLEMSGDEFBUTTON defaultButton = OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST; VsShellUtilities.ShowMessageBox(this.Site, title, errorMessage, icon, buttons, defaultButton); return VSADDRESULT.ADDRESULT_Failure; } else { throw new InvalidOperationException(errorMessage); } } // Build up the ContextParams safearray // [0] = Wizard type guid (bstr) // [1] = Project name (bstr) // [2] = ProjectItems collection (bstr) // [3] = Local Directory (bstr) // [4] = Filename the user typed (bstr) // [5] = Product install Directory (bstr) // [6] = Run silent (bool) object[] contextParams = new object[7]; contextParams[0] = EnvDTE.Constants.vsWizardAddItem; contextParams[1] = this.Caption; object automationObject = parentNode.GetAutomationObject(); if (automationObject is EnvDTE.Project) { EnvDTE.Project project = (EnvDTE.Project)automationObject; contextParams[2] = project.ProjectItems; } else { // This would normally be a folder unless it is an item with subitems ProjectItem item = (ProjectItem)automationObject; contextParams[2] = item.ProjectItems; } contextParams[3] = this.ProjectFolder; contextParams[4] = itemName; object objInstallationDir = null; IVsShell shell = (IVsShell)this.GetService(typeof(IVsShell)); ErrorHandler.ThrowOnFailure(shell.GetProperty((int)__VSSPROPID.VSSPROPID_InstallDirectory, out objInstallationDir)); string installDir = (string)objInstallationDir; // append a '\' to the install dir to mimic what the shell does (though it doesn't add one to destination dir) if (!installDir.EndsWith("\\", StringComparison.Ordinal)) { installDir += "\\"; } contextParams[5] = installDir; contextParams[6] = true; IVsExtensibility3 ivsExtensibility = this.GetService(typeof(IVsExtensibility)) as IVsExtensibility3; Debug.Assert(ivsExtensibility != null, "Failed to get IVsExtensibility3 service"); if (ivsExtensibility == null) { return VSADDRESULT.ADDRESULT_Failure; } // Determine if we have the trust to run this wizard. IVsDetermineWizardTrust wizardTrust = this.GetService(typeof(SVsDetermineWizardTrust)) as IVsDetermineWizardTrust; if (wizardTrust != null) { Guid guidProjectAdding = Guid.Empty; object guidProjectAddingAsObject = this.GetProperty((int)__VSHPROPID2.VSHPROPID_AddItemTemplatesGuid); wizardTrust.OnWizardInitiated(wizardToRun, ref guidProjectAdding); } int wizResultAsInt; try { Array contextParamsAsArray = contextParams; int result = ivsExtensibility.RunWizardFile(wizardToRun, (int)dlgOwner, ref contextParamsAsArray, out wizResultAsInt); if (!ErrorHandler.Succeeded(result) && result != VSConstants.OLE_E_PROMPTSAVECANCELLED) { ErrorHandler.ThrowOnFailure(result); } } finally { if (wizardTrust != null) { wizardTrust.OnWizardCompleted(); } } EnvDTE.wizardResult wizardResult = (EnvDTE.wizardResult)wizResultAsInt; switch (wizardResult) { default: return VSADDRESULT.ADDRESULT_Cancel; case wizardResult.wizardResultSuccess: return VSADDRESULT.ADDRESULT_Success; case wizardResult.wizardResultFailure: return VSADDRESULT.ADDRESULT_Failure; } }
private void Fire(HierarchyNode node, Action<IVsExtensibility3, EnvDTE.ProjectItem> fireForProjectItem) { if (!myCanFire) return; if (!myProjectNode.IsProjectOpened) return; // We do not fire for references, aligning with C#. // Those interested in references have to listen to our VSProject.Events.ReferencesEvent if (node is ReferenceNode) return; // SVsExtensibility isn't exposed to managed code, but it's the same as EnvDTE.IVsExtensibility var ext = myProjectNode.GetService(typeof(IVsExtensibility)) as IVsExtensibility3; if (ext != null) { object automationObject = node.GetAutomationObject(); var projectItem = automationObject as EnvDTE.ProjectItem; if (projectItem != null) fireForProjectItem(ext, projectItem); } }
/// <summary> /// Executes a wizard. /// </summary> /// <param name="parentNode">The node to which the wizard should add item(s).</param> /// <param name="itemName">The name of the file that the user typed in.</param> /// <param name="wizardToRun">The name of the wizard to run.</param> /// <param name="dlgOwner">The owner of the dialog box.</param> /// <returns>A VSADDRESULT enum value describing success or failure.</returns> public virtual VSADDRESULT RunWizard(HierarchyNode parentNode, string itemName, string wizardToRun, IntPtr dlgOwner) { Debug.Assert(!String.IsNullOrEmpty(itemName), "The Add item dialog was passing in a null or empty item to be added to the hierrachy."); Debug.Assert(!String.IsNullOrEmpty(this.ProjectFolder), "The Project Folder is not specified for this project."); // We just validate for length, since we assume other validation has been performed by the dlgOwner. if (this.ProjectFolder.Length + itemName.Length + 1 > NativeMethods.MAX_PATH) { string errorMessage = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.PathTooLong), itemName); if (!Utilities.IsInAutomationFunction(this.Site)) { string title = null; OLEMSGICON icon = OLEMSGICON.OLEMSGICON_CRITICAL; OLEMSGBUTTON buttons = OLEMSGBUTTON.OLEMSGBUTTON_OK; OLEMSGDEFBUTTON defaultButton = OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST; VsShellUtilities.ShowMessageBox(this.Site, title, errorMessage, icon, buttons, defaultButton); return VSADDRESULT.ADDRESULT_Failure; } else { throw new InvalidOperationException(errorMessage); } } // Build up the ContextParams safearray // [0] = Wizard type guid (bstr) // [1] = Project name (bstr) // [2] = ProjectItems collection (bstr) // [3] = Local Directory (bstr) // [4] = Filename the user typed (bstr) // [5] = Product install Directory (bstr) // [6] = Run silent (bool) object[] vContextParams = new object[7]; vContextParams[0] = EnvDTE.Constants.vsWizardAddItem; vContextParams[1] = this.Caption; object automationObject = parentNode.GetAutomationObject(); if (automationObject is EnvDTE.Project) { EnvDTE.Project project = (EnvDTE.Project)automationObject; vContextParams[2] = project.ProjectItems; } else { // This would normally be a folder unless it is an item with subitems ProjectItem item = (ProjectItem)automationObject; vContextParams[2] = item.ProjectItems; } vContextParams[3] = this.ProjectFolder; vContextParams[4] = itemName; object objInstallationDir = null; IVsShell shell = (IVsShell)this.GetService(typeof(IVsShell)); ErrorHandler.ThrowOnFailure(shell.GetProperty((int)__VSSPROPID.VSSPROPID_InstallDirectory, out objInstallationDir)); string installDir = (string)objInstallationDir; // append a '\' to the install dir to mimic what the shell does (though it doesn't add one to destination dir) if (!installDir.EndsWith("\\")) { installDir += "\\"; } vContextParams[5] = installDir; vContextParams[6] = true; EnvDTE.IVsExtensibility ivsExtensibility = (EnvDTE.IVsExtensibility)this.GetService(typeof(EnvDTE.IVsExtensibility)); Debug.Assert(ivsExtensibility != null, "Failed to get EnvDTE.IVsExtensibility service"); if (ivsExtensibility == null) { return VSADDRESULT.ADDRESULT_Failure; } EnvDTE.wizardResult wizResult = ivsExtensibility.RunWizardFile(wizardToRun, (int)dlgOwner, ref vContextParams); return (VSADDRESULT)wizResult; }