C++、C#互调用之C++ 调用C# dll

1、c# 创建dll

建立C#编写的DLL程序AddDll,项目类型为:类库

程序代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace AddDll
{
    public class Add
    {
        public int iadd(int a, int b)
        {
            int c = a + b;
            return c;
        }
    }
}

2、C++编写调用程序

建立C++的Win32控制台应用程序UseDll,项目类型为:Win32控制台应用程序

配置:右键点击解决方案资源管理器中的UseDll,选择“属性”,将公共语言运行库支持设置为“公共语言运行库支持(/clr)”

程序代码:

#include "stdio.h"

#using "..\debug\AddDll.dll"
using namespace AddDll;

int main()
{
        int result;
        Add ^add = gcnew Add();   //生成托管类型

//gcnew creates an instance of a managed type (reference or value type) on the garbage

//collected heap. The result of the evaluation of a gcnew expression is a handle (^) to

//the type being created.

        result = add->iadd(10,90);
        printf("%d",result);
        scanf("%s");
        return 0;
}


 

原文地址:https://www.cnblogs.com/MayGarden/p/1629620.html