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;
}
相关推荐
mudtools12 小时前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的1 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21881 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi1 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1231 天前
matlab画图工具
开发语言·matlab
dustcell.1 天前
haproxy七层代理
java·开发语言·前端
norlan_jame1 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054961 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django