You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于WiX v0.7.5实现Windows应用卸载API调用及MAC上报求助

实现WiX v0.7.5卸载时调用API上报MAC地址的方案

我之前刚好在WiX项目里实现过类似的卸载触发自定义逻辑的需求,针对你用的v0.7.5版本,给你一步步拆解实现步骤:

1. 编写自定义动作(Custom Action)

首先需要写一个能获取MAC地址并调用上报API的自定义逻辑,推荐用C#编写一个DLL(WiX对.NET自定义动作支持友好),核心代码示例如下:

using System;
using System.Net.NetworkInformation;
using System.Net.Http;
using Microsoft.Deployment.WindowsInstaller;

public class UninstallActions
{
    [CustomAction]
    public static ActionResult ReportUninstall(Session session)
    {
        try
        {
            // 获取物理网卡的MAC地址(过滤虚拟网卡)
            string macAddress = string.Empty;
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet && 
                    nic.OperationalStatus == OperationalStatus.Up &&
                    !nic.Description.Contains("Virtual") && !nic.Name.Contains("Virtual"))
                {
                    macAddress = nic.GetPhysicalAddress().ToString();
                    break;
                }
            }

            if (string.IsNullOrEmpty(macAddress))
            {
                session.Log("未能获取有效MAC地址");
                return ActionResult.Success; // 即使获取失败也不中断卸载
            }

            // 调用上报API
            using (HttpClient client = new HttpClient())
            {
                var payload = new { MacAddress = macAddress, UninstallTime = DateTime.UtcNow.ToString("o") };
                HttpResponseMessage response = client.PostAsJsonAsync("https://your-api-endpoint.com/report-uninstall", payload).Result;
                response.EnsureSuccessStatusCode();
                session.Log("卸载上报API调用成功");
            }

            return ActionResult.Success;
        }
        catch (Exception ex)
        {
            session.Log($"卸载上报失败: {ex.Message}");
            return ActionResult.Success; // 失败不中断卸载流程,可根据需求改为ActionResult.Failure
        }
    }
}

编译这个DLL时,要注意目标框架选.NET Framework 4.0或更低(WiX v0.7.5对高版本.NET支持有限),并且要引用Microsoft.Deployment.WindowsInstaller(可以通过NuGet安装对应包)。

2. 在WiX项目中集成自定义动作

在你的.wxs文件中添加以下配置:

2.1 注册自定义动作DLL

<Binary Id="UncustomActionsDll" SourceFile="path\to\your\UninstallActions.dll" />
<CustomAction Id="ReportUninstallAction" BinaryKey="UncustomActionsDll" DllEntry="ReportUninstall" Execute="deferred" Impersonate="no" Return="ignore" />
  • Execute="deferred":表示在延迟执行序列中运行,适合需要系统权限的操作
  • Impersonate="no":使用系统权限执行,避免用户权限不足的问题
  • Return="ignore":即使API调用失败也不终止卸载流程,可根据需求调整为check

2.2 在卸载序列中触发自定义动作

找到InstallExecuteSequence节点,添加卸载时的触发逻辑:

<InstallExecuteSequence>
    <!-- 其他已有序列节点 -->
    <Custom Action="ReportUninstallAction" After="RemoveFiles">REMOVE="ALL"</Custom>
</InstallExecuteSequence>

REMOVE="ALL"是WiX的条件,表示只有在完全卸载时才触发这个动作;After="RemoveFiles"表示在删除应用文件之后执行上报,你也可以根据需求调整到其他卸载阶段,比如Before="UninstallFinalize"。

3. 关键注意事项

  • 隐私合规:务必在应用的隐私政策中明确告知用户你会收集MAC地址用于卸载追踪,符合当地的隐私法规(如GDPR、CCPA等)
  • MAC地址过滤:代码中已经过滤了虚拟网卡,避免获取到无效的MAC地址,你可以根据实际需求调整过滤规则
  • API调用可靠性:建议给API调用添加超时设置,避免卸载流程长时间等待;如果API不可达,不要中断卸载流程
  • WiX版本兼容:v0.7.5版本比较老旧,确保自定义动作的DLL编译目标框架和WiX兼容,避免出现加载失败的问题

内容的提问来源于stack exchange,提问作者JK Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:48:46