Visual Studio 2005插件编写与安装教程

Visual Studio 2005插件编写与安装教程
Tutorial for Visual Studio 2005 Add-in(Integration Package) Dev and Setup or Installation
remex1980 原创于 2007-6-19 19:51:44
原作者: remex1980
最近在做一些VS2005上的插件,在程序安装的时候着实遇到了不少问题,现在把自己的心得记录下来,以便同仁查阅:)

本文将介绍:如何调用、调试一个简单的Visual Studio 2005集成包(Visual Studio 2005 Integration Package),并集中精力详解如何为插件写一个安装程序。



安装Visual Studio 2005 SDK

这个我不想说太多,到微软的网站上下载最新版本,然后安装就好了。这里,我安装的是SDK 4.0,2007年2月发布的版本。

创建工程
创建一个空的工程

image

添加安装后的插件的例子

到sdk 的实例目录,如C:/Program Files/Visual Studio 2005 SDK/2007.02/VisualStudioIntegration/Samples/IDE/CSharp,本文将使用 Reference.ComboBox例子,它会给vs 2005的工具栏上添加一个ComboBox下拉框。

拷贝Reference.ComboBox目录到工程文件夹,并添加为其中的一个项目。
image


image

此 时,有可能出现下面的错误提示"Unable to read the project file 'Reference.ComboBox.csproj'. E:/temp/ToolbarcomboBox/Reference.combobox/Reference.ComboBox.csproj(101,11), The imported project "E:/Tools/Build/Microsoft.VsSDK.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk."
image

这 是因为即将打开的项目是SDK下开发的,需要一个sdk的文件,用记事本打开该项目文件(此处是/Reference.ComboBox/ Reference.ComboBox.csproj),到出错的行数,或查找Microsoft.VsSDK.targets,修改全路径到SDK安装 目录下。
本例中


<Import Project="........ToolsBuildMicrosoft.VsSDK.targets" Condition="'$(_NTDRIVE)' == ''"/>





<Import Project="C:Program FilesVisual Studio 2005 SDK?7.02VisualStudioIntegrationToolsBuildMicrosoft.VsSDK.targets" Condition="'$(_NTDRIVE)' == ''"/>


然后,重复上面的步骤加入插件项目。

调试插件

设 置项目属性(右键Reference.ComboBox项目,选择Properties选项),选择Debug项,设置"Start Action"到"Start External Program:",并选择vs 2005 的ide 执行文件位置,本文处为D:/Program Files/Microsoft Visual Studio 8/Common7/IDE/devenv.exe。设置"start options"中“command line argumnent:”为/rootsuffix Exp。

image

然后,按F5,vs2005会打开另外一个ide。
右键新打开的VS 2005工具栏,选择Combo Box Sample,就可以看到下面的结果了。
image
image

添加安装时执行的Installer链接库

添加一个新的project,此处命名为Reference.Combobox.VsSetupAction。

image

添加一个新的类(Add New Item),继承Installer。本文为VSIDEInstaller。
image
右键DevEnvSetupAction.cs文件,选择查看代码View Code

重载Install函数

 

public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            if (DevenvIsRunning())    // 判断VS 2005是否正在运行
            {
                throw new System.Configuration.Install.InstallException(VS_RUNNING_ERROR_MSG);
            }   // end if
            else
            {
                DevenvSetup();    // 启动VS2005,执行插件安装
            }   // end else
        }


判断VS 2005是否正在运行

