How to use QT C++ library in C#

Posted on 2020-07-28 by Burak Hamdi TUFAN
General Programming
How to use QT C++ library in C#
Hello everyone, in this article we are going to talk about how can we use the libraries which created with QT C++ in our C# applications with Interop Services on an example.

Let's begin.

Firstly we are going to create a library with QT C++. Below image you can see the how to create a library project in QT. QT Create A C++ Library
And do not forget to select the Shared Library for our library like below image. Select the Shared Library in Qt

Now let's write our codes in our library. In here we are going to write some basic mathematical calculations. Below code blocks you will see them.

qtlibraryexample.h

#include "qtlibraryexample.h"
#include 

QtLibraryExample::QtLibraryExample()
{
}

extern "C" double add(int a, int b)
{
    return a + b;
}

extern "C" double subtrack(int a, int b)
{
    return a - b;
}

extern "C" double multiply(int a, int b)
{
    return a * b;
}

extern "C" QTLIBRARYEXAMPLESHARED_EXPORT double divide(int a, int b)
{
    return a / b;
}
qtlibraryexample_global.h

#ifndef QTLIBRARYEXAMPLE_GLOBAL_H
#define QTLIBRARYEXAMPLE_GLOBAL_H

#include 

#if defined(QTLIBRARYEXAMPLE_LIBRARY)
#  define QTLIBRARYEXAMPLESHARED_EXPORT Q_DECL_EXPORT
#else
#  define QTLIBRARYEXAMPLESHARED_EXPORT Q_DECL_IMPORT
#endif

extern "C" __declspec(dllexport) double add(int a, int b);
extern "C" __declspec(dllexport) double subtrack(int a, int b);
extern "C" __declspec(dllexport) double multiply(int a, int b);
extern "C" __declspec(dllexport) double divide(int a, int b);

#endif // QTLIBRARYEXAMPLE_GLOBAL_H
And lastly Build the project. Do not try to run this project just Build it to create dll files.
Also do not forget our library is 32bit. We are going to need this information in our C# program. Below image you can see the QT library compiler is compiling 32 bit. Qt Create library as 32bit library
We will activate the 32bit api usage in Visual Studio project properties.
Now we creat a C# application in MS Visual Studio.
First we are going to include Interop Services in our C# project.

using System.Runtime.InteropServices;
And then we are going to define the prototype methods which we defined in our C++ library. These methods must be same as the defined in C++ library.
And then we are going to create the function which will call these methods.

[DllImport("QtLibraryExample.dll")]
static extern double add(int a, int b);

[DllImport("QtLibraryExample.dll")]
static extern double subtrack(int a, int b);

[DllImport("QtLibraryExample.dll")]
static extern double multiply(int a, int b);

[DllImport("QtLibraryExample.dll")]
static extern  double divide(int a, int b);

static void callQtFunctions()
{
    Console.WriteLine("Result of 5 + 7 is " + add(5, 7).ToString());
    Console.WriteLine("Result of 21 - 10 is " + subtrack(21, 10).ToString());
    Console.WriteLine("Result of 9 x 3 is " + multiply(9, 3).ToString());
    Console.WriteLine("Result of 70 / 5 is " + divide(70, 5).ToString());
}
All we have to do call this method in main function.

static void Main(string[] args)
{
    Console.Title = "Using QT Libraries in C# Aplication Example - TheCodeProgram";
    callQtFunctions();
    Console.ReadLine();
}
Now I copied all of QT library files into visual studio executable file folder. This is important.
Also set the project as Release mode and 32-bit. Because our library has been built as 32bit as showed above. Below image you can see the 32bit building of Visual Studio project property.
Visual Studio Prefer 32bit
Below image you can see the project output. QT library project for C# program example output

That is all in this article.

You can reach the example application on Github via : https://github.com/thecodeprogram/QtLibraryForCSharpApplication

Have a good managing unmanaged codes.

Burak Hamdi TUFAN.


Tags
Share this Post
Send with Whatsapp