我有一个通过 DLL 调用 C++ CLI 的 C# 项目。它向 C# 返回一个字符串,该字符串显示在按钮事件的文本框中。我已将此 dll(named MyDll.dll) 添加为我的 C# 项目属性中的引用。我正在尝试使用 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++ CLI 代码
#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 header
// 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,则它可以正常工作。我该如何解决这个问题?
谢谢
最佳答案
错误在于这两行:
<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 文件的路径。
关于通过 WIX 安装程序安装的带有 CLI 项目 EXE 的 C# 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41608300/