Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Qt C++ Lib usable in C#?
QtWS25 Last Chance

[SOLVED] Qt C++ Lib usable in C#?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 8 Posters 7.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jnewing
    wrote on last edited by SGaist
    #1

    -I need some help and/or direction. I just wanted to see if I could make a .dll in Qt and have it work in C# via [DllImpor("blah.dll")] etc...- SOLVED

    test_global.h

    #ifndef TEST_GLOBAL_H
    #define TEST_GLOBAL_H
    
    #include <QtCore/qglobal.h>
    
    #if defined(TEST_LIBRARY)
    #  define TESTSHARED_EXPORT Q_DECL_EXPORT
    #else
    #  define TESTSHARED_EXPORT Q_DECL_IMPORT
    #endif
    
    #endif
    

    test.h

    #ifndef TEST_H
    #define TEST_H
    
    #include <stdio.h>
    #include <iostream>
    #include "test_global.h"
    
    using namespace std;
    
    class TESTSHARED_EXPORT Test
    {
    public:
        Test();
        static std::string echotest(std::string userstring);
        static std::string simpletest();
    };
    
    #endif
    

    test.cpp

    #include "test.h"
    
    Test::Test()
    {
        // nope
    }
    
    std::string Test::echotest(std::string userstring)
    {
        return userstring;    
    }
    
    std::string Test::simpletest()
    {
        return "f00";
    }
    

    Solution I found after a bit of reading was to create a wrapper using C++/CLI works like a charm, simple and fast to do as well.

    Then to use in C# simple ref. the compiled .dll and use as follows:

    some c# stuffs

    using (var testdll = new TestNet())
    {
        // test 1
        string test1_result = testdll.test1("some string");
    
        // test 2
        string test2_result = testdll.test2();
    }
    

    *testnet.h"

    #include "test.h"
    #include <string>
    #include <iostream>
    #using <mscorlib.dll>
    
    using namespace std;
    using namespace System;
    using namespace System::Runtime::InteropServices;
    
    public ref class TestNET
    {
    private:
        Test *m_Test;   // this is not really needed in this case but included for example
    
    public:    
        TestNET();
        ~TestNET();
    
        System::String^ test1(System::String^ userstr);
        System::String^ test2();
    };
    
    #endif
    

    testnet.cpp

    #include "testnet.h"
    
    TestNET::TestNET()
    {
        m_Test = new Test();
    }
    
    AuthimNET::~AuthimNET()
    {
        delete m_Test;
    }
    
    System::String^ TestNET::test1(System::String^ userstr)
    {
        return userstr;
    }
    
    System::String^ TestNET::test2()
    {
        return gcnew System::String("test no. 2");
    }```
    1 Reply Last reply
    0
    • H Offline
      H Offline
      hardcodes.de
      wrote on last edited by
      #2

      Never did it for myself but AFAIK it's about "exporting" the functions. Maybe you even need C wrappers for your methods because Windows doesn't know anything about the internal object model or namespacing.

      The following KBs/MSDN articles could give new inspiration:

      "http://support.microsoft.com/kb/106553":http://support.microsoft.com/kb/106553
      "http://support.microsoft.com/kb/88278":http://support.microsoft.com/kb/88278
      "http://msdn.microsoft.com/en-us/library/d91k01sh(v=vs.80).aspx":http://msdn.microsoft.com/en-us/library/d91k01sh(v=vs.80).aspx

      while(!sleep){++sheep;}

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        IMO, what you need is wrapping your C++ API with a C Api.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pkj__
          wrote on last edited by
          #4

          First a warning:

          • Try taking up a different task or just avoid this idea.
          • If not possible, use up your leaves.
          • If you must do this, try taking up a different job. For what you will be entailing is in no way be beneficial to your knowledge base.
            But if you insist. All that you need to know about pinvoke is "here":http://www.pinvoke.net
          1 Reply Last reply
          0
          • J Offline
            J Offline
            jnewing
            wrote on last edited by
            #5

            [quote author="hardcodes.de" date="1347952183"]Never did it for myself but AFAIK it's about "exporting" the functions. Maybe you even need C wrappers for your methods because Windows doesn't know anything about the internal object model or namespacing.

            The following KBs/MSDN articles could give new inspiration:

            "http://support.microsoft.com/kb/106553":http://support.microsoft.com/kb/106553
            "http://support.microsoft.com/kb/88278":http://support.microsoft.com/kb/88278
            "http://msdn.microsoft.com/en-us/library/d91k01sh(v=vs.80).aspx":http://msdn.microsoft.com/en-us/library/d91k01sh(v=vs.80).aspx[/quote]

            Thanks reading was great! It lead me to C++/CLI

            [quote author="1+1=2" date="1347953705"]IMO, what you need is wrapping your C++ API with a C Api.[/quote]

            Thanks for this tip I wrapped it!

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pwnstar23
              wrote on last edited by
              #6

              Read up on msdn about dll export and import.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                eastken
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                -1
                • M Offline
                  M Offline
                  mcosta
                  wrote on last edited by
                  #8

                  Hi and welcome to devnet,

                  I suggest to open a new thread with your question; this one is very old and marked as solved so people cannot know there is a new question inside

                  Once your problem is solved don't forget to:

                  • Mark the thread as SOLVED using the Topic Tool menu
                  • Vote up the answer(s) that helped you to solve the issue

                  You can embed images using (http://imgur.com/) or (http://postimage.org/)

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @mcosta

                    Looks more like an advertisement post

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved