繁体   English   中英

如何使用Visual Studio 2017注册“自定义工具”以使其工作?

[英]How to register “custom tool” with Visual Studio 2017 to make it work?

背景:我们有一个自定义工具 ,它接受xml输入并生成cs输出。 自定义工具需要向 Visual Studio 注册才能使其与该版本的visual studio一起使用。

我们做了什么:我们已经使用Visual Studio 2015完成了自定义工具注册 ,该工作正常。 但现在问题出在Visual Studio 2017上。

问题:到目前为止,在我的研究中,我发现在Visual Studio 2015之前,VS有直接的注册表项允许注册该工具,但是从VS 2017开始,Microsoft已经对注册表项的存储方式进行了更改( 一个很好的读取了解VS2017中的变化 )。

如果我打开VS 2017并尝试运行自定义工具,那么我得到错误

在此系统上找不到自定义工具“工具名称”

在此输入图像描述

这是显而易见的,因为自定义工具尚未在VS 2017中注册工作。

我试图跟随这个人说要将.bin文件加载到注册表中,但他也说它禁用了启动VS 2017.为了启动VS,我们必须卸载配置单元。 研究表明,.bin文件可以根据安装的VS的类型(企业,专业等)在不同的位置。

有没有人这样做过?

TIA

您可能必须通过创建Visual Studio扩展(VSIX)来遵循不同的方法,下面我已经详细解释了它,希望它有所帮助。

如何在Visual Studio 2017中创建自定义工具或单个文件生成器:

在VS2017创建自定义工具之前,需要实现Interface IVsSingleFileGenerator和代码来注册和注销系统注册表中的自定义工具,但在VS2017中,Microsoft已经更改了整个注册表结构。 更改是,VS将使注册表项进入私有注册表,以便系统注册表不会搞砸。 以前注册表项是在系统注册表中创建的,现在它们已经完成了

C:\\用户\\某某\\应用程序数据\\本地\\微软\\ VisualStudio的\\ 15.0_xx \\ privateregistry.bin

Visual Studio 2017还支持直接通过Visual Studio本身运行它来测试您的工具(F5),它启动另一个名为Visual Studio实验实例的Visual Studio实例,并且您的工具可以在其中进行测试,因为它使注册表项成为

C:\\用户\\某某\\应用程序数据\\本地\\微软\\ VisualStudio的\\ 15.0_xxExp \\ privateregistry.bin

按照以下步骤在VS2017中创建自定义工具:

  1. 我们需要创建一个VSIX扩展
  2. 添加新的Visual Studio包
  3. 实现IVsSingleFileGenerator
  4. 添加注册表项代码
  5. 通过在VS2017中运行它来编译和测试该工具
  6. 双击生成的.VSIX文件安装该工具

我们将创建一个扩展/自定义工具作为名为“CountLines”的示例,它将读取一个文件(将Custom Tool属性设置为CountLines)并生成一个包含文件中行数的XML文件。 例如<LineCount>1050</LineCount>

1.创建VSIX扩展为了创建扩展,您必须安装Visual Studio Extensibility Tools,它作为Visual Studio安装程序中的可选功能包含在内。 如果未安装,您也可以通过修改VS 2017安装程序来安装它。 通过选择创建新的VSIX(Visual Studio扩展)项目

新项目 - >可扩展性 - > VSIX项目

给它一些名字,如“CountLinesVSIX”。

2.添加新的Visual Studio包创建VSIX项目后,通过选择向其添加新的Visual Studio包

添加 - >新项 - >可扩展性 - > Visual Studio包

将其命名为“CountLines.cs”。 CountLines.cs我们需要删除现有代码并将其替换为我们的IVsSingleFileGenerator实现代码

3.实施IVsSingleFileGenerator写您的界面自定义实现IVsSingleFileGenerator ,我们的示例代码如下

using System;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Text;

