DLL INTRODUCTION

  • A DLL is a library that contains code and data that can be used by more than one program at the same time. For example, in Windows operating systems, the Comdlg32 DLL performs common dialog box related functions. Therefore, each program can use the functionality that is contained in this DLL to implement an Open dialog box. This helps promote code reuse and efficient memory usage.
  • By using a DLL, a program can be modularized into separate components. For example, an accounting program may be sold by module. Each module can be loaded into the main program at run time if that module is installed. Because the modules are separate, the load time of the program is faster, and a module is only loaded when that functionality is requested.
  • Additionally, updates are easier to apply to each module without affecting other parts of the program. For example, you may have a payroll program, and the tax rates change each year. When these changes are isolated to a DLL, you can apply an update without needing to build or install the whole program again.

DLL development

  • This section describes the issues and the requirements that you should consider when you develop your own DLLs.

  • Types of DLLS

    • When you load a DLL in an application,two methods of linking let you call the exported DLL functions. The two methods of linking are load-time dynamic linking and run-time dynamic linking.
    • Load-time dynamic linking
      • In load-time dynamic linking, an application makes explicit calls to exported DLL functions like local functions. To use load-time dynamic linking, provide a header (.h) file and an import library (.lib) file when you compile and link the application. When you do this, the linker will provide the system with the information that is requred to load the DLL and resolve the exported DLL function locations at load time.
    • Run-time dyanmic linking:
      • In run-time dynamic linking, an application calls either the LoadLibrary function or the LoadLibraryEx function to load the DLL at run time. After the DLL is successfully loaded, you use the GetProcAddress function to obtain the address of the exported DLL function that you want to call. When you use run-time dynamic linking, you do not need an import libraty file.
    • The following list describes the application criteria for when to use load-time dynamic linking and when to use run-time dynamic linking:
      • Startup performance

        • If the initial startup performance of the application is important, you should use run-time dynamic linking.
      • Ease of use
        • In load-time dynamic linking, the exported DLL functions are like local functions. This makes it easy for you to call these functions.
      • Application logic
        • In run-time dynamic linking, an application can branch to load different modules as required. This is important when you develop multiple-language versions.
  • The DLL entry point

    • When you create a DLL, you can optionally specify an entry point function. The entry point function is called when proccess or threads attach themselves to the DLL or detached themselves from the DLL. You can use the entry point function to initialize data structure or to destory data structure as required by the DLL. Additionally, if the application is multithreaded, you can use thread local storage to allocate memory that is private to each thread the entry point function.The following code is an example of the DLL entry point function.
    •   BOOL APIENTRY DllMain(
      HANDLE hModule,// Handle to DLL module
      DWORD ul_reason_for_call,// Reason for calling function
      LPVOID lpReserved ) // Reserved
      {
      switch ( ul_reason_for_call )
      {
      case DLL_THREAD_ATTACH:
      // A process is loading the DLL.
      break;
      case DLL_THREAD_DETACH:
      // A process is creating a new thread.
      break;
      case DLL_PROCESS_ATTACH:
      // A thread exits normally.
      break;
      case DLL_PROCESS_DETACH:
      // A process unloads the DLL.
      break;
      }
      return TRUE;
      }
    • When the entry point function returns a FALSE value, the application will not start if you are using load-time dynamic linking. If you are using run-time dynamic linking, only the individual DLL will not load.
    • The entry point function should only perform simple initialization tasks and should not call any other DLL loading or termination functions. For example ,in the entry point function, you should not directly or indirectly call the LoadLibrary function or the LoadLibraryEx function.
  • Exporting DLL functions

    • To export DLL functions, you can either add a function keyword to the exported DLL functions or create a module definition (.def) file that lists the exported DLL functions.
    • To use a function keyword, you must declare each function that you want to export with the following keyword:
      • __declspec(dllexport)
    • To use exported DLL functions in the application, you must declare each function that you want to import with the following keyword:
      • __declspec(dllimport)
    • Typically, you would use one header file that has a define statement and an ifdef statement to separate the export statement and the import statement.
    • You can also use a moudle definition file to declare exported DLL functions. When you use a module definition file, you do not have to add the function keyword to the exproted DLL functions.In the moudle definition file, you declare the LIBRARY statement and the EXPORTS statement for the DLL.
    •   LIBRARY	"DLL"
      EXPORTS
      HelloWorld
  • Sample DLL and application

    • In Visual Studio 2010, you can create DLL by selecting the Win32 Console Application project type.
    • The following code is an example of a DLL that was created in VS by using the Win32 Console Application
    • dllmain.cpp:
    •   // dllmain.cpp : Defines the entry point for the DLL application.
      #include "stdafx.h"
      #include "DLL.h"
      #define EXPORTING_DLL BOOL APIENTRY DllMain( HANDLE hModule,
      DWORD ul_reason_for_call,
      LPVOID lpReserved
      )
      {
      return TRUE;
      } void HelloWorld()
      {
      MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK);
      }
    • DLL.h:
    •   #ifndef INDLL_H
      #define INDLL_H #ifdef EXPORTING_DLL
      extern __declspec(dllexport) void HelloWorld() ;
      #else
      extern __declspec(dllimport) void HelloWorld() ;
      #endif #endif
    • The following code is an example of a Win32 Console Application project that calls the exported DLL function in the DLLTest.
    • DLLTest.cpp:
    •   // DLLTest.cpp : Defines the entry point for the console application.
      // #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[])
      {
      HelloWorld();
      return 0;
      }
    • stdafx.h:
    •   // stdafx.h : include file for standard system include files,
      // or project specific include files that are used frequently, but
      // are changed infrequently
      // #pragma once #include "targetver.h"
      #include "DLL.h"
      #include <stdio.h>
      #include <tchar.h>
      #pragma comment(lib,"DLL.lib") // TODO: reference additional headers your program requires here
    • Note In load-time dynamic linking, you must link the DLL.lib import library that is created when you build the DLLTest project.
    • In run-time dynamic linking, you use code that is similar to the following code to call the DLL.dll exported DLL function.
    •   // DLLTest.cpp : Defines the entry point for the console application.
      // #include "stdafx.h"
      #include <Windows.h>
      typedef VOID (*DLLPROC) (); int _tmain(int argc, _TCHAR* argv[])
      {
      HINSTANCE hinstDLL;
      DLLPROC HelloWorld;
      BOOL fFreeDLL; hinstDLL = LoadLibrary("DLL.dll");
      if (hinstDLL != NULL)
      {
      HelloWorld = (DLLPROC) GetProcAddress(hinstDLL, "HelloWorld");
      if (HelloWorld!=NULL)
      {
      HelloWorld();
      } fFreeDLL = FreeLibrary(hinstDLL);
      }
      getchar();
      ///HelloWorld();
      return 0;
      }
    • When you compile and link the DLLTest application, the Windows operating system searches for the DLLTest in the following locations in this order:
      1. The application folder
      2. The current folder
      3. The Windows system folder
      4. The Windows folder
  • 参考地址:https://support.microsoft.com/en-us/help/815065/what-is-a-dll

