通过 WIX 安装程序安装带有 CLI 项目 EXE 的 C# 不起作用

C# with CLI project EXE installed through WIX installer doesnt work

本文关键字:安装 EXE 项目 不起作用 WIX 程序 通过 CLI      更新时间:2023-10-16

我有一个通过DLL调用C++ CLI的C#项目。它将一个字符串返回到 C#,该字符串显示在按钮事件的文本框中。我通过在 C# 项目属性中添加此 dll(名为 MyDll.dll)作为引用来添加它。我正在尝试使用 wix 工具集创建安装程序。这是源代码。

C# 代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyDll;
namespace MyProject
{
    public partial class Form1 : Form
    {
        public Class1 obj = new Class1(); 
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string str = "";
            string str_new = obj.MyFunc(str);
            textBox1.Text = str_new;
        }
    }
}

C++ 命令行代码

#include "stdafx.h"
#include "MyDll.h"
namespace MyDll
{
    Class1::Class1(){}
    Class1::~Class1(){}
    Class1::!Class1(){}
    String^ Class1::MyFunc([Out] String^ str)
    {
        str = "My C++/Cli DLL";
        return outVal = str;
    }
}

C++ CLI 标头

// MyDll.h
#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
namespace MyDll {
    public ref class Class1
    {
        // TODO: Add your methods for this class here.
    public:
        Class1::Class1();
        Class1::~Class1();
        Class1::!Class1();
        String^ MyFunc([Out] String^ str);
        String^ outVal;
    };
}

我的 WXS 文件

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="installer_test" Language="1033" Version="1.0.0.0" Manufacturer="Company" UpgradeCode="a928c48a-8e5b-4038-b871-939ff8a9349f">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
        <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
        <MediaTemplate />
        <Feature Id="ProductFeature" Title="installer_test" Level="1">
            <ComponentGroupRef Id="ProductComponents" />
        </Feature>
    </Product>
  <Fragment>
    <Property Id="ROOTDRIVE">
      <![CDATA[E:]]>
    </Property>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFiles" Name="ProgramFiles">
        <Directory Id="TopFolder" Name="EXE3">
          <Directory Id="INSTALLFOLDER" Name="Main folder">
            <Component Id="cmpMain" Guid="{0509AAED-64A8-43F6-8935-70FB12305189}" KeyPath="yes" Feature="ProductFeature">
              <File Source="$(var.MyProject.TargetPath)" />
              <File Source="$(var.MyProject.TargetPath)" Name="MyDll.dll" ShortName="AF" />
            </Component>

          </Directory>
        </Directory>
      </Directory>
    </Directory>
    <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER"></Property>
    <UIRef Id="WixUI_InstallDir"/>
  </Fragment>
    <Fragment>
        <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
            <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
            <!-- <Component Id="ProductComponent"> -->
      <!--    <File Source="$(var.MyProject.TargetPath)"/>-->
         <!-- TODO: Insert files, registry keys, and other resources here. -->
      <!--   </Component> -->

    </ComponentGroup>
    </Fragment>
</Wix>

我的安装程序工作正常。它在指定的路径上安装一个exe和一个dll(MyDll.dll)。但是当我运行 exe 时,它什么也没做。如果我将已安装的 exe 替换为在我的 C# 项目的 bin 文件夹中创建的 exe,那么它可以正常工作。如何解决此问题?

谢谢

错误在于这 2 行:

          <File Source="$(var.MyProject.TargetPath)" />
          <File Source="$(var.MyProject.TargetPath)" Name="MyDll.dll" ShortName="AF" />

如果$(var.MyProject.TargetPath)是你的exe文件,那么你要安装它两次,第二次只是命名为MyDll.dll。没有安装原始 dll。

让它像

<File Source="$(var.MyProject.TargetPathExe)" />
<File Source="$(var.MyProject.TargetPathDll)" />

其中变量包含源 exe 和 dll 文件的路径。