Writing and using static library of WIN32 programming. The writing and using of dynamic link library

Source: Internet
Author: User
Tags function definition
Writing and using static library of Win32 programming. How to write and use a dynamic link library what is a static library. What is a dynamic link library.

Static libraries are a solution to the development of modules. In the past, when we write code. Everyone can write a project on their own. But not now. A project often needs to be written by many people. And the techniques used are similar to static libraries.

Everyone writes their own stuff. The last combination.

dynamic-link libraries. Also known as DLLs. Why you have a static library and a dynamic link library. The reason is the advantages and disadvantages of the static library. That's why the dynamic link library complements this disadvantage.

Disadvantages:

1. The code is large in size. A program that uses a static library compiles a large volume. In the assembly hierarchy, the code of the static library is linked to the executable file.

2. Duplicate code is many. A static library can be written by many people. There are several functions to be written.

Dynamic link library is the solution to the above shortcomings.

Ii. how to write a static library and how to use it. 1. Create a static library or lib step

If it is the VS series compiler. A method for creating a static library. vc++6.0 is created by a static lib .... Because the author Vc6.0 did not install successfully. (Does not support it) so no longer burdensome

New project, VC + +->win32 project-static library.

We can choose whether to build a DLL or a static library.

2. Writing a static Lib library

If we create a library file. That's simple. Add an. h file. Add an implementation file.

A. h one. cpp

. h put our function declaration.

. cpp put our functions into the implementation.

For example:

Add an addition function.

. h-Party declaration. . CPP implementation.

declarations in. h

  

int Retmyaddnumber (intint b);

The implementation of the. cpp.

  

int Retmyaddnumber (int A,int  b) {    return a + b;}

Compile directly and then look for the static Lib library that we compiled in the directory.

3. Using the static Lib library

Using our static Lib library is simple.

1. The. h file is required. That is, we write a static Lib declaration file.

2. A compiled static Lib is required.

3. Our program contains the. h declaration file. and use the macro command again to include the static lib. File #pragam comment (lib, "Xxxx.lib");

This is the first method.

The second method is put into the directory. And the VS Configuration library directory is the same. It is the same as the Lib library that uses C. How to configure is no longer cumbersome. The first kind of comparison is common.

Three, writing DLLs and using DLLs

Build our DLL is the same as above. But the main choice is the DLL.

Writing DLLs We also need to have a header file. Follow an implementation file. Because it is for others to use.

. h declaration file export our DLL

1. The first keyword export method.
extern " C " int _stdcall retmyaddnumber (intint  b); extern " C " int _stdcall retmysubnumber (intint b);

. CPP implementation File

  

#include "MyDllHead.h"  //Must be included. Not included will not be exported.
int retmyaddnumber (intint b) { return a + b;} int Retmysubnumber (intint b) { return A- b;}

Notice, because our function is to be used by others. So you have to export it. There are two ways to do this. The first is the keyword export. The second type is. DEF export.

Keyword export:

The _declspec (dllexeport) function returns a function calling convention function name (parameter list) so that the exported function has a name crush. That is, when we are going to use it. The name of the function has changed.

So the other keyword extern "C" means to export to us according to the C-language function definition. The name smash is because C + + has the concept of function overloading. So the function overloading is essentially the name is different. C language is not. So follow the C language export.

If you are familiar with PE, you should know that the export function has an export table. And this export table stores the functions exported by the DLL.

PS: The calling convention is different. The function name of the exported function is different. If we export with the C calling convention style, then the function name will not be renamed.

For example:

  

Export the function code.

  

 extern   "  C   __declspec (dllexport) int  _ Cdecl Retmyaddnumber (int  A, int   b) { return  a + b;}  extern   " c   " __declspec (dllexport) int  _cdecl Retmysubnumber (int  A, int   b) { return  a- b;} The 
is exported directly at the time of the function definition. You can also export on a function declaration. If it is used by others. and is implicitly called. The. h declaration file is required.
2. Second way. def file export

DEF file export is convenient.

The first thing to do is create a. def file in your project.

Export by syntax.

Grammar:

  

Exports        function name @ number        function name @ number NONAME

The function name is the name of the letter when it is exported. The number is the number that can be numbered when exporting.

The role of numbering: there is a time if the DLL is called dynamically. (LoadLibrary + GetProcAddress) then we can directly GetProcAddress (DLL handle, (LPCSTR) number) to call our DLL

Use the DEF file to export the two functions we wrote.

The LIBRARY says to indicate the name of the DLL we exported. Our name is DLL, so we gave it.

Exports is the export function.

The function we want to export is only one retmyaddnumber @ 1. The number of this function is 1. So we get the use of the use of number calls. Name calls.

PS: take note. If we export with a. def file. Then you can't use the name extern "C". Don't use keywords either.

3. Using DLLs

There are two ways to use a DLL. The first is an explicit invocation. The other is implicitly called

1. Show Call

The display call is simple.

1. Define the function pointer.

2. Using LoadLibrary to load a DLL, return a DLL handle

3. Use GetProcAddress (DLL handle, the function name or number you want to get)

The code is as follows:

#include <stdio.h>#include<stdlib.h>#include<Windows.h>#pragmaComment (lib, "Staticlib.lib")typedefint(*_cdecl PFN) (int,int);//defines a function pointer. To use a DLLintMain () {//using the exported global variableshmodule hDLL= LoadLibrary (TEXT ("Dll.dll")); PFN Padd= (PFN) GetProcAddress (hDLL,"Retmyaddnumber"); intNvalue = Padd ( -, One); printf ("Nvalue =%d \ r \ n", Nvalue);    GetChar (); return 0;}

2. Implicit invocation

implicit invocation is simple. When generating the DLL. The lib will be generated. We can use this lib directly. Same as using a static Lib library. But we need to be aware that we also need DLLs

This Lib library is just auxiliary information. It is not the same as your Static library lib above. There is a substantial code in the static library lib.

For example, the following pseudo-code:

  

" XXX Head " #pragma comment (lib, "dll Lib")int myadd (int A,int  b);  A declaration can be introduced. If our header file is introduced then do not write.

PS: Static Lib library code when used will be linked with the EXE.  The call address is seen in the assembler. DLL libraries are called call [address] indirectly.

The address will only be filled in when it is really used.

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.