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;
}
相关推荐
Larry_Yanan1 分钟前
Qt+OpenCV(一)环境搭建
开发语言·c++·qt·opencv·学习
独特的螺狮粉7 分钟前
开源鸿蒙跨平台Flutter开发:微波射频阻抗匹配系统-极坐标史密斯圆图与天线信号渲染架构
开发语言·flutter·华为·架构·开源·harmonyos
冰暮流星8 分钟前
javascript之dom方法访问内容
开发语言·前端·javascript
唐青枫8 分钟前
C#.NET gRPC 深入解析:Proto 定义、流式调用与服务间通信取舍
c#·.net
我命由我123459 分钟前
在 React 项目中,配置了 setupProxy.js 文件,无法正常访问 http://localhost:3000
开发语言·前端·javascript·react.js·前端框架·ecmascript·js
浅时光_c17 分钟前
9 循环语句
c语言·开发语言
stevenzqzq18 分钟前
Kotlin 协程:withContext 与 async 核心区别与使用场景
android·开发语言·kotlin
CDN36025 分钟前
弱网下游戏盾掉线重连失败?链路保活与超时参数优化
开发语言·游戏·php
im_AMBER26 分钟前
Leetcode 153 课程表 | 腐烂的橘子
开发语言·算法·leetcode·深度优先·图搜索
烈风30 分钟前
01_Tauri环境搭建
开发语言·前端·后端