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;
}
相关推荐
他们都不看好你,偏偏你最不争气15 分钟前
【iOS】AFNetworking
开发语言·macos·ios·objective-c
Bigemap22 分钟前
BigemapPro快速添加历史影像(Arcgis卫星地图历史地图)
java·开发语言
进击的_鹏34 分钟前
【C++11】initializer_list列表初始化、右值引用和移动语义、可变参数模版等
开发语言·c++
mark-puls40 分钟前
C语言打印爱心
c语言·开发语言·算法
西阳未落1 小时前
C语言柔性数组详解与应用
c语言·开发语言·柔性数组
Huhbbjs1 小时前
SQL 核心概念与实践总结
开发语言·数据库·sql
咕噜咕噜啦啦1 小时前
Qt之快捷键、事件处理、自定义按键——完成记事本项目
开发语言·qt
Source.Liu1 小时前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
晚云与城1 小时前
今日分享:C++ deque与priority_queue
开发语言·c++
半梦半醒*1 小时前
正则表达式
linux·运维·开发语言·正则表达式·centos·运维开发