Qt程序中调用C#编写的dll(推荐)

  #include "mainwindow.h"

  #include "ui_mainwindow.h"

  #include

  #include

  typedef int (*x_add)(int a , int b);

  typedef void (*x_substract)(int a , int b , int* c);

  typedef void (*x_showBox)(const char* content);

  MainWindow::MainWindow(QWidget *parent) :

  QMainWindow(parent),

  ui(new Ui::MainWindow)

  {

  ui->setupUi(this);

  }

  MainWindow::~MainWindow()

  {

  delete ui;

  }

  //add

  void MainWindow::on_pushButton_clicked()

  {

  int a = ui->lineEdit->text().toInt();

  int b = ui->lineEdit_2->text().toInt();

  QLibrary library("http://www.jb51.net/article/CppDll.dll");

  if(library.load()){

  x_add add = (x_add)library.resolve("?add@@YAHHH@Z");

  if(add){

  QString str = QString::number(add(a , b));

  QMessageBox::information(this , "call add from dll" , str);

  }

  }

  }

  //sub

  void MainWindow::on_pushButton_2_clicked()

  {

  int a = ui->lineEdit_3->text().toInt();

  int b = ui->lineEdit_4->text().toInt();

  int c = 0;

  QLibrary library("http://www.jb51.net/article/CppDll.dll");

  if(library.load()){

  x_substract sub = (x_substract)library.resolve("?substract@@YAXHHPAH@Z");

  if(sub){

  sub(a , b , &c);

  QString str = QString::number(c);

  QMessageBox::information(this , "call sub from dll" , str);

  }

  }

  }

  //showBox

  void MainWindow::on_pushButton_3_clicked()

  {

  QLibrary library("http://www.jb51.net/article/CppDll.dll");

  if(library.load()){

  x_showBox showBox = (x_showBox)library.resolve("?showBox@@YAXPBD@Z");

  if(showBox){

  showBox("showBox!");

  }

  }

  }