VC ++ generates DLL and uses DLL in QT

Source: Internet
Author: User

1. generate DLL

 

1. Create a DLL Project

You can use multiple methods to generate a DLL. In vs, create an empty project, select Win32 console application, and modify the project properties after the new project is created: Change the generated EXE to the generated DLL

 

2.Source code:

View plaincopy to clipboardprint?
# include "stdafx. H "
# ifdef _ cplusplus // if used by C ++ Code
extern" C "{// We need to export the C interface
# endif
_ declspec (dllexport) void mysub (int x, int y, int * z)
{< br> * z = x-y;
}< BR ># ifdef _ cplusplus
}< BR ># endif
# include "stdafx. H "

# Ifdef _ cplusplus // if used by C ++ code
Extern "C" {// We need to export the C interface
# Endif
_ Declspec (dllexport) void mysub (int x, int y, int * z)
{
* Z = x-y;
}
# Ifdef _ cplusplus
}
# Endif

 

 

3. Compile the connection and generate the XYZ. dll file.

 

Ii. Use DLL

 

1. Create a project

Create a New GUI application in QT creator, and place two Pushbutton (load and quit) on the left side of the label on the form ).

Use the quit clicked () signal to bind the close () slot, and load the clicked () signal to the load () slot.

2. SourceCode:

View plaincopy to clipboardprint?
Void mainwindow: load ()
{
Int A = 1, B = 2, c = 6;
Typedef void (* myfun) (INT, Int, int *);
Qlibrary lib ("XYZ ");
Qstring qss;
If (Lib. Load ())
{
Myfun fun1 = (myfun) Lib. Resolve ("mysub"); // use resolve to parse the fun1 Function
If (fun1) // if the resolution is successful, the operation is performed and related information is prompted.
{
Fun1 (a, B, & C );
Qss = tr ("DLL success load! /N 1 + 2 = ") + qstring: Number (C, 10 );

}
}
Else
{
Qss = tr ("not found DLL! ");
}
UI-> label-> settext (qss );
}
Void mainwindow: load ()
{
Int A = 1, B = 2, c = 6;
Typedef void (* myfun) (INT, Int, int *);
Qlibrary lib ("XYZ ");
Qstring qss;
If (Lib. Load ())
{
Myfun fun1 = (myfun) Lib. Resolve ("mysub"); // use resolve to parse the fun1 Function
If (fun1) // if the resolution is successful, the operation is performed and related information is prompted.
{
Fun1 (a, B, & C );
Qss = tr ("DLL success load! /N 1 + 2 = ") + qstring: Number (C, 10 );

}
}
Else
{
Qss = tr ("not found DLL! ");
}
UI-> label-> settext (qss );
}
 

 

PS: the QT help document provides the following description:

 

Void * qlibrary: Resolve (const char * symbol)

 

The symbol must be exported as a C function from the library. this means that the function must be wrapped in an extern "C" if the library is compiled with a C ++ compiler. on Windows you must also explicitly export the function from the DLL using the_declspec (dllexport) compiler Directive, for example:

For a description of _ declspec, see the http://blog.csdn.net/lw02nju/archive/2009/07/22/4370002.aspx

Extern "C" my_export int AVG (int A, int B)
{
Return (A + B)/2;
} With my_export defined

# Ifdef q_ws_win
# Define my_export _ declspec (dllexport)
# Else
# Define my_export
# Endif

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/marsterran/archive/2010/03/26/5419706.aspx

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.