动态链接库(Dynamic Link Library)的更多相关文章

  1. 动态链接库(Dynamic Link Library)学习笔记(附PE文件分析)

    转载:http://www.cnblogs.com/yxin1322/archive/2008/03/08/donamiclinklibrary.html 作者:EricYou 转载请注明出处   注 ...

  2. Walkthrough: Creating and Using a Dynamic Link Library (C++)

    Original Link: http://msdn.microsoft.com/zh-cn/library/ms235636.aspx Following content is only used ...

  3. Custom Action : dynamic link library

    工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...

  4. How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)

    The Dynamic Link Library (DLL) is stored separately from the target application and shared among dif ...

  5. DYNAMIC LINK LIBRARY - DLL

    https://www.tenouk.com/ModuleBB.html MODULE BB DYNAMIC LINK LIBRARY - DLL Part 1: STORY What do we h ...

  6. Walkthrough: Create and use your own Dynamic Link Library (C++)

    参考网站:https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-librar ...

  7. [DLL] Dynamic link library (dll) 的编写和使用教程

    前一阵子,项目里需要导出一个DLL,但是导出之后输出一直不怎么对,改了半天才算改对...读了一些DLL教程,感觉之后要把现在的代码导出,应该还要花不少功夫...下面教程参照我读的3个教程写成,所以内容 ...

  8. How to Create DLL(Dynamic link library)

    该文章属于在YouTube视频上看到的,链接如下: https://www.youtube.com/watch?v=EmDJsl7C9-k&t=3s 1.创建一个工程并建立一个控制台程序 2. ...

  9. Linux Dynamic Shared Library && LD Linker

    目录 . 动态链接的意义 . 地址无关代码: PIC . 延迟版定(PLT Procedure Linkage Table) . 动态链接相关结构 . 动态链接的步骤和实现 . Linux动态链接器实 ...

