How to use dynamic link library for Qt in Windows

Source: Internet
Author: User

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

Declaration: I have already written a simple DLL file (mydll. dll) and a C version interface. In addition, I used the methods in the previous two DLL articles to export the imported Library (. Lib) file from the DLL. The dll has two functions, and the prototype is as follows:

Void helloworld ();
// The function calls the Win32 API internally. The helloworld prompt box is displayed.

Int add (int A, int B );
// Add two numbers and return results

The following two methods are displayed and called implicitly to simulate how QT calls the function functions in the external DLL file, follow me ....

Prerequisites:

1. If the file is not imported into the database (. lib), but only the header file (. h) and dynamic link library (. DLL), we need to display the call. If all three files are complete, we can use simple and convenient implicit call.

2. Generally, in windows, the procedure of calling a DLL is divided into three steps (three functions): loadlibrary (), getprocadress (), and freelibrary ()

The loadlibrary () function is used to load the specified DLL file and load it to the memory of the calling program (the DLL does not have its own memory !)

The getprocaddress () function retrieves the function address of the output library in the specified dynamic link library (DLL) for calling.

Freelibrary () releases DLL Space

 

1,Show call

Qt provides a qlibrary class for display calling. The following is a complete example:

1 # include <qapplication>
2 # include <qlibrary>
3 # include <qdebug>
4 # include <qmessagebox>
5 # include "DLL. H"
// Introduce the header file
6 typedef int (* Fun) (INT, INT); // defines the function pointer for future calls
7 int main (INT argc, char ** argv)
8 {
9 qapplication app (argc, argv );
10 qlibrary mylib ("mydll. dll ");
// Declare the DLL file used
11 int result;
12 if (mylib. Load () // determine whether the load is correct
13 {
14 qmessagebox: Information (null, "OK", "DLL load is OK! ");
15 fun open = (fun) mylib. Resolve ("add ");
// Reference the add () function
16 if (open) // whether the connection is successful add () function
17 {
18 qmessagebox: Information (null, "OK", "link to function is OK! ");
19 result = open (5, 6 );
// Here, the function pointer calls the add () function in the DLL.
20 qdebug () <result;
21}
22 else
23 qmessagebox: Information (null, "no", "Linke to function is not OK !!!! ");
24}
25 else
26 qmessagebox: Information (null, "no", "DLL is not loaded! ");

27 return 0; // exit if loading fails.

28}

Mydll. dll is a custom DLL file and can be called by copying it to the output directory of the program. Obviously, it is inconvenient to show that the calling code has a huge amount of writing.

 

2. implicit call

At this time, we need three files, including the header file (. H), the imported file (. Lib), and the dynamic link library (. dll). The specific steps are as follows:

1,First, copy the. h and. lib/. A files to the current directory of the program, and then copy the DLL files to the output directory of the program,

2,Next we will add the. Lib file in the Pro file: libs + =-l D:/hitempt/API/-l mydll

-L parameter specifies the location of the. lib/. A file

-L parameter specifies the import/export file name (without the extension)

In addition, in the path of the imported file, the backslash is skewed to the right.

3,Include the header file in the program (the DLL used in my experiment is written in C, so I needExtern "C" {# include "DLL. H "})

The following example code is called implicitly:

1 # include <qapplication>
2 # include <qdebug>
3 extern "C" // because it is a C-version DLL file, you need to add extern to the header file in C ++.
"C" {}, note
4 {
5 # include "DLL. H"
6}
7 int main (INT argv, char ** argv)
8 {
9 qapplication app (argv, argv );
10 hellowordl (); // call Win32 API to bring up the helloworld dialog box
11 qdebug () <add (5, 6); // an addition function written by myself in DLL
12 Return 0; // exit directly after the mission is completed, and prevent it from entering the event Loop
13}

It's easy to call the functions in the DLL directly...

Related Article

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.