Results 1 to 12 of 12

Thread: Create dll/idl to use with visual studio 2010

  1. #1
    Join Date
    Jun 2012
    Posts
    14

    Default Create dll/idl to use with visual studio 2010

    I've created an application using Qt Creator and now I would like to use the functionalities within an C# visual studio solution. With this i want to create a library from my QT application and add this as a reference within my visual studio solution. I've realized a few things when searching the web for a solution.

    I would like to know what is the best way of doing this, I've tried creating a library file with the use of TEMPLATE = lib in my .pro file but as I relaize I then have to use the .h file and this is now possible as I in my visual studio use C# as language but my QT project is developed with c++.

    I need to create a COM interop dll as I understand it but all I can find online is either eight years old or done through visual studio itself.

    So what I want to do is to create a simple library file which I can use without the .h file, which I can add to my visual studio solution, without any GUI necessary. I'm only going to use the functions within the dll as help functions to my visual studio solution.

    If someone please can help me with a small tutorial or step by step guide this would really really help me?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Create dll/idl to use with visual studio 2010

    Google for loading unmanaged C++ DLL in C# - there are many tutorials on how to do it.
    This is not a Qt issue.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Create dll/idl to use with visual studio 2010

    ActiveQt can be used to create an in-process COM server which is, I assume, usable from .Net land.

  4. #4
    Join Date
    Jun 2012
    Posts
    14

    Default Re: Create dll/idl to use with visual studio 2010

    Thanks for your reply.
    What I'm looking for is how to create an Assembly or COM component from an QT Application?

    I realize that a com component need some kind of GUIID and a need to extract the functions from the application.
    This is what I got right now:


    Qt Code:
    1. #define APPTOLIB_EXPORT Q_DECL_EXPORT
    2. using namespace std;
    3. class APPTOLIB_EXPORT MyMathFuncs : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. Q_CLASSINFO("ClassID", "{b50a71db-c4a7-4551-8d14-49983566afee}")
    8. Q_CLASSINFO("InterfaceID", "{4a427759-16ef-4ed8-be79-59ffe5789042}")
    9. Q_CLASSINFO("RegisterObject", "yes")
    10.  
    11. public:
    12. MyMathFuncs(QObject* parent = 0);
    13. // Returns a + b
    14. static double Add(double a, double b);
    15.  
    16. // Returns a / b
    17. // Throws DivideByZeroException if b is 0
    18. static double Divide(double a, double b);
    19. };
    20.  
    21. MyMathFuncs::MyMathFuncs(QObject *parent) : QObject(parent)
    22. {
    23. setObjectName("From QAxFactory");
    24. }
    25.  
    26. double MyMathFuncs::Add(double a, double b)
    27. {
    28. return a + b;
    29. }
    30.  
    31. double MyMathFuncs::Divide(double a, double b)
    32. {
    33. if (b == 0)
    34. {
    35. throw new invalid_argument("b cannot be zero!");
    36. }
    37.  
    38. return a / b;
    39. }
    40.  
    41.  
    42. QAXFACTORY_BEGIN("{edd3e836-f537-4c6f-be7d-6014c155cc7a}", "{b7da3de8-83bb-4bbe-9ab7-99a05819e201}")
    43. QAXCLASS(MyMathFuncs)
    44. QAXFACTORY_END()
    To copy to clipboard, switch view to plain text mode 

    I can access the interface but I don't get up any functions. I've been able to extract seperate functions but when adding them to a class I only get an interface when adding the reference to my visual studio solution. I which to be able to create a new object of MyMathFuncs and then access the functions which I've made available.

    Maybe my title wasn't so well written but what I want is to use my QT c++ class by adding a dll as reference in Visual Studio C# where I can then get hold of the functions/classes which I have made extern.?

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Create dll/idl to use with visual studio 2010

    Quote Originally Posted by Sajjmon View Post
    I've created an application using Qt Creator and now I would like to use the functionalities within an C# visual studio solution. With this i want to create a library from my QT application and add this as a reference within my visual studio solution. I've realized a few things when searching the web for a solution.

    I would like to know what is the best way of doing this, I've tried creating a library file with the use of TEMPLATE = lib in my .pro file but as I relaize I then have to use the .h file and this is now possible as I in my visual studio use C# as language but my QT project is developed with c++.

    I need to create a COM interop dll as I understand it but all I can find online is either eight years old or done through visual studio itself.

    So what I want to do is to create a simple library file which I can use without the .h file, which I can add to my visual studio solution, without any GUI necessary. I'm only going to use the functions within the dll as help functions to my visual studio solution.

    If someone please can help me with a small tutorial or step by step guide this would really really help me?

    Thanks!
    you can use COM (yuck!), but p/invoke is simpler and easier for simple dlls with unmangled symbols (use external "C"). Search elsewhere though, this is definitely not Qt related.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Create dll/idl to use with visual studio 2010

    As someone who's been digging into COM for a few weeks, while it is one possible solution (as it'll be a layer between the two) it's... a lot of work.

    Further, it won't necessarily be compatible if you shift it somewhere else. In order to use COM you'll have to be adding (at least one) entry to the registry and accessing it at runtime. Unless you build a proper install/batchfile/something this won't be usable by another computer without manually editing the registry (or using your batch file that does it for you manually.)

    That said, COM will work, but it's the long road to success here. I imagine the tuts that high_flyer mentioned will be easier.



    COM Stuff:
    If you wish to do COM I'll suggest the book i've been going through: COM and ATL 3.0 by Andrew Troelson. It gives a good quick-and-dirty hands on of COM by first pretending to build COM like client/servers with C++, then moving into building actual COM objects by hand, then using ATL to actually use COM more efficiently. Most material you find on standard COM *WILL* be 8 years old or so. COM doesn't change over time! (An extremely important concept.) To expand COM they add new classes (either inheriting from old ones as a means to "update" them, or building entirely new classes.); I.E. COM+ adds a bunch of features by subclassing or adding new content. Standard COM usage hasn't and (AFAIK) won't change; so old material is gold aside from deprecated C++ headers and such. The book I mentioned happens to be at least 10 years old and I've had no issues compiling or getting the code to run just fine.

    Still, seems like a very complicated way to utilize a DLL between languages for something this small. COM is more for an official release that you want:
    --People to be truly abstracted from the implementation
    --People to be able to use several language types in a compatible way (i.e. a company building a large project with many different programmers specialized in different languages)

    A good example is a project I'm working on where I'm tearing a GUI away from it's COM layer and replacing it. By using COM I don't have to actually change anything but GUI code to update it. This is good because the language it's currently in is now unsupported so being able to recode only this portion of it leaves a usable product.

    Similarly as long as you conform to the contract COM allows you to update from the other direction. You could replace the entire codebase that controls your hardware beneath the GUI implementation, without ever touching the COM layer or the GUI layer.
    Last edited by tescrin; 30th July 2012 at 18:31.

  7. #7
    Join Date
    Jun 2012
    Posts
    14

    Default Re: Create dll/idl to use with visual studio 2010

    Quote Originally Posted by tescrin View Post
    As someone who's been digging into COM for a few weeks, while it is one possible solution (as it'll be a layer between the two) it's... a lot of work.

    Further, it won't necessarily be compatible if you shift it somewhere else. In order to use COM you'll have to be adding (at least one) entry to the registry and accessing it at runtime. Unless you build a proper install/batchfile/something this won't be usable by another computer without manually editing the registry (or using your batch file that does it for you manually.)

    That said, COM will work, but it's the long road to success here. I imagine the tuts that high_flyer mentioned will be easier.



    COM Stuff:
    If you wish to do COM I'll suggest the book i've been going through: COM and ATL 3.0 by Andrew Troelson. It gives a good quick-and-dirty hands on of COM by first pretending to build COM like client/servers with C++, then moving into building actual COM objects by hand, then using ATL to actually use COM more efficiently. Most material you find on standard COM *WILL* be 8 years old or so. COM doesn't change over time! (An extremely important concept.) To expand COM they add new classes (either inheriting from old ones as a means to "update" them, or building entirely new classes.); I.E. COM+ adds a bunch of features by subclassing or adding new content. Standard COM usage hasn't and (AFAIK) won't change; so old material is gold aside from deprecated C++ headers and such. The book I mentioned happens to be at least 10 years old and I've had no issues compiling or getting the code to run just fine.

    Still, seems like a very complicated way to utilize a DLL between languages for something this small. COM is more for an official release that you want:
    --People to be truly abstracted from the implementation
    --People to be able to use several language types in a compatible way (i.e. a company building a large project with many different programmers specialized in different languages)

    A good example is a project I'm working on where I'm tearing a GUI away from it's COM layer and replacing it. By using COM I don't have to actually change anything but GUI code to update it. This is good because the language it's currently in is now unsupported so being able to recode only this portion of it leaves a usable product.

    Similarly as long as you conform to the contract COM allows you to update from the other direction. You could replace the entire codebase that controls your hardware beneath the GUI implementation, without ever touching the COM layer or the GUI layer.
    Thanks for the information. The actual project which I'm going to be using is actual much larger, the small class above was just an example I'm working with so I'll get started.

    I've tried to use the unmangled dll import and it seems to work, so long that I don't use any QT code. I'm gonna keep on that track I think because as you said, the COM way seems like it's alot of extra work, would be nice though. Thanks everyone!

  8. #8
    Join Date
    Jun 2012
    Posts
    14

    Default Re: Create dll/idl to use with visual studio 2010

    I've now been able to create a COM component out of my smalll project and can access one specified method. The problem is now that when I run my dll through an C# Console Application in Visual Studio I get this error message:
    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

    Additional information: Retrieving the COM class factory for component with CLSID {FA11D634-3F6D-4DFF-8FE7-7528A0373411} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

    What I have now is:

    main.cpp
    Qt Code:
    1. #include "ItShouldWork_global.h"
    2. #include <QObject>
    3. #include <QAxFactory>
    4. #include <QAxBindable>
    5. class ITSHOULDWORKSHARED_EXPORT ItShouldWork : public QObject
    6. {
    7. Q_OBJECT
    8. Q_CLASSINFO("ClassID", "{fa11d634-3f6d-4dff-8fe7-7528a0373411}")
    9. Q_CLASSINFO("InterfaceID", "{1a41b926-eaec-4beb-9f49-4a914eebc6e1}")
    10. Q_CLASSINFO("RegisterObject", "yes")
    11.  
    12. Q_PROPERTY(QString id READ id)
    13.  
    14. public:
    15. ItShouldWork(QObject* parent = 0);
    16. QString test(QString a, QString b);
    17.  
    18. QString id() const { return objectName(); }
    19. };
    20.  
    21. ItShouldWork::ItShouldWork(QObject* parent) : QObject(parent)
    22. {
    23. }
    24.  
    25. QString ItShouldWork::test(QString a, QString b)
    26. {
    27. return a + " - " + b;
    28. }
    29.  
    30. #include "main.moc"
    31.  
    32. QAXFACTORY_BEGIN("{f9e2e91a-ca63-4e5a-85b7-5bb65b8ee358}", "{90873f03-a81a-4007-8652-b9c134e72126}")
    33. QAXCLASS(ItShouldWork)
    34. QAXFACTORY_END()
    To copy to clipboard, switch view to plain text mode 

    and then when trying to instantiate it in my c# Console application:
    Qt Code:
    1. Console.WriteLine("Testing: ");
    2.  
    3. itshouldworkLib.ItShouldWork it = new itshouldworkLib.ItShouldWork();
    4.  
    5. Console.WriteLine("Worked id: " + it.id);
    To copy to clipboard, switch view to plain text mode 

    This stops the program and shows the error message above, any ideas?

  9. #9
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Create dll/idl to use with visual studio 2010

    This isn't exactly a COM forum :P, but...

    were I to guess I'd say that in your driver (your main function or whatever you're actually trying to use the ClassFactory from) you're probably missing the "CoInitialize(null)" statement. I recently forgot to add that in and had my HRESULT on classfactory returning failure; hence my guess.

    If not that, I'll look over it again, but I'm no expert by any means.

    EDIT:
    Congrats on your quick work in COM! Kind of a cool concept/programming style when you learn about it.

  10. #10
    Join Date
    Jun 2012
    Posts
    14

    Default Re: Create dll/idl to use with visual studio 2010

    Quote Originally Posted by tescrin View Post
    This isn't exactly a COM forum :P, but...

    were I to guess I'd say that in your driver (your main function or whatever you're actually trying to use the ClassFactory from) you're probably missing the "CoInitialize(null)" statement. I recently forgot to add that in and had my HRESULT on classfactory returning failure; hence my guess.

    If not that, I'll look over it again, but I'm no expert by any means.

    EDIT:
    Congrats on your quick work in COM! Kind of a cool concept/programming style when you learn about it.
    Yes I know and I'm sorry about that. Actually I created the COM dll using QAxFactory so its not completely out of order...:P

    Thanks, but I can't say I really know everything I did. The classFactory is created through QAxFactory as I understand it and I wonder if the problems lies in how it creates the COM component. I've searched around for the error message but no solution out there seems to help me... I tried using tlbimp.exe to create an assembly from it but the same message appears again...
    I'll try to see how the CoInitialize method works, maybe you can give me an example of how to implement it with a corresponding COM method?

    I'm very thankful for you taking your time and helping me, the book you linked seems interesting, maybe take a look on buying that later.. Would be nice to get this working soon!

  11. #11
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Create dll/idl to use with visual studio 2010

    Er, well so far the stuff I've done is building COM objects from scratch.

    When you call ClassFactory in C++ you have to call CoInitialize(NULL) sometime before that point; it initializes the "COM Subsystem." Every thread using COM libraries has to call it before asking the COM for anything.

    I'm not 100% sure, but I'm thinking you just need to start off your main thread (whichever is using QAxFactory) with the statement
    Qt Code:
    1. CoInitialize(NULL);
    2. //...stuff
    3. CoUninitialize(); //be nice to the poor COM subsystem :)
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jun 2012
    Posts
    14

    Default Re: Create dll/idl to use with visual studio 2010

    Thanks, I'll take a look at that.
    Seems like QAxFactory does all the underlying work with COM registration/initialization and so on but I'm not completely sure, it actually just should work if you read the QT docs about ActiveQT, at least that's how i understand it.

    The error I get seem to have something with security permissions issue or something, tried the solutions some people gave but nothing worked.. wondering if I should go over to C++/CLI instead...

    Thanks for all your help anyway!

Similar Threads

  1. [QT] - Include DLL in Visual Studio 2010
    By vinzz38 in forum Qt Programming
    Replies: 4
    Last Post: 3rd April 2012, 16:44
  2. Visual Studio 2010 Add-In - unable to create project
    By garaddon in forum Installation and Deployment
    Replies: 2
    Last Post: 1st January 2011, 11:40
  3. Qt and Visual Studio 2010
    By SixDegrees in forum Qt Programming
    Replies: 6
    Last Post: 15th November 2010, 19:56
  4. Visual Studio Plugin (1.1.6) crashes Visual Studio (2010)
    By mboeni in forum Installation and Deployment
    Replies: 0
    Last Post: 11th October 2010, 16:46
  5. Plan for Visual Studio 2010 (MSVC 2010)?
    By Vinzz in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2010, 17:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.