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;
}
相关推荐
ʚB҉L҉A҉C҉K҉.҉基҉德҉^҉大7 分钟前
C++中的策略模式进阶
开发语言·c++·算法
xb113212 分钟前
C#串口通信
开发语言·c#
小小码农Come on13 分钟前
QT内存管理
开发语言·qt
周杰伦fans17 分钟前
CAD二次开发中的线程、异步操作与LockDocument
c#
Zach_yuan19 分钟前
C++ Lambda 表达式从入门到进阶
开发语言·c++
weixin_4454023023 分钟前
模板元编程应用场景
开发语言·c++·算法
xyq202426 分钟前
Julia 日期和时间处理指南
开发语言
s1hiyu26 分钟前
嵌入式C++低功耗设计
开发语言·c++·算法
阿钱真强道34 分钟前
11 JetLinks MQTT 直连设备功能调用完整流程与 Python 实现
服务器·开发语言·网络·python·物联网·网络协议
£漫步 云端彡1 小时前
Golang学习历程【第十二篇 错误处理(error)】
开发语言·学习·golang