c ++ dll creation and calling

A, DLL

English as a dynamic link library DLL, is an abbreviation for Dynamic Link Library. DLL may comprise a plurality of programs, codes and data used simultaneously library.

  • By using the DLL, a program can be modular, relatively independent from the components, since the modules are independent of each other, the program loading speed faster, and when the requested module is only loaded in the corresponding function.
  • Dynamic linking differs in that the statically linked: executable modules allows dynamic link (.dll files or .exe file) contains only the information required to locate DLL function at run-time executable code. In the statically linked, the linker get all the functions referenced from a static link library into the executable file and the library together with the code.
  • Instead of using a dynamically linked statically linking has several advantages. DLL save memory, reduce exchange operation, save disk space, easier to upgrade, to provide after-sales support, provides a mechanism MFC extension library classes, support for multi-language programs, and to create an international version of the easy to complete.
  • Called divided into two types: implicit (three-piece -.h.lib.dll) and explicit (DLL files).

Second, an implicit call (three steps)

Implicit call three elements, .h files, .dll files, .lib files, are indispensable .
step:一创二引三调用

1. Create a DLL

vs2015: New = "item =" Win32 console application = "Project Name" myDll "=" OK = "next =" Select DLL = "blank check item => Done.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Add in the header file of the project in myDLL.h, add myDLL.cpp in the source file, and write the following code:
Here Insert Picture Description

//myDLL.h
#include <iostream>
using namespace std;
#ifdef MYDLL_API
#define MYDLL_API __declspec(dllexport)
#else                                                                            
#define MYDLL_API __declspec(dllimport) //当编译时,头文件不参加编译,所以.cpp文件中先定义,后头文件被包含进来,因此外部使用时,为dllexport,而在内部编译时,则为dllimport
#endif                                         
namespace myPro
{
	class MYDLL_API Person
	{
	public:
		void show();
		Person(char*, int);
		~Person();

	private:
		char* m_name;
		int m_age;
	};
	MYDLL_API  void print();
	MYDLL_API int add(int a, int b);

	MYDLL_API void swap(float &a, float &b);
}

//myDLL.cpp


#include "myDLL.h"
namespace myPro
{
	Person::Person(char* name, int age) :m_name(name), m_age(age) {}
	Person::~Person() {}
	void Person::show()
	{
		cout<<"name = "<< m_name;
		cout<< "   age = " << m_age << endl;
	}
	void print()
	{
		cout << "Hello DLL!!!" << endl;
	}
	int add(int a, int b)
	{
		return a + b;
	}
	void swap(float &a, float &b)
	{
		float temp = a;
		a = b;
		b = temp;
		
	}
}

Right = "Generate the project name. \ MyDll \ Debug directory will generate dll, lib.
Here Insert Picture Description

2. The reference DLL

In the same solution (can be in different solutions) Create a Win32 console application calls dll Solution: Right on the program name = "add =" New Project = "= Win32 Console Application" project called " useDLL "=> OK =" checked = empty project "Finish.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
He cited three files: .lib .h .dll
selected useDLL project = "item =" attribute.

  • 包含目录:添加.h所在的文件夹
  • 库目录:添加。lib所在的文件夹
  • 链接器=》输入=》附加依赖项中添加myDll.lib=>并且确定。
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description

3 调用

在useDLL.cpp引入头文件,并使用DLL中的内容。

#include <iostream>
#include "myDLL.h"
using namespace std;
using namespace myPro;
int main()
{
	char* name = "Heiren";
	Person p(name, 12345);
	p.show();
	int a1 = 12, a2 = 21;
	cout << "a1 + a1 = " << add(a1, a2) << endl;
	float f1 = 1.2, f2 = 3.4;
	myPro::swap(f1, f2);
	cout << "f1 = " << f1 << "  f2 = " << f2 << endl;
	system("pause");
	return 0;
}

Here Insert Picture Description

三、显式调用

      显示调用只需要一个dll文件。显示调用好处是模块相对独立,更新非常方便。不好的地方是使用起来稍微复杂。感觉显式调用不常用,所以这里对调用函数简单介绍一下。

  • 显示调用dll中的类好像比较繁琐,可以参考https://www.cnblogs.com/fer-team/archive/2017/12/13/8033538.html

新建一个dll项目,生成dll,把dll放在调用它的win32应用程序同目录下。
Here Insert Picture Description

//d111.cpp
extern "C" __declspec(dllexport) const char* print()
 {
    return "hello,DLL!!!";
 }
//main.cpp
#include <Windows.h>
#include <iostream>
using namespace std;
typedef const char*(*testFunc)();
int  main()
{
	HINSTANCE hDll = LoadLibraryA("myDll2.dll");
	testFunc tf = (testFunc)GetProcAddress(hDll, "print");
	if (!tf)
	cout << "Error" << endl;
	else
		cout << tf() << endl;
	FreeLibrary(hDll);
	system("pause");
	return 0;
}

Here Insert Picture Description
输出结果
Here Insert Picture Description

四 总结

动态链接具有下列优点:

  • 节省内存和减少交换操作。很多进程可以同时使用一个 DLL,在内存中共享该 DLL 的一个副本。相反,对于每个用静态链接库生成的应用程序,Windows 必须在内存中加载库代码的一个副本。
  • 节省磁盘空间。许多应用程序可在磁盘上共享 DLL 的一个副本。相反,每个用静态链接库生成的应用程序均具有作为单独的副本链接到其可执行图像中的库代码。
  • 升级到 DLL 更为容易。当 DLL 中的函数发生更改时,只要函数的参数和返回值没有更改,就不需重新编译或重新链接使用它们的应用程序。相反,静态链接的对象代码要求在函数更改时重新链接应用程序。
  • 提供售后支持。例如,可修改显示器驱动程序 DLL 以支持当初交付应用程序时不可用的显示器。
  • 支持多语言程序。只要程序遵循函数的调用约定,用不同编程语言编写的程序就可以调用相同的 DLL 函数。程序与 DLL 函数在下列方面必须是兼容的:函数期望其参数被推送到堆栈上的顺序,是函数还是应用程序负责清理堆栈,以及寄存器中是否传递了任何参数。
  • 提供了扩展 MFC 库类的机制。可以从现有 MFC 类派生类,并将它们放到 MFC 扩展 DLL 中供 MFC 应用程序使用。
  • International versions of the creation easy to complete. By resources into a DLL, to create an international version of the application much easier. It may be used for each language version of the application string into a resource file separate DLL, and different language versions load the appropriate resource.
  • A potential drawback is the application that uses the DLL is not independent; it depends on whether there is a separate DLL module.

Detailed DLL (Microsoft's official documentation)

Published 33 original articles · won praise 10 · views 2001

Guess you like

Origin blog.csdn.net/qq_33670157/article/details/104667945