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;
}
相关推荐
mjhcsp4 小时前
C++ 数组:基础与进阶全解析
开发语言·c++
5335ld5 小时前
后端给的post 方法但是要求传表单数据格式(没有{})
开发语言·前端·javascript·vue.js·ecmascript
量子炒饭大师5 小时前
【一天一个计算机知识】—— 【编程百度】预处理指令
java·开发语言
任子菲阳5 小时前
学Java第四十四天——Map实现类的源码解析
java·开发语言
听风吟丶5 小时前
Java 11+ HttpClient 实战:从 HttpURLConnection 到现代 HTTP 客户端的全面升级
java·开发语言·http
今晚打老虎5 小时前
c++(斗罗大陆3)
开发语言·c++·斗罗大陆3
mywpython5 小时前
Python使用消息队列rabbitmq
开发语言·python·rabbitmq
hygge9995 小时前
JVM GC 垃圾回收体系完整讲解
java·开发语言·jvm·经验分享·面试
wuwu_q5 小时前
通俗易懂 + Android 开发实战的方式,详细讲讲 Kotlin 中的 StateFlow
android·开发语言·kotlin