namespace CountLinesVSIX
    {
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration( "CountLines", "Generate XML with line count", "1.0")] 
    [Guid("202E7E8B-557E-46CB-8A1D-3024AD68F44A")]
    [ComVisible(true)]
    [ProvideObject(typeof(CountLines))]
    [CodeGeneratorRegistration(typeof(CountLines), "CountLines", "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}", GeneratesDesignTimeSource = true)]
    public sealed class CountLines : IVsSingleFileGenerator
    {

        #region IVsSingleFileGenerator Members

        public int DefaultExtension(out string pbstrDefaultExtension)
        {
            pbstrDefaultExtension = ".xml";
            return pbstrDefaultExtension.Length;
        }

        public int Generate(string wszInputFilePath, string bstrInputFileContents,
          string wszDefaultNamespace, IntPtr[] rgbOutputFileContents,
          out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
        {
            try
            {
                int lineCount = bstrInputFileContents.Split('\n').Length;
                byte[] bytes = Encoding.UTF8.GetBytes("<LineCount>" + lineCount.ToString() + "</LineCount>" );
                int length = bytes.Length;
                rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(length);
                Marshal.Copy(bytes, 0, rgbOutputFileContents[0], length);
                pcbOutput = (uint)length;
            }
            catch (Exception ex)
            {
                pcbOutput = 0;
            }
            return VSConstants.S_OK;
        }

        #endregion
    }
}

我们需要为我们的扩展提供唯一的GUID,例如上面代码中的一个[Guid("202E7E8B-557E-46CB-8A1D-3024AD68F44A")] 通过选择“工具 - >创建GUID”,可以从VS2017 创建GUID 选择GUID格式作为注册表格式。 请注意,上面提到的GUID代码没有花括号。

COM Interops需要[ComVisible(true)]

[CodeGeneratorRegistration(typeof(CountLines), "CountLines", "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}", GeneratesDesignTimeSource = true)]是一个类属性,带有用于注册工具的代码。 参数是GeneratorType,GeneratorName和C#语言GUID

您还可以从支持自定义TextTemplate格式的“TemplatedCodeGenerator”派生,这可能需要一些额外的代码实现。

4.添加注册表项代码使用以下代码创建新的类文件,将其命名为CodeGeneratorRegistrationAttribute.cs

using System;
using System.Globalization;
using Microsoft.VisualStudio.Shell;

namespace CountLinesVSIX
{

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class CodeGeneratorRegistrationAttribute : RegistrationAttribute
    {
        private string _contextGuid;
        private Type _generatorType;
        private Guid _generatorGuid;
        private string _generatorName;
        private string _generatorRegKeyName;
        private bool _generatesDesignTimeSource = false;
        private bool _generatesSharedDesignTimeSource = false;

        public CodeGeneratorRegistrationAttribute(Type generatorType, string generatorName, string contextGuid)
        {
            if (generatorType == null)
                throw new ArgumentNullException("generatorType");
            if (generatorName == null)
                throw new ArgumentNullException("generatorName");
            if (contextGuid == null)
                throw new ArgumentNullException("contextGuid");

            _contextGuid = contextGuid;
            _generatorType = generatorType;
            _generatorName = generatorName;
            _generatorRegKeyName = generatorType.Name;
            _generatorGuid = generatorType.GUID;
        }

        /// <summary> 
        /// Get the generator Type 
        /// </summary> 
        public Type GeneratorType
        {
            get { return _generatorType; }
        }

        /// <summary> 
        /// Get the Guid representing the project type 
        /// </summary> 
        public string ContextGuid
        {
            get { return _contextGuid; }
        }

        /// <summary> 
        /// Get the Guid representing the generator type 
        /// </summary> 
        public Guid GeneratorGuid
        {
            get { return _generatorGuid; }
        }

        /// <summary> 
        /// Get or Set the GeneratesDesignTimeSource value 
        /// </summary> 
        public bool GeneratesDesignTimeSource
        {
            get { return _generatesDesignTimeSource; }
            set { _generatesDesignTimeSource = value; }
        }

        /// <summary> 
        /// Get or Set the GeneratesSharedDesignTimeSource value 
        /// </summary> 
        public bool GeneratesSharedDesignTimeSource
        {
            get { return _generatesSharedDesignTimeSource; }
            set { _generatesSharedDesignTimeSource = value; }
        }


        /// <summary> 
        /// Gets the Generator name  
        /// </summary> 
        public string GeneratorName
        {
            get { return _generatorName; }
        }

        /// <summary> 
        /// Gets the Generator reg key name under  
        /// </summary> 
        public string GeneratorRegKeyName
        {
            get { return _generatorRegKeyName; }
            set { _generatorRegKeyName = value; }
        }

        /// <summary> 
        /// Property that gets the generator base key name 
        /// </summary> 
        private string GeneratorRegKey
        {
            get { return string.Format(CultureInfo.InvariantCulture, @"Generators\{0}\{1}", ContextGuid, GeneratorRegKeyName); }
        }
        /// <summary> 
        ///     Called to register this attribute with the given context.  The context 
        ///     contains the location where the registration inforomation should be placed. 
        ///     It also contains other information such as the type being registered and path information. 
        /// </summary> 
        public override void Register(RegistrationContext context)
        {
            using (Key childKey = context.CreateKey(GeneratorRegKey))
            {
                childKey.SetValue(string.Empty, GeneratorName);
                childKey.SetValue("CLSID", GeneratorGuid.ToString("B"));

                if (GeneratesDesignTimeSource)
                    childKey.SetValue("GeneratesDesignTimeSource", 1);

                if (GeneratesSharedDesignTimeSource)
                    childKey.SetValue("GeneratesSharedDesignTimeSource", 1);

            }
        }

        /// <summary> 
        /// Unregister this file extension. 
        /// </summary> 
        /// <param name="context"></param> 
        public override void Unregister(RegistrationContext context)
        {
            context.RemoveKey(GeneratorRegKey);
        }
    }
}

