如何使用 WiX 部署 VSTO 3.0 插件? [英] How do you use WiX to deploy VSTO 3.0 addins?

查看:35
本文介绍了如何使用 WiX 部署 VSTO 3.0 插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部署我用 Visual Studio 2008 编写的 VSTO 3 Application Level Word 2007 插件.我看到 WiX 有一个名为 WixOfficeExtension 的扩展,看起来它可能具有此功能,但我找不到任何它的文档,我无法从源代码中看出它的用途.

I want to deploy a VSTO 3 Application Level Word 2007 addin that I've written with Visual Studio 2008. I see that WiX has an extension named WixOfficeExtension that looks like it might have this functionality, but I can't find any documentation for it, and I can't discern it's purpose from the source code.

以前有没有人尝试过,你能成功吗?

Has anyone attempted this before, and were you able to pull it off successfully?

推荐答案

这是我最终使用的代码.我基本上将 MSDN 中的示例移植到使用 WiX.

This is the code I ended up using. I basically ported the examples from MSDN to use WiX.

注意:此特定解决方案仅适用于 Word 2007 插件,但 Excel 的情况非常相似.根据前面提到的MSDN 文章.

Note: This specific solution is only for a Word 2007 addin, but the case for Excel is very similar. Simply modify the registry/component checks and keys/values according to the aforementioned MSDN Article.

为了以完全信任的方式运行插件,必须将其添加到当前用户的包含列表中.可靠地做到这一点的唯一方法是使用自定义操作.这是 文章 中的自定义操作移植到新的 部署工具基础包含在 WiX 中.

In order to run addins with full trust, it must be added to the Inclusion List for the current user. The only way to do this reliably is with a custom action. This is a port of the custom action in the article to the new Deployment Tools Foundation included with WiX.

要使用它,请创建一个名为 VSTOCustomAction 的新 DTF 项目并添加 CustomAction.cs.

To use it, create a new DTF project called VSTOCustomAction and add CustomAction.cs.

using System;
using System.Security;
using System.Security.Permissions;
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.VisualStudio.Tools.Office.Runtime.Security;

namespace VSTOCustomActions
{
    public class CustomActions
    {
        private static string GetPublicKey(Session session)
        {
            return session["VSTOCustomAction_PublicKey"];
        }
        private static string GetManifestLocation(Session session)
        {
            return session["VSTOCustomAction_ManifestLocation"];
        }
        private static void ErrorMessage(string message, Session session)
        {
            using (Record r = new Record(message))
            {
                session.Message(InstallMessage.Error, r);
            }
        }

        [CustomAction]
        public static ActionResult AddToInclusionList(Session session)
        {
            try
            {
                SecurityPermission permission =
                    new SecurityPermission(PermissionState.Unrestricted);
                permission.Demand();
            }
            catch (SecurityException)
            {
                ErrorMessage("You have insufficient privileges to " +
                    "register a trust relationship. Start Excel " +
                    "and confirm the trust dialog to run the addin.", session);
                return ActionResult.Failure;
            }

            Uri deploymentManifestLocation = null;
            if (Uri.TryCreate(GetManifestLocation(session),
                UriKind.RelativeOrAbsolute, out deploymentManifestLocation) == false)
            {
                ErrorMessage("The location of the deployment manifest is missing or invalid.", session);
                return ActionResult.Failure;
            }

            AddInSecurityEntry entry = new AddInSecurityEntry(deploymentManifestLocation, GetPublicKey(session));
            UserInclusionList.Add(entry);

            session.CustomActionData.Add("VSTOCustomAction_ManifestLocation", deploymentManifestLocation.ToString());

            return ActionResult.Success;

        }

        [CustomAction]
        public static ActionResult RemoveFromInclusionList(Session session)
        {
            string uriString = session.CustomActionData["VSTOCustomAction_ManifestLocation"];
            if (!string.IsNullOrEmpty(uriString))
            {
                Uri deploymentManifestLocation = new Uri(uriString);
                UserInclusionList.Remove(deploymentManifestLocation);
            }
            return ActionResult.Success;
        }

    }
}

蜡片段

我们显然需要实际的 WiX 文件来安装插件.从你的主 .wcs 文件中引用它

Wix Fragment

We obviously need the actual WiX file to install the addin. Reference it from your main .wcs file with

<FeatureRef Id="MyAddinComponent"/>

