c# - 从 .Net DLL 引用方法时,C++ 函数的 Ruby FFI 调用在脚本执行时失败
问题描述
我已经在 ffi github 上打开了一个问题,但我也在这里发帖,因为显然堆栈溢出具有更广泛的社区。或许你们会有一些想法。
我有以下戏剧性的故事要与您分享:
- 我需要从 ruby 进程访问 .Net dll。
- 在这种情况下,COM 不是一个选项,请认为这是理所当然的 :)
- 我已经在 .Net dll 上编写了一个 C++ 包装器 dll 并导出了所需的函数。
- 以下是简短的来源:
demo.cs - CSharpDemo.dll
...
public class CSharpClass
{
public static string CSharpTest(string input)
{
return "Hello from C#";
}
}
demo.h - CppDemo.dll
...
extern "C" __declspec(dllexport) const char * CppTest(const char * inputValue);
demo_csharp.h - CppDemo.dll
...
using namespace CSharp::Demo;
inline
std::string CSharpTest(const char * inputValue) {
String^ managedInput = ToManagedString(inputValue); // unmanaged input str to managed str
String^ managedOutput = CSharpClass::CSharpTest(managedInput); // call C# method
return ToStdString(managedOutput); // managed str to unmanaged str
}
demo.cpp - CppDemo.dll
...
const char * CppTest(const char * inputValue) {
std::string output = CSharpTest(inputValue);
char * str = _strdup(output.c_str());
return str;
}
演示.rb
require "ffi"
module CppDemo
extend FFI::Library
ffi_lib "C:/full_path/CppDemo.dll", "C:/full_path/CSharpDemo.dll"
attach_function :CppTest, [ :string ], :string
end
message = CppDemo.CppTest("input")
puts "message = #{message}"
- 它可以正常工作:
- 我从 C++ 控制台应用程序访问 C++ dll:调用 .Net dll 成功并返回预期值;
- 我从 C++ dll 中删除了 C# dll 方法调用,ruby 脚本从 C++ 函数中获取值。
但是,只要 ffi 从 ruby 脚本加载的 C++ dll 调用 .Net dll 方法 - 我就会收到以下错误: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'CSharp.Demo, Version=1.0.0.0 , Culture=neutral, PublicKeyToken=null' 或其依赖项之一。该系统找不到指定的文件。在 CSharpTest(basic_string<char,std::char_traits,std::allocator >* , SByte* inputValue) 在 CppTest(SByte* inputValue) 在 c:\full_path\demo.cpp:line 5
问题:
- 任何想法为什么会发生?
- ffi 是否甚至假设可以使用具有 .Net dll 依赖项的 C++ dll?
- 如果是这样,我们是否有样品?
我知道这篇文章可能有点长而且令人困惑,所以如果需要一些澄清,请告诉我。
任何反馈将不胜感激。
提前致谢!
解决方案
推荐阅读
- php - 使用 PHP mySQL 显示来自两个 sql 表的数据
- c# - 找不到位图
- c++ - 静态全局变量初始化顺序
- python - 在 pytest 中创建不同的环境设置
- android - 如何自定义 SwipeToReferesh 布局顶部 ProgressBar 或 Loader?
- excel - Excel VBA粘贴链接循环
- javascript - 通过javascript根据自定义数据属性对div进行排序
- python - Python函数调用中莫名其妙的运算符行为?
- excel - 从星期数、星期几和年份 PowerQuery M 获取日期
- javascript - Object.create 相对于 Constructor 函数的实际优势是什么?