How to Use QT to write DLL programs in Windows

Source: Internet
Author: User
Tags win32 window

Copyright Notice

Respect Original Works. Reprinted, please maintain the integrity of the article, and in the form of a hyperlink to indicate the original author "tingsking18" and the main site address, so that other friends can ask and correct.

 

How to Use QT to write DLL programs in Windows

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> <br/> # include <qtwinmigrate/qwinwidget. h> <br/> # include <qmessagebox. h> <br/> # include <windows. h> <br/> bool winapi dllmain (hinstance, DWORD dwreason, lpvoid lpvreserved) <br/>{< br/> static bool ownapplication = false; <br/> If (dwreason = dll_process_attach) <br/> ownapplication = qmfcapp: plugininstance (hinstance); <br/> If (dwreason = dll_process_detach & ownapplication) <br/> Delete qapp; <br/> return true; <br/>}</P> <p>

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) <br/>{< br/> If (qapp) <br/> return false; <br/> qt_wa ({<br/> hhook = setwindowshookexw (wh_getmessage, qtfilterproc, 0, getcurrentthreadid (); <br/> }, {<br/> hhook = setwindowshookexa (wh_getmessage, qtfilterproc, 0, getcurrentthreadid (); <br/>}); <br/> int argc = 0; <br/> (void) New qapplication (argc, 0); <br/> If (plugin) {<br/> char filename [256]; <br/> If (getmodulefilenamea (hinstance) Plugin in, filename, 255) <br/> loadlibrarya (filename ); <br/>}</P> <p> return true; <br/>}< br/>

 

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) <br/>{< br/> qwinwidget win (parent); <br/> win. showcentered (); <br/> qmessagebox: About (& Win, "about qtmfc", "qtmfc version 1.0/ncopyright (c) 2003 "); </P> <p> return true; <br/>}< br/>

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)

 

You must add this sentence to the *. Pro file during compilation! Remember, remember!

 

Reference: http://doc.trolltech.com/solutions/qtwinmigrate/winmigrate-qt-dll-example.html

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.