[SOLVED] Qt C++ Lib usable in C#?
-
-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"); }```
-
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 -
IMO, what you need is wrapping your C++ API with a C Api.
-
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
-
[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!