上面的代码将确保您的条目是VS私人注册表

5.通过在VS2017中运行来编译和测试该工具您可以在“ source.extension.vsixmanifest ”中添加“安装目标”,以确保您的extesion支持不同的VS2017版本。 在VS 2017中运行您的工具以测试它是否按预期工作。 运行VSIX后,Visual Studio实验实例将安装扩展并将其注册到注册表“C:\\ Users \\ xyz \\ AppData \\ Local \\ Microsoft \\ VisualStudio \\ 15.0_xxExp \\ privateregistry.bin”。 您可以通过选择“工具 - >扩展和更新”来查看已安装的扩展。 要测试该工具,我们必须打开一个虚拟项目,在解决方案资源管理器中选择一个文件,转到其属性并将自定义工具属性更新为“CountLines”。 完成此操作后,VS将在后台运行该工具并生成输出,在我们的示例中,它将在所选文件下生成一个xml文件。 或者,一旦设置了自定义工具属性,您可以右键单击该文件并选择“运行自定义工具”

6.双击生成的.VSIX文件安装该工具成功测试后,尝试安装可在“projectName / bin / debug”位置找到的VSIX。 双击文件安装VSIX,按照安装步骤操作。 现在您的工具可用于VS2017。 使用工具类似,右键单击要运行自定义工具的文件,然后选择“运行自定义工具”

如果要卸载扩展,请转到“工具 - >扩展和更新 - >选择扩展”,然后单击“卸载”。 请注意,在VS关闭之前,不会卸载工具。 关闭后,您将获得一个要卸载的弹出窗口,选择“修改”进行卸载。

那么,在研究期间,我得到了这个问题的答案。

解:

  1. 我们必须加载.bin文件(通过load hiv)。
  2. 进行更改或编辑bin以注册您的工具。
  3. 卸载蜂巢。

步骤#1:加载配置单元。

a)开放注册表(注册表)。 选择节点HKEY_LOCAL_MACHINE

b)转到| 文件 - >加载配置单元

c)选择 - > %LocalAppData%\\ Microsoft\\ VisualStudio\\ 15.0_'instance_id'\\privateregistry.bin可用的bin文件。

d)提供密钥名称。 这将使用您的密钥名称在HKEY_LOCAL_MACHINE创建一个新的密钥条目。

e)您可以在HKEY_LOCAL_MACHINE\\YourKeyName\\Software\\Microsoft\\VisualStudio\\检查.bin文件

在此输入图像描述

步骤2:编辑bin :您现在可以按照与其他VS版本相同的方式注册自定义工具。 实际上唯一的问题是将VS2017密钥放入全局注册表中,并使用上面的步骤#1解决。

第3步:卸载Hive。

a)在HKEY_LOCAL_MACHINE下选择您的密钥。

b)转到| 文件菜单

c)卸载Hive。

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM