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;
}
相关推荐
jz_ddk26 分钟前
[指南] Python循环语句完全指南
开发语言·python·continue·循环·for·while·break
chilavert31827 分钟前
技术演进中的开发沉思-368:锁机制(中)
java·开发语言·jvm
大黄说说44 分钟前
MySQL数据库运维管理基础知识:从安装到日常维护的完整指南
开发语言
HAPPY酷1 小时前
C++ 多线程实战三板斧
java·开发语言·c++·技术美术
独自破碎E1 小时前
BISHI54货物堆放
android·java·开发语言
沃码2 小时前
【Lively Wallpaper 】插件开发:LivelyProperties.json 全流程实战教程
c#·json·livelywallpaper·视频壁纸
顾北122 小时前
SpringCloud 系列 04:Gateway 断言 / 过滤器 / 限流 一站式落地指南
java·开发语言·数据库
wuqingshun3141592 小时前
java创建对象的方式
java·开发语言
二十雨辰3 小时前
[英语]-介词和动词
开发语言
程序员敲代码吗3 小时前
提升Python编程效率的五大特性
开发语言·python