插件.wcs

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment Id="Word2007Fragment">

      <!-- Include the VSTO Custom action  -->
      <Binary Id="VSTOCustomAction" SourceFile="path	oVSTOCustomAction.dll"/>
      <CustomAction Id="AddToInclusionList" BinaryKey="VSTOCustomAction" DllEntry="AddToInclusionList" Execute="immediate"/>
      <CustomAction Id="RemoveFromInclusionList" BinaryKey="VSTOCustomAction" DllEntry="RemoveFromInclusionList" Execute="immediate"/>

      <!-- Set the parameters read by the Custom action -->
      <!-- 
        The public key that you used to sign your dll, looks something like <RSAKeyValue><Modulus>...</Modulus><Exponent>...</Exponent></RSAKeyValue>
        Take note: There should be no whitespace in the key!
      -->
      <Property Id="VSTOCustomAction_PublicKey"><![CDATA[Paste you public key here]]></Property>
      <CustomAction Id="PropertyAssign_ManifestLocation" Property="VSTOCustomAction_ManifestLocation" Value="[INSTALLDIR]MyAddin.MyAddin.vsto" />

      <!-- Properties to check prerequisites -->
      <Property Id="VSTORUNTIME">
        <RegistrySearch Id="RegistrySearchVsto"
                        Root="HKLM"
                        Key="SOFTWAREMicrosoftvsto runtime Setupv9.0.30729"
                        Name="SP"
                        Type="raw"/>
      </Property>
      <Property Id="HASWORDPIA">
        <ComponentSearch Id="ComponentSearchWordPIA"
                         Guid="{816D4DFD-FF7B-4C16-8943-EEB07DF989CB}"/>
      </Property>
      <Property Id="HASSHAREDPIA">
        <ComponentSearch Id="ComponentSearchSharedPIA"
                         Guid="{FAB10E66-B22C-4274-8647-7CA1BA5EF30F}"/>
      </Property>


      <!-- Feature and component to include the necessary files -->
      <Feature Id="MyAddinComponent" Title ="Word 2007 Addin" Level="1" AllowAdvertise="no">
        <ComponentRef Id="MyAddinComponent"/>
        <Condition Level="0"><![CDATA[NOT ((VSTORUNTIME="#1") AND HASSHAREDPIA AND HASWORDPIA)]]></Condition>
      </Feature>

      <DirectoryRef Id="INSTALLDIR">
          <Component Id="MyAddinComponent" Guid="your component guid here">
              <File Name="MyAddin.dll" Source="path	oMyAddin.dll" />
              <File Name="MyAddin.dll.manifest" Source="path	oMyAddin.dll.manifest" />
              <File Name="MyAddin.vsto" Source="path	oMyAddin.vsto" />
              <RegistryKey Root="HKCU"
                  Key="SoftwareMicrosoftOfficeWordAddinsMyAddin"
                  Action="createAndRemoveOnUninstall">
                <RegistryValue Type="string" Name="FriendlyName" Value="MyAddin Word 2007 Addin" />
                <RegistryValue Type="string" Name="Description" Value="MyAddin Word 2007 Addin" />
                <RegistryValue Type="string" Name="Manifest" Value="[INSTALLDIR]MyAddin.vsto|vstolocal" KeyPath="yes"/>
                <RegistryValue Type="integer" Name="LoadBehavior" Value="3"/>
              </RegistryKey>
          </Component>
      </DirectoryRef>

      <!-- Modify the install sequence to call our custom action -->
      <InstallExecuteSequence>
        <Custom Action="AddToInclusionList" After="InstallFinalize"><![CDATA[(&MyAddinComponent = 3) AND NOT (!MyAddinComponent = 3)]]></Custom>
        <Custom Action="PropertyAssign_ManifestLocation" Before="AddToInclusionList"><![CDATA[(&MyAddinComponent = 3) AND NOT (!MyAddinComponent = 3)]]></Custom>
        <Custom Action="RemoveFromInclusionList" After="InstallFinalize"><![CDATA[(&MyAddinComponent = 2) AND NOT (!MyAddinComponent = 2)]]></Custom>
      </InstallExecuteSequence>
    </Fragment>
</Wix>

希望这可以为外面的人节省一些时间.

Hope that this saves some time for someone out there.

这篇关于如何使用 WiX 部署 VSTO 3.0 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