网上有大神教我们怎么用C#调用Qt写的dll,试了一下,没调出来,可能是Qt版本的问题;
今天调了个程序出来,话不多说,写步骤,上代码:
1,从网上下一个qtwinmigrate:
https://github.com/qtproject/qt-solutions/tree/master/qtwinmigrate
发现qtwinmigrate文件夹里面有例程;
2,Qt中编译一下,然后在C#中调用,OK;
3,然后自己写一个程序,编译成dll,用C#调用,OK;
----------------------------------------------------------------
代码如下:
Qt dll:
qtui.pro:
TEMPLATE = lib
CONFIG += dll
SOURCES = main.cpp \
dialog.cpp
TARGET = qtui
DLLDESTDIR = $$[QT_INSTALL_PREFIX]/bin
include(../../src/qtwinmigrate.pri)
FORMS += \
dialog.ui
HEADERS += \
dialog.h
注意,要把qtwinmigrate文件夹中的src添加到项目中;
头文件:dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void on_m_pbCalc_clicked();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
cpp:
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_m_pbCalc_clicked()
{
int add1 = ui->m_lE1->text().toInt();
int add2 = ui->m_lE2->text().toInt();
int sum = add1+add2;
QString strSum;
strSum.setNum(sum);
ui->m_lbSum->setText(strSum);
ui->m_lEsum->setText(strSum);
}
main.cpp
#include <qmfcapp.h>
#include <qwinwidget.h>
#include <QMessageBox>
#include <windows.h>
#include "dialog.h"
extern "C" __declspec(dllexport) int showDialog()
{
int argc = 0;
//char* argv[] = NULL;
QApplication a(argc, NULL);
Dialog *dlg = new Dialog();
dlg->show();
return a.exec();
}
//int main()
//{
// Dialog *dlg = new Dialog();
// dlg->show();
// return 0;
//}
界面没有啥,就是三个文本框: 实现一个加法;
C#这边简单粗暴:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows;
namespace CSCallQtdll
{
public partial class Form1 : Form
{
[DllImport("qtui.dll")]
public static extern int showDialog();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
showDialog();
}
}
}
界面就一个按钮,把qtui.dll添加进项目就可以了;
//
时隔多年,今天(2020/11/27)又要开始写一个Qt带界面的dll,照着自己的博客重新操作了一遍,结果运行不起来;
后来如有神助,找到了解决方法。
1,如果运行有问题,看一下dll和C#程序是否都是32位,或者都是64位;
2,dll可能缺少一些Qt库;
对于第二个问题的解决方案,可以参考以下这段文字;
于是,我就按照教程进行了设置。设置完后,兴奋地双击执行程序,发现还是出现同样的错误。于是,继续寻找其他解决方案,找啊找,看到一篇国外论坛解决方案,让我用Qt部署工具windeployqt.exe来检测运行该exe所需动态库,运行命令很简单:
windeployqt.exe 目标.exe
运行完成后,目标.exe同级目录中就会出现其依赖库了,相关的plugins和platform文件夹与动态库都会拷贝在该目录下。
当我再次运行时,奇迹发生了,exe启动了,功能也都正常!
作者:菜菜子_forest
链接:https://www.jianshu.com/p/304c9e6de4d2
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
感谢这位作者,解决了我的问题,而且,确实很神奇,对了,对C#的exe文件进行操作没有意义,但可以对Qt 生成的dll进行这样的操作,嗯,再次运行就可以了。
2020 11 30
今天尝试在VS-Qt 中打开Qt gui dll项目,然后编译,让C#的Form调用,发现也是可行的;所以,就不必采用Qt Creator进行开发了;
项目代码在github中:
https://github.com/ohenrygithub/QtGuiDLL/archive/master.zip
qtwinmigrate\examples\qtdll\qtdll.sln 在VS中可以打开这个;
在Qt中可以打开这个:qtwinmigrate\examples\qtdll\qtdll.pro
对应的C# Form程序,也上传到了github:
https://github.com/ohenrygithub/WForm/archive/master.zip
感兴趣的小伙伴,可以参考一下。