About lib and DLL

Source: Internet
Author: User

// ================================================ ========================================
// Title:
// Talk about lib and DLL
// Author:
// Norains
// Date:
// Wednesday 30-January-2008
// Environment:
// Vs2005 + SDK-WINCE5.0-MIPSII (include web browser component)
// ================================================ ========================================
Lib and DLL. The former is used for link, and the latter is used for runtime. it should be reasonable to say that there should be no ambiguity, but when Microsoft defines the DLL call, it involves lib again, so many beginners may ask this question: why did I link lib, DLL required during runtime? This article attempts to explain this question with a meager strength.

1. dll Export

DLL, a dynamic link library, which is literally required for running the program.

In fact, DLL and exe are very similar in architecture. The similarities are that both are binary files. The difference is that EXE uses winmain as the function entry point (the console program is main ), the DLL is dllmain. apart from this point, there is no difference between EXE and DLL.

Creating a DLL is no more complex than an EXE. To use EVC as an example, you only need to select wce dynamic-Link Library in the project tab. the only thing we need to worry about is that we must define the DLL function export interface.

There are two methods to define the export interface:

1) Use _ declspec

Use a DLL to export a function named test as an example:

//////////////////////////////////////// ////
// Dllsmp. cpp: defines the entry point for the DLL application.
//////////////////////////////////////// ////
Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Return true;
}/////////////////////////////////////// /////
// Function. h
//////////////////////////////////////// ////
Extern "C" _ declspec (dllexport) void test (); //////////////////////////////////////// ////
// Function. cpp
//////////////////////////////////////// ////
# Include "function. H"
Void test ()
{
Printf ("DLL test ");
}

We only need to pay attention to the declaration statement of function test in function. h: extern "C" _ declspec (dllexport) void test ();

C ++ will process the function name during compilation, that is, the test function may become the form of test @ 1. to avoid this problem, we need to use the extern "C" keyword to tell the compiler that this function uses the C language compilation method. On the one hand, test will appear in the DLL as its original name, on the other hand, it also enables the C program to call the dll library written in C ++. the _ declspec (dllexport) tells the compiler that this function is used as an external interface.


2) use def files

Another method is to create a file with the. Def suffix, and then define the external interface.

If this method is used, the previous example is changed:
//////////////////////////////////////// ////
// Dllsmp. cpp: defines the entry point for the DLL application.
//////////////////////////////////////// ////
Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Return true;
}/////////////////////////////////////// /////
// Function. h
//////////////////////////////////////// ////
Void test (); //////////////////////////////////////// ////
// Function. cpp
//////////////////////////////////////// ////
# Include "function. H"
Void test ()
{
Printf ("DLL test ");
}/////////////////////////////////////// ////
// Dllsmp. Def
//////////////////////////////////////// //

Exports
Test

2. One of the DLL call Methods

If we know the form parameter of the DLL export function, we can explicitly call this function.

Then we call the test function of the DLL generated in the previous example, the code can be written as follows:


Int winapi winmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
Typedef void (winapi * dll_test) (void );
Dll_test test;

// Dllsmp. dll is the dll path we want to call.
Hinstance hinstdll = loadlibrary (text ("dllsmp. dll "));
If (hinstdll! = NULL)
{
// Obtain the DLL function address
Test = (dll_test) getprocaddress (hinstdll, text ("test "));
}

Test ();

// Release resources
Freelibrary (hinstdll );

Return 0;
}

3. Lib type

There are two types of Lib, one is static Lib, and the other is the DLL export function list (here I will call it dynamic lib ).

Their Similarities and differences are roughly as follows:

1) Both are binary files.

2) Both are used during the link period.

3) Static lib can be directly run and used after link. dynamic lib also needs the corresponding DLL.

4) Static lib requires the establishment of a project for compilation. dynamic lib is an additional product automatically generated by the compiled DLL.

Because dynamic lib is automatically generated, Let's ignore it, and then let's see how to create a static Lib.

Static lib has no function entry point. That is to say, there is no function similar to winmain or dllmain. In short, it is just a set of functions. it is also very easy to create static Lib. Taking EVC as an example, you only need to select wce static library when creating the project.

To convert the previous example to static Lib, you only need to change the code as follows:

//////////////////////////////////////// ////
// Function. h
//////////////////////////////////////// ////
Void test ();
//////////////////////////////////////// ////
// Function. cpp
//////////////////////////////////////// ////
# Include "function. H"
Void test ()
{
Printf ("static test ");
}

There is no dllmain or winmain, or even the key _ declspec for identity export is not required, which is so simple.

4. Use of LIB

To use Lib in code, two conditions must be met: one is the H declaration file of Lib, and the other is the corresponding Lib. because lib plays a role only in the Link stage, you only need to set the corresponding lib path in the IDE environment. but this will bring about an unavoidable problem, that is, the path must be fixed. If you want to move the project to another machine for compilation, you need to re-set the IDE. to avoid lib link failure during frequent migration, I strongly recommend that you display the inclusion in the Code as much as possible when you know which Lib is used.

Both static lib and dynamic lib can be used accordingly. If we need to use the Lib generated in the previous example, the code can look like this:

 
// Declaration header file of LIB
# Include "../stclib/function. H"

// Import the Lib File
# Pragma comment (Lib, "../stclib. lib ")

Int winapi winmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
// Call the test function.
Test ();
Return 0;
}


Stclib in the code. if Lib is static, it can be directly run after compilation. If it is dynamic, you also need to copy the corresponding DLL to the relevant directory (in wince, can be copied to the current running directory or Windows Folder ).

By the way, I would like to tell you how to determine the possible causes of errors that are not declared by a function or macro. if comple is called, it cannot pass smoothly. h file; If comple is successful but an error occurs during link, the corresponding Lib is not imported.



4. dll call method 2

As described earlier, DLL files can also be called through Lib. This method makes the code more refined.

Rewrite the 2nd-point DLL call code in the text in the form of Lib:


// Declaration header file of the DLL export Function
# Include "../dllsmp/function. H"

// Import the DLL lib file, which contains the DLL export function Information
# Pragma comment (Lib, "../dllsmp. lib ")

Int winapi winmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
Test ();
Return 0;
}

The two DLL call methods have their own advantages and disadvantages. in the code writing stage, it is difficult to dynamically obtain the DLL export function address, which requires reading and releasing, and determining whether to obtain the export function address. The LIB method is used, you only need to include the header file and related Lib for convenient use. in the deployment phase, when the DLL export function is dynamically obtained, as long as the DLL function parameters are not changed, the EXE does not need to be re-compiled, but only updates the corresponding DLL. In the Lib mode, because lib statically lists the DLL function address, the EXE program calls the DLL function based on this address. After the DLL is updated, the exported function address is different from the previous one, therefore, the EXE file must be re-compiled based on the Lib generated by the updated DLL.

Note that, even if the same code is different from the compiler, the DLL export function address is not necessarily the same.

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.