Create a shared Dll to be used in a vb.net or c# application
-
I am trying to find out how to use ActiveQt to create a in-process dll which can be used in a vb.net or c# or vb6 application. Somehow I am totally confused with the documentation that i have read and hence thought of asking the question on the forum to the experts.
My environment is qt5.7.1, Visual Studio 2015 Community on Windows 7.
The shared testing library created, source code of files:
#------------------------------------------------- # # Project created by QtCreator 2017-02-04T19:10:10 # #------------------------------------------------- QT -= gui TARGET = testlibrary TEMPLATE = lib DEFINES += TESTLIBRARY_LIBRARY # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += testlibrary.cpp HEADERS += testlibrary.h\ testlibrary_global.h unix { target.path = /usr/lib INSTALLS += target }
the source code for testlibrary.h
#ifndef TESTLIBRARY_H #define TESTLIBRARY_H #include "testlibrary_global.h" class TESTLIBRARYSHARED_EXPORT Testlibrary { public: Testlibrary(); qreal getSum(qreal val1,qreal val2); private: qreal myres; }; #endif // TESTLIBRARY_H
The source code for testlibrary.cpp
#include "testlibrary.h" Testlibrary::Testlibrary() { } qreal Testlibrary::getSum(qreal val1, qreal val2) { return val1 + val2; }
And another file created by the creator.. testlibrary_global.h
#ifndef TESTLIBRARY_GLOBAL_H #define TESTLIBRARY_GLOBAL_H #include <QtCore/qglobal.h> #if defined(TESTLIBRARY_LIBRARY) # define TESTLIBRARYSHARED_EXPORT Q_DECL_EXPORT #else # define TESTLIBRARYSHARED_EXPORT Q_DECL_IMPORT #endif #endif // TESTLIBRARY_GLOBAL_H
Now I want to have this same testlibrary compiled as a DLL which can be included as a Reference in the VB.net or vb6 application and so that I can access the functions and events from a VB.net or C# or vb6 application.
Can someone let me know the right way to do this?
-
Thanks for this tip.
I compiled the hierarchy application as a release dll.
But when i try to include the dll as a Reference to the VB.NET project, it gives an error
"A reference to .... dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.Am I doing the right thing for including the DLL in VB.net or what is the right way?
-
Hi, I just tried, this way:
-
Compile to release (You did this already)
-
Go to the directory where the hierarchyax.dll is, in my case it is:
cd C:\Qt\Examples\Qt-5.8\activeqt\build-hierarchy-Desktop_Qt_5_8_0_MSVC2015_32bit-R
elease\release> -
Use windeployqt to copy all the needed Qt dlls into that directory:
C:\Qt\Examples\Qt-5.8\activeqt\build-hierarchy-Desktop_Qt_5_8_0_MSVC2015_32bit-R
elease\release>\qt\5.8\msvc2015\bin\windeployqt hierarchyax.dll -
Register the .dll as a COM dll in Windows:
C:\Qt\Examples\Qt-5.8\activeqt\build-hierarchy-Desktop_Qt_5_8_0_MSVC2015_32bit-R
elease\release>regsvr32 hierarchyax.dll -
Start Visual Studio, create a new VB.net project, add the .dll as a COM reference:
-
Try some simple code, like this:
-
You should see Qt's window in the upper left corner:
Good luck!
EDIT: For some reason my 3 pictures are not visible any more in Firefox, use Chrome or Safari... -
-
Hello hskoglund
I got stuck up at the step 4 of registering the .dll as a COM dll in Windows.
I get the typical error for which you have written the blog. "Could not find the platform plugin "windows" in "".
I tried adding the qt.conf file, verified that all the files are copied in the directory, again copied the qwindows.dll in the location, but nothing seems to work.
This is the directory listing with the files.
02/05/2017 07:08 PM <DIR> .
02/05/2017 07:08 PM <DIR> ..
03/11/2014 04:24 PM 4,173,928 D3Dcompiler_47.dll
02/04/2017 09:18 PM 968 hierarchy.res
02/04/2017 09:18 PM 245,248 hierarchyax.dll
02/04/2017 09:18 PM 1,304 hierarchyax.exp
02/04/2017 09:18 PM 6,140 hierarchyax.idl
02/04/2017 09:18 PM 1,580 hierarchyax.lib
02/04/2017 09:18 PM 8,060 hierarchyax.tlb
02/05/2017 06:32 PM <DIR> iconengines
02/05/2017 06:32 PM <DIR> imageformats
12/01/2016 04:01 PM 15,360 libEGL.dll
12/01/2016 04:01 PM 2,546,176 libGLESV2.dll
02/04/2017 09:18 PM 137,058 main.obj
02/04/2017 09:18 PM 8,467 moc_objects.cpp
02/04/2017 09:18 PM 8,645 moc_objects.obj
10/04/2013 11:58 PM 660,128 msvcp120.dll
10/04/2013 11:58 PM 963,232 msvcr120.dll
02/04/2017 09:18 PM 33,222 objects.obj
09/25/2014 04:58 PM 17,593,856 opengl32sw.dll
02/05/2017 06:32 PM <DIR> platforms
02/05/2017 07:08 PM 20 qt.conf
02/05/2017 06:32 PM 5,645,312 Qt5Core.dll
12/01/2016 04:08 PM 5,937,152 Qt5Gui.dll
12/01/2016 06:47 PM 317,440 Qt5Svg.dll
12/01/2016 04:13 PM 5,518,848 Qt5Widgets.dll
02/05/2017 06:32 PM <DIR> translationsWhat could be the point being missed?
-
Hi. I see you're using MSVC2013 64-bit version, I always have problems with using regsvr32 on a 64-bit .dll :-( For example see this stackoverflow discussion)
Try recompiling the hierarchy example with MSVC2013 32-bit version. Also a qt.conf file shouldn't be needed. If you run into problems with regsvr32, you could try debugging it with ``SET QT_DEBUG_PLUGINS=1```, like this:
C:\Qt\Examples\Qt-5.8\activeqt\build-hierarchy-Desktop_Qt_5_8_0_MSVC2015_32bit-R elease\release>SET QT_DEBUG_PLUGINS=1 C:\Qt\Examples\Qt-5.8\activeqt\build-hierarchy-Desktop_Qt_5_8_0_MSVC2015_32bit-R elease\release>dbgview C:\Qt\Examples\Qt-5.8\activeqt\build-hierarchy-Desktop_Qt_5_8_0_MSVC2015_32bit-R elease\release>regsvr32 hierarchyax.dll
(To see the output download DbgView from here)
-
Hello
I tried the debugging. I got an output as shown here.
[1356] QFactoryLoader::QFactoryLoader() checking directory path "C:/Windows/system32/platforms" ...
[1356] This application failed to start because it could not find or load the Qt platform plugin "windows"
[1356] in "".
[1356]
[1356] Reinstalling the application may fix this problem.Does it mean that it is searching for the platform plugins in the windows/system32 directory?
I copied the platforms directory in the system32 directory and wow, it registered the dll.
what is the way to avoid this coping of platforms directory? -
And even after the regsvr32 success, i still cannot see the reference in visual basic.
-
Hello hskoglund
So finally after a lot of trial and errors the hierarchy example worked for me only after i downloaded the Qt5.8 for MSVC15 32bit version.
When the dll was created.. all the steps happened just the way you had shown earlier.Thanks for your help.