gpt4 book ai didi

c# - 从 C++/CLI 调用 C# dll 时出现 FileNotFoundException

转载 作者:太空宇宙 更新时间:2023-11-03 23:10:57 26 4
gpt4 key购买 nike

我有一个 C# dll,其中包含一些方法,我正尝试在我的本地项目中使用 /CLR 支持访问这些方法。

我使用 #using 指令引用此 DLL,然后识别 DLL 并编译项目。

但是,在运行时,我得到一个 FileNotFoundException,这很奇怪,因为 DLL 存在于项目的源目录中。

DLLVS 2015 中编译,.NET 版本为 4.5.2。因为我的 C++ 有 CLR 支持,所以我编辑了项目文件使 TargetFrameworkVersion 为 4.5.2,但 runtime 仍然支持不工作。

请告知可能是什么问题?

EIDT - 添加了一些代码

C#

namespace TestManagedLibrary
{
public class Class1
{
public int i;
public Class1()
{
i = 5;
}

public int returnValue()
{
return i;
}
}
}

C++/CLI

#using <TestManagedLibrary.dll>


using namespace System;
using namespace System::Runtime::InteropServices; // Marshal
using namespace TestManagedLibrary;


ref class ManagedFoo
{
public:
ManagedFoo()
{
Console::WriteLine(_T("Constructing ManagedFoo"));
Class1 ^testObject = gcnew Class1();
int a = testObject->returnValue();
}
};

最佳答案

首先您需要确保TestManagedLibrary.dll 文件位于Fusion 所在的位置可以找到它。您的第一次尝试应该是您正在运行的可执行文件的位置。

处理此问题的一种方法是通过引用属性。如果对 TestManagedLibrary.dll 的引用设置了 copy local 标志,那么在构建期间,引用的库将从引用的位置复制到输出目录。

您可以启用内部 fusion logging了解详情:

  1. 以管理权限运行 Developer Command Prompt
  2. 运行fuslogvw
  3. Assembly Binding Log Viewer 中点击设置并设置 Log bind failures to diskLog all binds to disk
  4. 开始您的服务
  5. Assembly Binding Log Viewer 中点击 Refresh,选择您的可执行文件并点击 View Log

关于c# - 从 C++/CLI 调用 C# dll 时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39230290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com