I need some help now. I would love if someone could help we with “transferring” my QT based code to work with my C# application.
Lets say I have this simple QT Class:
class ItShouldWork : public QObject
{
Q_OBJECT
public:
ItShouldWork(QObject* parent = 0) : QObject(parent){}
QString id() const { return objectName(); }
};
Now I want to be able to access this class with Visual Studio C#, I’ve tried creating an unmanaged dll and access through an wrapper, tried to create an COM component but right now I’m completely stuck so I’m going back to the beginning with this simple class. I’ve seen that you can use an extern “C” with __declspec(dllexport) like this:
extern "C" __declspec(dllexport) double Subtract(double a, double b)
and then use _dllImport in .net, this works fine with the function above but when adding for example a QString to it it doesn’t recognize my dll anymore and a dllNotFoundException is thrown.
Then I thought maybe you need a wrapper of some kind, yeah and how do you do that then..? googled away but nothing to really help me with c++ and qt functions.
A small step by step on how creating a dll and a c-wrapper or creating a COM component dll directly would be awesome, I don’t really care how it’s done but if you know a way, please help me with a small tutorial? I’m going crazy…
C++/CLI is the way to go, I think.
See: Wrapping unmanaged C++ with C++/CLI – a proper approach
And especially: http://www.delmarlearning.com/companions/content/1592009638/bonus/009638_BonusCh02.pdf
Going into that subject in depth is not easy to do in a forum post. But with some research you should be able to create something useable quite quickly.
C++/CLI takes some getting used to, if you’re coming from C/C++. It’s a bit more restrictive, especially regarding mixing managed and unmanaged types. But the literature will guide you there.
EDIT:
In response to OP’s comment to my answer(also see Comment by Ramhound, Me), here is an example I got here:
Have a look at the content behind the link to see the context of m_impl…
EDIT 2:
Also depending on the desired depth/complexity/power of your wrapper, you might think about wrapping your classes in C++/CLI “ref class”es instead of using DllImport.