Compiling and using dynamic library DLL and static library lib under VC

Source: Internet
Author: User

 

In some projects, considering the security and stability of the system, some DLL or lib libraries are often encapsulated for use by others. How can we create DLL or lib files? Let's explain the basic production methods based on your actual situation.
The following are my personal operation records:
1. How to compile dynamic library dll:
Create a Dynamic Link Library:

Enter the project name to select the project type. If there is no special requirement, select an empty project.
We can see that there are no default files created by the system, just like the standard console project.

Next we need to add some files.
To create a dynamic library DLL file, we do not need to write the main function. You only need to implement the interface functions one by one.
Extern "C" _ declspec (dllexport) description. Add extern "C" _ declspec (dllexport) to the function declaration to export the function.
For example:
Create Function. H, function. cpp
Function. h
# Ifndef _ function_h __
# DEFINE _ function_h __
# Include <afx. h>
# Include "include. H"
// 3DES encryption function. 24-byte key (export function)
Extern "C" _ declspec (dllexport) handle open ();
# Endif
In function. cpp, you can write
Extern "C" _ declspec (dllexport) handle open ()
{
Handle devhandle = invalid_handle_value;
Devhandle = opendevice ();
Return devhandle;
}
After compilation, you can see the DLL file in the debuf or release folder.
It is also easy to use DLL files: place the generated DLL files in the Application Program Project directory, we also need to export the header file in the previous step.
# Include "apptest. H"
// # Pragma comment (Lib, "apptest. lib ")
Hinstance mydll;
Handle encryp;
// Open the DLL file
Mydll = loadlibrary ("apptest. dll ");
If (mydll = NULL)
{
Exit (0 );
}
Typedef handle (* dllfun) (); // function pointer. It must be in the same format as the original function prototype.
Dllfun myfun;
Myfun = (dllfun) getprocaddress (mydll, "open"); // obtain the virtual address of the Function
If (myfun = NULL)
{
Afxmessagebox ("An error occurred while obtaining the DLL function! ");
Exit (0 );
}
Encryp = myfun (); // calling myfun is actually calling opendog in DLL
If (encryp = invalid_handle_value)
{
Exit (0 );
}

Static Library Creation and Application
Create a static library project

There is no option to pay attention to. The default value is enough. After the project is completed, there are currently no files, and we need to add them ourselves. We do not need to write the main function. We only need to write the implementation of the function. Then, use extern in the header file to compile the file.
For example, create the function. h and function. cpp files.
Function. h
# Ifndef _ function_h __
# DEFINE _ function_h __
# Include <afx. h>
# Include "include. H"
Extern handle opendog ();
# Endif
Function. cpp
Handle opendog ()
{
Handle devhandle = invalid_handle_value;
Devhandle = opendevice ();
Return devhandle;
}
In this case, compile.
However, I have encountered many problems here, and errors are always prompted during compilation. This is mainly caused by the name duplication of some namespaces. This error is troublesome. Here are the project settings for compiling Lib.

 

 

 

Pay special attention to the ignore library shown in the second one.
Use of LIB static library: Use # pragma comment (Lib, "apptest. lib") to directly call the function of the static library.
# Include "apptest. H"
# Pragma comment (Lib, "apptest. lib ")
Handle encrypdog;
Encrypdog = opendog ();
If (encrypdog = invalid_handle_value)
{
Exit (0 );
}

 

 

This article from teku bar http://www.tekuba.net/, the original address: http://www.tekuba.net/program/179/

 

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.