private static bool DevenvIsRunning()
        {
            foreach (Process process in Process.GetProcesses())
            {
                if (process != null)
                {
                    try
                    {
                        foreach (ProcessModule module in process.Modules)
                        {
                            if (module != null && module.FileName != null)
                            {
                                if (string.Compare(Path.GetFileName(module.FileName), "devenv.exe", StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    return true;
                                }   // end if
                            }   // end if
                        }   // end foreach
                    }   // end try
                    catch (System.ComponentModel.Win32Exception)
                    {
                        // For some reason we're unable to enumerate the modules of some processes.
                        // Just ignore the error in that case.
                    }   // end catch
                }   // end if
            }   // end foreach

            return false;
        }   // end DevenvIsRunning

        private static void DevenvSetup()
        {
    // 读注册表获取vs2005的安装位置
            RegistryKey vsKey = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//VisualStudio//8.0");

            if (vsKey != null)
            {
                string vsPath = ((string)vsKey.GetValue("InstallDir")) + "devenv.exe";

                if (File.Exists(vsPath))
                {
    //执行devenv.exe /setup
                    Process vsProcess = Process.Start(vsPath, "/setup");
                    vsProcess.WaitForExit();
                }
            }

        }



为了使上面的代码能够运行,还需要引用下面的三个命名空间

using System.IO;
using System.Diagnostics;
using Microsoft.Win32;


添加安装项目

添加一个新项目,并选择类型为Setup Project。

image

添加文件到安装项目
右键安装项目,在弹出菜单中选择View->File System,在Application Folder中添加上面两个项目的输出文件(右键Add->Project Output)。
image

添加安装时的动作到安装项目

右键安装项目,在弹出菜单中选择View->Custom Actions,在Install文件夹图标中,右键选择Add Customer Action,选择应用程序文件夹中的安装动作项目的输出文件。


image

添加注册表项到安装项目

插件是需要注册到系统中的,也就是要注册到系统的注册表中,更具体一点说,就是注册到VS2005的注册表项里。

不过问题来了,谁知道该注册那些项?都是那些值呢?
不用担心,VS2005已经给了一个很好的工具,让你知道该注册那些东西了。

获取插件需要的注册表项

运行cmd.exe,到命令行模式,然后调用SDK中的regpkg.exe输出插件包所需要注册的注册表项。
本例中命令行如下:

regpkg.exe /regfile:c:ComboPackage.reg "c:Reference.ComboBox.dll"



首 先需要到regpkg.exe所在目录下,本例在C:/Program Files/Visual Studio 2005 SDK/2007.02/VisualStudioIntegration/Tools/Bin位置,输出注册表文件c:/ ComboPackage.reg。c:/Reference.ComboBox.dll是插件dll的路径(先编译vs package,然后拷贝到C:,当然,任何一位置都可以,只需要指定其全路径就行)。

image

打开上面输出的注册表文件,把对应的项和值,写在安装项目的注册表中就可以了,如下图:
image

对应的注册表文件中的值如下:


REGEDIT4

[HKEY_LOCAL_MACHINESoftwareMicrosoftVisualStudio8.0InstalledProductsComboBoxPackage]
@="#100"
"Package"="{0cd316df-48c8-4c28-af14-1be390348214}"
"ProductDetails"="#102"
"PID"="1.0"
"LogoID"="#400"
[HKEY_LOCAL_MACHINESoftwareMicrosoftVisualStudio8.0Packages{0cd316df-48c8-4c28-af14-1be390348214}]
@="Microsoft.Samples.VisualStudio.ComboBox.ComboBoxPackage, Reference.ComboBox, Version=1.0.2726.34182, Culture=neutral, PublicKeyToken=null"
"InprocServer32"="C:/WINDOWS/system32/mscoree.dll"
"Class"="Microsoft.Samples.VisualStudio.ComboBox.ComboBoxPackage"
"CodeBase" = "[TARGETDIR]Reference.ComboBox.dll"
"Assembly"="Reference.ComboBox, Version=1.0.2726.34182, Culture=neutral, PublicKeyToken=null"
[HKEY_LOCAL_MACHINESoftwareMicrosoftVisualStudio8.0Packages{0cd316df-48c8-4c28-af14-1be390348214}]
"ID"=dword:00000001
"MinEdition"="Standard"
"ProductVersion"="1.0"
"ProductName"="Reference.ComboBox Sample"
"CompanyName"="Microsoft"
[HKEY_LOCAL_MACHINESoftwareMicrosoftVisualStudio8.0Menus]
"{0cd316df-48c8-4c28-af14-1be390348214}"=", 1000, 1"

注意:为了适应不同的系统环境,可以将InprocServer32中的值,设置成[SystemFolder]mscoree.dll。这样,就可以自动找到对应的系统目录了。

保存工程,编译Setup Project,这样你的插件的安装程序就做好了,是不是很简单?:)


