Visual Studio methods for creating and using DLLs

Source: Internet
Author: User
Tags visual studio 2010

A DLL is a library that contains code and data that can be used by multiple programs at the same time.

DLL files are the process of exporting some functions and then reusing them in a new program.

Part I: Making DLLs with Visual Studio 2010

Build method One:

First step: Select Create Win32 Application

Step two: Make the following selections in the Pop-up Application wizard:

Step three: Add Demoone.h to the header file

The code is as follows:

1 #ifndef _demo_h_2 #define_demo_h_3 #ifdef Libdll4     #definelibdll extern "C" _declspec (dllimport)5 #else6     #definelibdll extern "C" _declspec (dllexport)7 #endif8LibdllintADD (intPlus1,intplus2);9 #endif

Add Demoone.cpp

The code is as follows:

" Demoone.h " int int int b) {    return (A + b);}

The structure view is as follows:

Then click "Build Solution" under "build" to

DllDemoone.lib and DllDemoone.dll exist under the/debug/directory under the project directory

Generation Method Two:

This method does not require the use of _declspec (dllimport), one or two steps and up.

Step Three:

The Demotwo.h code is as follows:

#ifndef _demo_h_ #define _demo_h_extern"C"int Add (intint b); #endif

Demotwo.cpp is the same as the previous step

Fourth Step:

Add def file: Select "Code" on the left side of "Add New item" and create a new module definition file with the name of a random one. It is used to create Lib file contents as follows:

" Dllmaketwo "  1

The final view is as follows:

The build solution can also get DLL files.

Part II: Using DLL files

When our program needs to use a DLL, we need to load the DLL, there are two ways to load the DLL in the program, namely the dynamic link at load time and the runtime dynamic link .
In a dynamic link at load time, the application makes a display call to the exported DLL function as if it were called a local function. To use dynamic linking at load time, you need to provide the header file and the import library file (. lib) when you compile and link the application. When doing so, the linker will provide the system with the information needed to load the DLL and parse the location of the exported DLL function at load time;
In a run-time dynamic link, the application calls the LoadLibrary function or the LoadLibraryEx function to load the DLL at run time. After the DLL is loaded successfully, you can use the GetProcAddress function to get the address of the exported DLL function to invoke. You do not need to use the import library file (Lib) When using run-time dynamic linking.
There are two ways to use DLLs when actually programming, so what exactly should you use? In practical development, it is based on the following points to consider:
1. Startup performance If the initial startup performance of your application is important, you should use runtime dynamic linking;
2. Ease of use in dynamic linking at load time, the exported DLL function is similar to the local function, we can make the call of these functions conveniently;
3, application logic in the run-time dynamic link, the application can branch, in order to load the different modules as needed.

Method One: Dynamic linking at load time

Create a new normal console project and copy Dlldemoone.lib,dlldemoone.dll and Demoone.h to the same path as the project code:

The code is written as follows:

#include <iostream>#include"Demoone.h"//Method Ausing namespacestd;#pragmaComment (lib, "DLLDemoone.lib")//extern "C" _declspec (dllimport) int Add (int a, int b);//Method BintMainintargcChar*argv[]) {cout<<add (2,3) <<Endl; return 0;}

Run to get the right results.

Method A and Method B are two different ways, and if you use method B, you do not need to copy Demo.h this header file.

Method Two: Dynamic link at runtime

Create a new normal console project and copy Dlldemoone.lib,dlldemoone.dll and Demoone.h to the same path as the project code:

#include <windows.h>#include<iostream>using namespaceStd;typedefint(*addfunc) (intAintb);intMainintargcChar*argv[]) {hmodule hDLL= LoadLibrary ("DLLmaketwo.dll"); if(hDLL! =NULL) {Addfunc Add= (Addfunc) GetProcAddress (hDLL, Makeintresource (1)); if(Add! =NULL) {cout<<add (2,3) <<Endl;      } freelibrary (hDLL); }}

Here you will encounter the error C2664: "Loadlibraryw": Cannot convert the parameter 1 from "const char *" to "lpcwstr" problem, the solution is to change the project character way, see another article:

Http://www.cnblogs.com/holyprince/p/4236586.html

The method of creation and use is broadly divided into the above four, referring to two articles, thanks to them:

http://www.jellythink.com/archives/111

http://www.aichengxu.com/view/14438

How Visual Studio creates and uses DLLs

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.