Qt之調(diào)用C#的動(dòng)態(tài)庫(kù)的解決方法
環(huán)境:VS2019+Qt5.12
1. CLR庫(kù)安裝
首先,如果你VS2019沒有安裝CLR庫(kù),那么操作步驟為:
- 打開 Visual Studio Installer
- 在已安裝中點(diǎn)擊修改
- 將使用C++的桌面開發(fā)的對(duì)V142(14.25)生成工具的C++/CLI支持
- 點(diǎn)擊右下角的修改,安裝完成后重啟軟件即可
2. 新建類庫(kù)(.NET Framework)
注意:此處請(qǐng)確認(rèn)選擇用于創(chuàng)建C#類庫(kù)(.dll)的項(xiàng)目
此時(shí)解決方案的視圖為:
一個(gè)簡(jiǎn)單的測(cè)試直接在Class1.cs文件添加內(nèi)容即可,此測(cè)試中只修改此文件內(nèi)容
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassLibrary1 { public class Class1 { public Class1() { } public int myAdd(int a, int b) { int c = a + b + 5; return c; } public void mySub(int a, int b, ref int c) { c = a - b - 5; } public void mySetText(string text) { Console.WriteLine("ClassLibrary1的類名Class1下的mySetText: {0}", text); } public void myGetText(ref string text) { text = "ClassLibrary1的類名Class1下的myGetText"; } } }
編寫好了之后生成dll,將生成的dll復(fù)制到CLR的文件路徑下
3. 新建CLR類庫(kù)(.NET Framework)
此時(shí)解決方案的視圖為:
一個(gè)簡(jiǎn)單的測(cè)試對(duì)應(yīng)的頭文件ClassLibrary2.h修改即可,此測(cè)試中只修改此文件內(nèi)容
#pragma once using namespace System; using namespace System::Reflection; using namespace System::Runtime::InteropServices; #using "./ClassLibrary1.dll" using namespace ClassLibrary1; extern "C" __declspec(dllexport) int MyAdd(int a, int b) { ClassLibrary1::Class1 obj; return obj.myAdd(a, b); } extern "C" __declspec(dllexport) void MySub(int a, int b,int *c) { ClassLibrary1::Class1 obj; return obj.mySub(a, b, *c); } extern "C" __declspec(dllexport) void MySetText(char* text) { ClassLibrary1::Class1 obj; String^ str = gcnew String(text); obj.mySetText(str); } extern "C" __declspec(dllexport) void MyGetText(char** text) { ClassLibrary1::Class1 obj; String^ str = gcnew String(""); obj.myGetText(str); *text = (char*)(void*)Marshal::StringToHGlobalAnsi(str); }
這里編寫完成后生成dll,然后非常重要的一步來了,將ClassLibrary1.dll、ClassLibrary2.dll、ClassLibrary2.lib準(zhǔn)備復(fù)制到運(yùn)行的Qt執(zhí)行目錄下,如果沒有在同一個(gè)目錄下,在ClassLibrary2調(diào)用ClassLibrary1時(shí)會(huì)找不到ClassLibrary1.dll文件而報(bào)錯(cuò)
4. Qt調(diào)用
4.1. 調(diào)用方法1
#include <QCoreApplication> #include <windows.h> #include <QDebug> #include <QLibrary> #include <QFile> typedef int (*ADD)(int,int); typedef void (*SUB)(int,int,int *); typedef void (*SHOW)(QString); int main(int argc, char *argv[]) { QLibrary mylib("ClassLibrary2.dll"); //聲明dll文件 if (mylib.load()) //判斷加載是否成功 { qDebug() << "DLL loaded!"; ADD add = (ADD)mylib.resolve("MyAdd"); //鏈接到add函數(shù) qDebug() << "add status: " << add; if (add){ qDebug()<< "Link to add Function is OK!" << add(3,2) ; } SUB sub = (SUB)mylib.resolve("MySub"); qDebug() << "sub status: " << sub; if (sub){ int c = 10; sub(3,2,&c); qDebug()<< "Link to sub Function is OK!" << c; } SHOW show = (SHOW)mylib.resolve("MySetText"); qDebug() << "show status: " << show; if (show){ qDebug()<< "Link to show Function is OK!" ; const char *buf = "helloworld"; show(buf); } qDebug()<< "DLL unload " << mylib.unload (); } else { qDebug()<< "DLL is not loaded!" ; } return 0; }
4.2. 調(diào)用方法2
- 右鍵項(xiàng)目->選擇添加庫(kù)->選擇外部庫(kù)
- 在庫(kù)文件中找到剛才生成的ClassLibrary2.lib
- 將平臺(tái)下的linux、Mac取消勾選
- 將Windows下的所有都取消勾選
此時(shí)界面如下
extern "C" __declspec(dllexport) void MyGetText(char **p); extern "C" __declspec(dllexport) void MySetText(char *p); int main(int argc, char *argv[]) { MySetText(QString("helloworld").toUtf8().data()); char* change_t=nullptr; MyGetText(&change_t); qDebug() << QString(change_t); }
到此這篇關(guān)于Qt之調(diào)用C#的動(dòng)態(tài)庫(kù)的文章就介紹到這了,更多相關(guān)Qt調(diào)用C#動(dòng)態(tài)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用FileStream復(fù)制一個(gè)任意文件
這篇文章主要為大家詳細(xì)介紹了C#使用FileStream復(fù)制一個(gè)任意文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05c# 實(shí)現(xiàn)計(jì)時(shí)器功能
這篇文章主要介紹了c# 實(shí)現(xiàn)計(jì)時(shí)器功能的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12C#?使用EntityFramework?CodeFirst?創(chuàng)建PostgreSQL數(shù)據(jù)庫(kù)的詳細(xì)過程
這篇文章主要介紹了C#使用EntityFramework?CodeFirst創(chuàng)建PostgreSQL數(shù)據(jù)庫(kù)的過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07基于C#實(shí)現(xiàn)鼠標(biāo)設(shè)置功能
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)鼠標(biāo)設(shè)置功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#實(shí)現(xiàn)DataTable映射成Model的方法(附源碼)
這篇文章主要介紹了C#實(shí)現(xiàn)DataTable映射成Model的方法,以實(shí)例形式較為詳細(xì)的分析了DataTable映射成Model的具體步驟與相關(guān)技巧,并附帶了完整實(shí)例源碼供讀者下載,需要的朋友可以參考下2015-11-11