我已经用C#构建了一个DLL。现在我想使用R环境调用该DLL中的函数。R环境支持调用非托管的C/C++ DLL,但不支持.NET DLL。那么我的问题是,我能否从C/C++ DLL中调用C# DLL中的函数?如果可以,您有关于如何实现的信息链接吗?
这篇文章或许可以帮到你:
更新:有一个叫做mergebin的工具随.NET SQLite封装器一起发布,可以用于创建混合模式的本机/托管DLL。从以下地址获取源代码:
SQLite for ADO.NET 2.0 (SourceForge)
你会在bin\tools
文件夹中找到可执行文件。
Kev
这实际上非常简单。只需使用NuGet将“UnmanagedExports”包添加到您的.Net项目中即可。有关详细信息,请参见https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports。
然后,您可以直接导出,而无需进行COM层。以下是示例C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;
class Test
{
[DllExport("add", CallingConvention = CallingConvention.Cdecl)]
public static int TestExport(int left, int right)
{
return left + right;
}
}