c# - 如何导出 C# dll 方法/函数以在 C++ 中使用它

标签 c# c++ unmanaged dllexport

<分区>

我已经用C#完成了我的部分编码,下面给出了一个小方法

 public  static unitdefn GetUnit(XmlDocument doc)
    {

        XmlNodeList nodeList1 = (XmlNodeList)doc.DocumentElement.SelectNodes("//UnitDefinitions/Unit");
        int countdefn = nodeList1.Count;
        unitdefn[] arrunt = new unitdefn[countdefn];
        if (countdefn != 0)
        {

            int i = 0;
            foreach (XmlNode node in nodeList1)
            {
                XmlAttribute att = node.Attributes["name"];
                if (att != null)
                {
                    string idu = node.Attributes["name"].Value;
                    //System.Console.WriteLine(id1);
                    arrunt[i].name = idu;
                }
                i++;
            }

        }
        return arrunt[0];
    }

我想在我的 C++ 项目中使用这个方法,我已经按照下面给出的方法做了

int  xmlRead()
{

int x=1,s=1;
char xmldllpath[1000]="//dllpath";
HMODULE h = LoadLibrary(xmldllpath);

if (!h) {
    printf("error: Could not load %s\n", xmldllpath);
    return 0; // failure
}

getAdr(&s, h, "GetUnit");
}

dll 已成功加载,但未获取该方法 任何特定的方式来做到这一点

最佳答案

不要。不支持从 C# 导出方法。手动编辑发出的程序集几乎不可能,但实际上 - 不要。

您可以使用一些合适的解决方案:

  • 使用 C++/CLI 项目将托管方法公开给非托管代码
  • 使用 COM(.NET 基本上是 COM 2.0,所以这很容易;在 C++ 方面也很容易使用)
  • 在初始化时将委托(delegate)传递给非托管代码,并在需要时调用它
  • 使用一些其他的进程间通信方法(例如命名管道、TCP...)

关于c# - 如何导出 C# dll 方法/函数以在 C++ 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32311166/

相关文章:

c# - foreach循环

c# - 使用 C# 在控制台中计算 sstf 算法

c# - 添加方法后有关静态局部函数的错误

C# 托管非托管代码

c# - 从非托管 C++ 调用 C# 函数(通过托管包装器)

c# - 如何将 unsigned long * params 转换为 ulong[] params

c# - 为什么 Outlook 在使用 Office 互操作时崩溃?

c++ - 如何使用 CIN 在一行中捕获两种不同的数据类型?

c++ - 双哈希函数在 C++ 中如何工作?

C++ getline() 不需要命名空间声明