特别提醒:别忘了在里加上上面的蓝色注册表项,在生成的文件中没有自动产生。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Visual Studio 十个有用的小插件 无意中发现这么个地方:Ten Essential Tools,上面介绍了十个很好用的插件,以前用过几个,比如:TestDriven.NET,CodeKeep,于是使劲下了下来,但是还有两个找不到下载连接一个是PInvoke.NET 一个是VSMouseBindings,有那位朋友有或知道下载连接的提供一下,谢谢,我把下下来的打了个包,免得后来的朋友一个个找,请到这里下载 Vs10Add-Ins.part1.rar Vs10Add-Ins.part2.rar Vs10Add-Ins.part3.rar TestDriven.NET Test-driven development is the practice of writing unit tests before you write code, and then writing the code to make those tests pass. By writing tests before you write code, you identify the exact behavior your code should exhibit and, as a bonus, at the end you have 100 percent test coverage, which makes extensive refactoring possible. GhostDoc XML comments are invaluable tools when documenting your application. Using XML comments, you can mark up your code and then, using a tool like nDoc, you can generate help files or MSDN-like Web documentation based on those comments. The only problem with XML documentation is the time it takes to write it you often end up writing similar statements over and over again. The goal of GhostDoc is to automate the tedious parts of writing XML comments by looking at the name of your class or method, as well as any parameters, and making an educated guess as to how the documentation should appear based on recommended naming conventions. This is not a replacement for writing thorough documentation of your business rules and providing examples, but it will automate the mindless part of your documentation generation. Smart Paster Strings play a large role in most applications, whether they are comments being used to describe the behavior of the system, messages being sent to the user, or SQL statements that will be executed. One of the frustrating parts of working with strings is that they never seem to paste correctly into the IDE. When you are pasting comments, the strings might be too long or not aligned correctly, leaving you to spend time inserting line breaks, comment characters, and tabbing. When working with strings that will actually be concatenated, you have to do even more work, usually separating the parts of the string and inserting concatenation symbols or using a string builder. CodeKeep Throughout the process of software development, it is common to reuse small snippets of code. Perhaps you reuse an example of how to get an enum value from a string or a starting point on how to implement a certain pattern in your language of choice. PInvoke.NET P/Invoke is the process used to access native Win32 API calls within the .NET Framework. One of the hard parts of using P/Invoke is determining the method signature you need to use; this can often be an exercise in trial and error. Sending incorrect data types or values to an unmanaged API can often result in memory leaks or other unexpected results. VSWindowManager PowerToy The Visual Studio IDE includes a huge number of different Windows, all of which are useful at different times. If you are like me, you have different window layouts that you like to use at various points in your dev work. When I am writing HTML, I like to hide the toolbox and the task list window. When I am designing forms, I want to display the toolbox and the task list. When I am writing code, I like to hide all the windows except for the task list. Having to constantly open, close, and move windows based on what I am doing can be both frustrating and time consuming. WSContractFirst Visual Studio makes creating Web services deceptively easy You simply create an .asmx file, add some code, and you are ready to go. ASP.NET can then create a Web Services Description Language (WSDL) file used to describe behavior and message patterns for your Web service. VSMouseBindings Your mouse probably has five buttons, so why are you only using three of them? The VSMouseBindings power toy provides an easy to use interface that lets you assign each of your mouse buttons to a Visual Studio command. CopySourceAsHTML Code is exponentially more readable when certain parts of that code are differentiated from the rest by using a different color text. Reading code in Visual Studio is generally much easier than trying to read code in an editor like Notepad. Cache Visualizer Visual Studio 2005 includes a new debugging feature called visualizers, which can be used to create a human-readable view of data for use during the debugging process. Visual Studio 2005 includes a number of debugger visualizers by default, most notably the DataSet visualizer, which provides a tabular interface to view and edit the data inside a DataSet. While the default visualizers are very valuable, perhaps the best part of this new interface is that it is completely extensible. With just a little bit of work you can write your own visualizers to make debugging that much easier. Wrapping It Up While this article has been dedicated to freely available add-ins, there are also a host of add-ins that can be purchased for a reasonable price. I encourage you to check out these other options, as in some cases they can add some tremendous functionality to the IDE. This article has been a quick tour of some of the best freely available add-ins for Visual Studio. Each of these add-ins may only do a small thing, but together they help to increase your productivity and enable you to write better code.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值