随机推荐

  1. angularjs路由传值$routeParams

    AngularJS利用路由传值, 1.导包 <script src="angular.min.js"></script> <script src=&q ...

  2. Fundebug网站升级HTTP/2,真的变快了!

    作为新一代的HTTP协议,HTTP/2可以提高网站性能,优化用户体验,Fundebug也是时候升级HTTP/2了,虽然已经有点晚了. 升级HTTP/2是一件很简单的事情,改1行Nginx配置就好了,但 ...

  3. [PHP] 循环查看php-fpm的内存占用情况

    在webmail的业务中进行发信,如果携带了附件,会把附件拼接内嵌到邮件正文里,这时会极大的占用内存,可以使用以下命令查看fpm的进程内存占用 ps --no-headers --sort -rss ...

  4. LInux:网络连接的设置

    主机名的配置 主机名的配置(配置文件/etc/hostname) 1.使用 hostname 命令临时设置主机名 命令格式:hostname [新主机名] 2.永久设置主机名 命令格式:hostnam ...

  5. 【西北师大-2108Java】第八次作业成绩汇总

    [西北师大-2108Java]第八次作业成绩汇总 作业题目 面向对象程序设计(JAVA)--第10周学习指导及要求 实验目的与要求 (1)掌握java异常处理技术: (2)了解断言的用法: (3)了解 ...

  6. [配置]VUE中通过process.env判断开发,测试和生产环境,并分环境配置不同的URL HOST

    本文链接:https://blog.csdn.net/tom_wong666/article/details/89763620 Tom哥的博客博文分类和索引页面地址:https://blog.csdn ...

  7. Vue入门(二)

    1.vue3.0安装 cnpm install -g @vue/cli 或者 yarn global add @vue/cli //创建项目 vue create hello-world //运行 n ...

  8. 浅谈C++ STL list 容器

    浅谈C++ STL list 容器 本篇随笔简单讲解一下\(C++STL\)中\(list\)容器的使用方法和使用技巧. list容器的概念 学习过\(C++STL\)的很多同学都知道,\(STL\) ...

  9. 一道常被人轻视的web前端常见面试题(JS)

    本文转载自站长之家,如有侵权问题,请联系我,马上删除. 面试题是招聘公司和开发者都非常关心的话题,公司希望通过它了解开发者的真实水平和细节处理能力,而开发者希望能够最大程度地展示自己的水平(甚至超常发 ...

  10. [开源] FreeSql 配套工具,基于 Razor 模板实现最高兼容的生成器

    FreeSql 经过半年的开发和坚持维护,在 0.6.x 版本中完成了几大重要事件: 1.按小包拆分,每个数据库实现为单独 dll: 2.实现 .net framework 4.5 支持: 3.同时支 ...