C#生成dll给c++调用 方法二COM方式 vs2022 NO Make Assembly COM-Visible选错了 不需要clr

有些C++项目中也用了C语言.c,用方法一就无法使用【不能使用 /clr 选项编译 C 文件】。就用方法2。

方法二:COM方式

参考: https://www.5axxw.com/questions/content/2ozion

1.C# 生成dll

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Runtime.InteropServices;




namespace ClassLibrary1
{
    public class Class1
    {
        public interface IMyClass
        {
            void Initialize();
            void Dispose();
            int Add(int a, int b);
        }

        public class MyClass : IMyClass
        {
            public void Initialize()
            {

            }

            public void Dispose()
            {

            }

            public int Add(int a, int b)
            {
                return a + b;
            }
        }
    }
}

编译正常,C# dll就完成了。

2.C++调用示例

cs 复制代码
#include <iostream>
using namespace std;

#import "../x64/Debug/ClassLibrary1.tlb"

int main()
{
	CoInitialize(NULL); // 初始化com环境
	ClassLibrary1::IMyClassPtr p(__uuidof(ClassLibrary1::MyClass));
	cout << p->Add(3, 4) << endl;

	system("pause");
	return 0;
}
相关推荐
yqcoder1 分钟前
JS 类型检测双雄:typeof vs instanceof 深度解析
开发语言·javascript·ecmascript
NEGl DRYN7 分钟前
Go基础之环境搭建
开发语言·后端·golang
AI木马人8 分钟前
20.人工智能实战:大模型项目如何从 Demo 走向生产?一套可落地的上线验收清单与工程治理方案
java·开发语言·人工智能
CandyU210 分钟前
Unity —— 反射
java·开发语言
初心未改HD11 分钟前
Go Modules:依赖管理的完全指南
开发语言·golang
楼田莉子11 分钟前
仿照Muduo的高并发服务器:EventLoop模块及与TimeWheel模块联调
java·开发语言
小雅痞14 分钟前
[Java][Leetcode middle] 3. 无重复字符的最长子串
java·开发语言·leetcode
逻辑驱动的ken24 分钟前
Java高频面试考点场景题21
java·开发语言·面试·职场和发展·求职招聘
rOuN STAT31 分钟前
Golang 构建学习
开发语言·学习·golang
fengxin_rou36 分钟前
黑马点评项目万字总结:从redis基础到实战应用详解
java·开发语言·分布式·后端·黑马点评