function functions in the QT call DLL

Source: Internet
Author: User

Disclaimer: in advance I have already written a simple DLL file (MyDLL.dll), the C version of the interface. And using my first two articles about DLL article, the import library (. lib) file is exported from the DLL, and there are two functions in the DLL, the prototype is as follows:

void HelloWorld (); function call Win32 API, function is to pop up a HelloWorld prompt box
int add (int a,int b); Implements two number additions and returns the result

The following are two methods of displaying calls and implicit calls to simulate how QT calls function functions in external DLL files, follow me ....

Pre-Knowledge:

1, if you do not import the library file (. lib), and only the header file (. h) and the dynamic link library (. dll), we need to display the call, if the three files are all, we can use simple and convenient implicit invocation.

2, usually windows under the program to show the call DLL is divided into three steps (three functions): LoadLibrary (), getprocadress (), FreeLibrary ()

where the LoadLibrary () function is used to load the specified DLL file into the calling program's memory (DLL does not have its own memory!). )

The GetProcAddress () function retrieves the address of the output library function in the specified dynamic-link library (DLL) for invocation

FreeLibrary () releases the space occupied by the DLL

1. Display Call

QT provides a qlibrary class for display calls. A complete example is given below:

1 #include <QApplication>
2 #include <QLibrary>
3 #include <QDebug>
4 #include <QMessageBox>
5 #include "dll.h"//Introduction header file
6 typedef int (*fun) (int,int); Define a function pointer in case you call
7 int main (int argc,char **argv)
8 {
9Qapplication app (ARGC,ARGV);
TenQlibrary mylib ("MyDLL.dll"); DLL file used by the Declaration
Oneint result;
Aif (Mylib.load ())//Determine if the load is correct
-{
-Qmessagebox::information (NULL, "OK", "DLL load is ok!");
open= (Fun) mylib.resolve ("Add"); Invoke the Add () function
if (open)//is successfully connected on the Add () function
17 {
Qmessagebox::information (NULL, "OK", "Link to Function is ok!");
+Result=open (5,6); Here the function pointer calls the Add () function in the DLL
Qdebug () <<result;
21}
All else
Qmessagebox::information (NULL, "NO", "Linke to Function are not OK!!!!");
24}
-Else
Qmessagebox::information (NULL, "NO", "DLL is not loaded!");

return 0; Load fails to exit

28}

MyDLL.dll is a custom DLL file that can be called when it is copied to the output directory of the program. Obviously, it is inconvenient to write a large amount of code.

2. Implicit invocation

At this point we need three files, a header file (. h), a guided storage file (. lib), a dynamic link library (. dll), and the following steps:

1 . First, we copy the. h and. lib/.a files to the program's current directory, and then copy the DLL files to the program's output directory.

2, below we in the Pro file, add the location of the. lib file: libs+=-L d:/hitempt/api/-L MyDLL

The-l parameter specifies the location of the. lib/.a file

-l parameter specifies the import library file name (do not add extension)

Also, in the path to the import library file, the backslash is tilted to the right.

3, in the program include header file (my test DLL is written in C, so to use extern "C" {#include "dll.h"} )

The following is an implicit invocation of the instance code:

1 #include <QApplication>
2 #include <QDebug>
3 extern "C"//Because it is a C version of the DLL file, introduce its header file in C + + to add extern "C" {}, note
4 {
5 #include "dll.h"
6}
7 int main (int argv, char **argv)
8 {
9Qapplication app (ARGV,ARGV);
Ten hellowordl (); Invoke the Win32 API popup HelloWorld dialog box
Qdebug () <<add (5,6); An addition function I wrote myself in the DLL
return 0; Once the mission is complete, exit directly and don't let it go into the event loop
13}

Or the implicit invocation is convenient ah, directly can call the function in the DLL ...

Http://www.cnblogs.com/hicjiajia/archive/2010/08/27/1810239.html

function functions in the QT call DLL

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.