Because QT must call the exec method of qapplication to generate a message loop and the QT program can run. Therefore, if we use QT to write a DLL program, it cannot be called in common Windows programs. An error occurs during the call. Of course, QT provides a solution: qtwinmigrate
Here is an introduction to qtwinmigrate on the official QT Website:
Http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Windows/qtwinmigrate
The following describes how to use qtwinmigrate to compile a DLL.
First, we need to rewrite the dllmain function:
# Include <qtwinmigrate/qmfcapp. h>
# Include <qtwinmigrate/qwinwidget. h>
# Include <qmessagebox. h>
# Include <windows. h>
Bool winapi dllmain (hinstance, DWORD dwreason, lpvoid lpvreserved)
{
Static bool ownapplication = false;
If (dwreason = dll_process_attach)
Ownapplication = qmfcapp: plugininstance (hinstance );
If (dwreason = dll_process_detach & ownapplication)
Delete qapp;
Return true;
}
We all know that the dllmain function is the entry function of the Windows dynamic library. If you use the qt ui in the DLL, you must first create a global qapplication and the application must create an eventloop.
Go to the qmfcapp: plugininstance method,
Bool qmfcapp: plugininstance (QT: handle plugin)
{
If (qapp)
Return false;
Qt_wa ({
Hhook = setwindowshookexw (wh_getmessage, qtfilterproc, 0, getcurrentthreadid ());
},{
Hhook = setwindowshookexa (wh_getmessage, qtfilterproc, 0, getcurrentthreadid ());
});
Int argc = 0;
(Void) New qapplication (argc, 0 );
If (plugin ){
Char filename [256];
If (getmodulefilenamea (hinstance) plugin, filename, 255 ))
Loadlibrarya (filename );
}
Return true;
}
We can see that the qapplication is created. Qmfcapp: plugininstanc is used to ensure that a qapplication object exists in the process, and the DLL needs to load the qapplication instance to the memory.
Below are the export functions in dll:
Extern "C" _ declspec (dllexport) bool showdialog (hwnd parent)
{
Qwinwidget win (parent );
Win. showcentered ();
Qmessagebox: About (& Win, "About qtmfc", "qtmfc version 1.0/ncopyright (c) 2003 ″);
Return true;
}
The export function in dll must be in the form of extern "C", and qwinwidget provides a stack for the Native WIN32 window.
The program has not been written. No. You can use this program.
Qmake-Project
Qmake
Nmake
In this way, it cannot be compiled in any way.
If you carefully read the examples of qtwinmigrate, you will notice:
Include (D:/qt4.4.3/qtwinmigrate-2.8-opensource/src/qtwinmigrate. PRI)
During compilation, you must add it to the *. Pro file.
For more information about QT, click here.
Post from: http://www.meegox.com/1018.html