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;
}
相关推荐
天若有情6734 分钟前
打破思维定式!C++参数设计新范式:让结构体替代传统参数列表
java·开发语言·c++
斯特凡今天也很帅7 分钟前
python测试SFTP连通性
开发语言·python·ftp
sunywz10 分钟前
【JVM】(4)JVM对象创建与内存分配机制深度剖析
开发语言·jvm·python
亲爱的非洲野猪10 分钟前
从ReentrantLock到AQS:深入解析Java并发锁的实现哲学
java·开发语言
星火开发设计11 分钟前
C++ set 全面解析与实战指南
开发语言·c++·学习·青少年编程·编程·set·知识
沛沛老爹26 分钟前
Web开发者进阶AI:Agent Skills-深度迭代处理架构——从递归函数到智能决策引擎
java·开发语言·人工智能·科技·架构·企业开发·发展趋势
Good_Starry34 分钟前
Java——正则表达式
java·开发语言·正则表达式
二哈喇子!39 分钟前
前端HTML、CSS、JS、VUE 汇总
开发语言·前端
欧洵.41 分钟前
Java.基于UDP协议的核心内容
java·开发语言·udp
情缘晓梦.1 小时前
C语言数据存储
c语言·开发语言