簡體   English   中英

Wix CustomAction dll

[英]Wix CustomAction dll

我有一個使用CAQuietExec的自定義操作,在某些情況下會失敗-唯一的錯誤消息出現在日志文件中,這對我作為開發人員來說是個好消息,而對最終用戶來說卻毫無用處。 我的目標是在回滾安裝之前捕獲失敗的操作並預設標准錯誤消息。

為此,根據我的研究,我決定需要編寫自己的自定義操作dll,因此開始學習本教程 在此階段,我的dll僅記錄一條測試消息,並嘗試將錯誤消息傳遞回對話框屏幕。 但是,當我編譯並運行msi時,什么也沒有發生-沒有日志記錄,也沒有錯誤消息。 查看Orca中的msi,對我來說看起來不錯-但如您在下面看到的,如果測試dll實際上運行並且沒有運行,則測試dll應該導致安裝立即中止。

所以我的問題是:我要以最好的方式向用戶提供反饋嗎? 如果是這樣,為什么我的自定義操作無法執行?

謝謝

CustomAction.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace MyCustomActions
{
public class CustomActions
{
    [CustomAction]
    public static ActionResult CustomAction1(Session session)
    {
        session.Log("Begin CustomAction1");

        DisplayMSIError(session, "A test message");

        return ActionResult.Failure;
    }

    static void DisplayMSIError(Session session, String msg)
    {
        var r = new Record();
        r.SetString(0, msg);
        session.Message(InstallMessage.Error, r);
    }
}
}

產品.wxs

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="MyPackage" Language="1033" Version="0.0.0.1" Manufacturer="Acme" UpgradeCode="GUID">
<Package InstallerVersion="300" Compressed="yes" InstallScope="perMachine" />

<Binary Id="MyCustomActions.CA.dll" src="MyCustomActions\MyCustomActions.CA.dll" />

<CustomAction Id="MyCustomActionsTest"
  Return="check"
  Execute="immediate"
  BinaryKey="MyCustomActions.CA.dll"
  DllEntry="CustomAction1" />

<InstallExecuteSequence>
  <Custom Action="MyCustomActionsTest" After="LaunchConditions"  />
  ...
</InstallExecuteSequence>
</Product>
</Wix>

gh,我在這上面浪費了時間:但是對於遇到相同問題的任何人,我都會通過自定義操作發布解決我的問題的答案-希望它可以節省您的時間和沮喪的時間。

我很早就將測試動作放在序列中,以為我會看到錯誤消息對話框。 因此,我實際上並沒有嘗試完成安裝-當出現歡迎對話框時,我認為我的操作沒有運行。 但是,它必須已運行,因為當您實際選擇功能並允許安裝進行時,隨后會出現錯誤消息。

不過,如果任何人都可以提供某種見解,以了解這是否是從CAQuietExec正常運行的動作中提供一些用戶反饋的最佳方法,將不勝感激。

您如何看待該解決方案:

      Record record = new Record();
      record.FormatString = string.Format("Something has gone wrong!");

      session.Message(
         InstallMessage.Error | (InstallMessage) ( MessageBoxIcon.Error ) |
         (InstallMessage) MessageBoxButtons.OK,
         record );

這個答案

另一種方法-創建自己的UI並根據需要顯示任何消息。 小例子:

自定義操作:

    [CustomAction]
    public static ActionResult CheckSqlSessionState(Session session)
    {
        try
        {
            session.SendMessage(InstallMessage.Info, "Check Sql Session State");

            return ActionResult.Success;
        }
        catch (Exception exception)
        {
            session.SendMessage(InstallMessage.Error,
                string.Format("Error during the cheking sesssion state. {0}", exception.Message));
            return ActionResult.Failure;
        }
    }

注冊:

    <CustomAction Id="WiXSetup.CustomAction.CheckSqlSessionState"
          BinaryKey="WiXSetup.CustomActions.dll" 
          DllEntry="CheckSqlSessionState" Execute="deferred" />

    <CustomAction Id="SetCheckSqlSessionStateDataValue"
          Return="check"
          Property="WiXSetup.CustomAction.CheckSqlSessionState" />

    <InstallExecuteSequence>
         <Custom Action="SetCheckSqlSessionStateDataValue" Before="WiXSetup.CustomAction.CheckSqlSessionState">NOT Installed</Custom>
         <Custom Action="WiXSetup.CustomAction.CheckSqlSessionState" After="WiXSetup.CustomAction.UpdateHomePageUrls">NOT Installed</Custom>
   </InstallExecuteSequence>

CSharp用戶界面:

   using Microsoft.Deployment.WindowsInstaller;

   private ExternalUIRecordHandler recordHandler;
   // ...

   IntPtr parent = IntPtr.Zero;

   recordHandler = RecordHandler;

   private MessageResult RecordHandler(InstallMessage messageType, Record messageRecord, MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultButton)
    {
        switch (messageType)
        {
            case InstallMessage.Info:
                log.Info(message);
                return MessageResult.OK;
            case InstallMessage.Initialize:
                log.Debug(message);
                return MessageResult.OK;
            case InstallMessage.ActionData:
                log.Debug(message);
                return MessageResult.OK;
            case InstallMessage.ActionStart:
                log.Debug(message);
                return MessageResult.OK;
            case InstallMessage.CommonData:
                return MessageResult.OK;
            case InstallMessage.Error:
                log.Error(message);
                return MessageResult.OK;
            case InstallMessage.FatalExit:
                log.Fatal(message);
                return MessageResult.OK;
            case InstallMessage.FilesInUse:
                return MessageResult.No;
            case InstallMessage.OutOfDiskSpace:
                break;
            case InstallMessage.Progress:
                return MessageResult.OK;
            case InstallMessage.ResolveSource:
                return MessageResult.No;
            case InstallMessage.ShowDialog:
                return MessageResult.OK;
            case InstallMessage.Terminate:
                return MessageResult.OK;
            case InstallMessage.User:
                return MessageResult.OK;
            case InstallMessage.Warning:
                log.Warn(message);
                return MessageResult.OK;
        }

        return MessageResult.No;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM