qt调用c#编写的dll

博客介绍了在Qt环境中如何通过C#和C++混合编译DLL来实现跨语言交互。首先尝试了直接使用C# DLL但未成功,然后尝试CLR工程但因与Qt moc冲突导致失败。最终解决方案是创建C# DLL和C++ CLR DLL,通过C++ CLR DLL作为桥梁在Qt应用中调用C#的功能,实现了Qt程序对C#方法的调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想法:

1.com的方式(自定义的c# dll可以,对于手边的dll文件,不行)

2.clr工程的方式(qt里面的moc直接和clr冲突,编译报错,不行)

3.c#的dll + c++的clr工程dll + qt exe调用

---- c#的dll编写(示例Class1.cs) ----

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
namespace ClassLibraryDll
{
    public class Class1
    {
        public int testDllMut(int a,int b)
        {
            return a * b;
        }
    }
}

---- c++ clr的dll编写(示例testCppClrDll.h) ----

配置工程属性->添加引用->浏览(找到c# dll文件)->确定

testCppClrDll.h

#pragma once

using namespace System;

namespace testCppClrDll
{
    public ref class Class1
    {
        public:
                .....
    };
}

注:该文件导出的类只能子啊clr工程或者c#工程(exe/dll)中才能调用。调用方式参考其他文章。

因为上面的文件testCppClrDll.h内容中头using...托管资源,在Qt中无法直接#using...使用, 必须新建文件(如SolidClrDll.h,SolidClrDll.cpp)

SolidClrDll.h

#pragma once

class __declspec(dllexport) Class2
{
public:
    int mu2(int a,int b);
}

SolidClrDll.cpp

#include "stdafx.h"
#include "SolidClrDll.h"
#include <iostream>

using namespace ClassLibraryDll;

int Class2::mu2(int a,int b)
{
    Class1^ c1 = gcnew Class1();
    return 2*c1->testDllMut(a,b);
}

---- qt的控制台程序编写(示例Class1.cs) ----

#include <QtCOre/QCoreApplication>
#include <QDebug>

#include "../testCppClrDll/SolidClrDll.h"
#pragma comment(lib,"../x64/Debug/testCppClrDll.lib")

int main(int argc, char* argv[])
{
    QCoreApplication a(argc,argv);

    Class2 c2;
    qDebug() << c2.mu2(2,6); // 24 

    return a.exec();

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值