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;
}
相关推荐
灰子学技术3 分钟前
性能分析工具比较pprof、perf、valgrind、asan
java·开发语言
Minilinux20188 分钟前
Google ProtoBuf 简介
开发语言·google·protobuf·protobuf介绍
大尚来也11 分钟前
看不见的加速器:深入理解 Linux 页缓存如何提升 I/O 性能
java·开发语言
wWYy.11 分钟前
程序编译链接过程
开发语言
铁蛋AI编程实战14 分钟前
AI调用人类服务入门与Python实现(30分钟搭建“AI+真人”协作系统)
开发语言·人工智能·python
zhougl99615 分钟前
Java 常见异常梳理
java·开发语言·python
SunflowerCoder16 分钟前
基于插件化 + Scriban 模板引擎的高效 HTTP 协议中心设计
http·c#
独自破碎E19 分钟前
已经 Push 到远程的提交,如何修改 Commit 信息?
开发语言·github
数智工坊32 分钟前
【数据结构-栈、队列、数组】3.3栈在括号匹配-表达式求值上
java·开发语言·数据结构
lsx20240634 分钟前
Bootstrap 插件概览
开发语言