2

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...

Simon Nielsen
  • 105
  • 1
  • 13
  • `QString` is unique to the `QT` framework. You would have to write w wrapper close to convert the `QString` class to a normal `String` – Security Hound Jul 31 '12 at 16:43
  • @Rambound Can you help we with how to do that? The wrapper I mean, how to convert a QString to char* shouldn't be a problem.. – Simon Nielsen Aug 01 '12 at 11:46

3 Answers3

2

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...

String ^ CSimpleObjectWrapper::ToString(void) 
{ 
    wchar_t szStr[100]; 
    HRESULT hr = m_impl->ToString(szStr, ARRAYSIZE(szStr)); 
    if (FAILED(hr)) 
    { 
        Marshal::ThrowExceptionForHR(hr); 
    } 
    // Marshal PWSTR to System::String and return it. 
    return gcnew String(szStr); 
} 

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.

Community
  • 1
  • 1
mw_21
  • 150
  • 5
  • I looked at your links and I think I understand a little bit more about how these things work. If I go with the C++/CLI solution I went in to another problem, it's how __declspec(dllexport) works together with QT variables, for example this function: __declspec(dllexport) QString getId(); When trying to access it through DllImport it just says dllNotFoundException all the time, and then when I remove that and only have ordinary C-functions it is found as it should. – Simon Nielsen Aug 01 '12 at 11:38
  • Well, assuming that " __declspec(dllexport) QString getId();" is a function wrapper, that you're exporting: In my opinion you'd have to convert the QString to another string type in your function wrapper. Otherwise you'll run into problems like the one you mentioned, as well as (probably) Marshalling Hell :) – mw_21 Aug 01 '12 at 13:00
  • Please note that c++/CLI is not supported in mono, and therefore any solution using it is not cross platform. – trampster Oct 10 '13 at 20:13
1

You can create wrapper on Managed C++. Then you will be able to use managed classes in c#. Check this, it might help you http://msdn.microsoft.com/en-us/library/ms235638.aspx

Uriil
  • 11,948
  • 11
  • 47
  • 68
  • 2
    @tobsen well, the abbreviated expr. managed C++ stands for "Managed Extensions for C++". That's definitely deprecated, see [here, first sentence](http://en.wikipedia.org/wiki/Managed_Extensions_for_C%2B%2B) and [also here, removed support for VS](http://msdn.microsoft.com/en-us/library/b23b94s7(v=vs.90).aspx) for example. The current standard is C++/CLI which is also ECMA ratified. Also see [Bjarne Stroustup's Website](http://www.stroustrup.com/bs_faq.html#CppCLI). – mw_21 Aug 01 '12 at 16:30
1

Take a look at http://doc.qt.nokia.com/4.7-snapshot/activeqt-dotnet.html. It has some information on using ActiveQT, which per the website:

Qt's ActiveX and COM support allows Qt for Windows developers to: Access and use ActiveX controls and COM objects provided by any ActiveX server in their Qt applications. Make their Qt applications available as COM servers, with any number of Qt objects and widgets as COM objects and ActiveX controls.

The page also has examples on how to integrate with .Net.

Robert H
  • 11,520
  • 18
  • 68
  • 110
  • Thanks, this page helped me alot and I've now successfully created a com component. – Simon Nielsen Aug 07 '12 at 07:35
  • @SimonNielsen Glad it helped. If any of these posts have answered your question please mark it as answered so the next person with the same issue can find the appropriate answer. It is also good etiquette to upvote any answers that provide helpful information or alternative answers to your question. – Robert H Aug 07 '12 at 11:18