DLLの紹介と作成


1.         

               “    ”   ,        (.obj)、      (.lib)          (.res)     ,         (.exe)。       ,                         。                              exe   ,     、      。 “    ”                      (.dll),       dll             (         ),    dll              、   dll      ,         dll        dll    ;     dll         ,            。    dll           dll   ,    dll           (     dll        )。               :         。             ,                。            ,                  。

2.         

  →    ;
  →     “  ”;
  →                      ;
  →            (   VC++  dll,   VB   );
  →      ;
  →           
  →......

3.       

              dll,   exe,drv,fon       ,       dll         Windows    。               ,                LoadLibrary LoadLibraryEx         。 

4.  SDK       dll  

   VC++       Win32 Dynamic-Link Library。      c/c++ head file   c/c++ source file     。              ,       DllMain          。           。 

//dlldemo.h 
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport) 
#endif

EXPORT void CALLBACK DllFoo(void) ;

//dlldemo.c
#include <windows.h>
#include "dlldemo.h" 

int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
 return TRUE ; 
}

EXPORT void CALLBACK DllFoo(void)
{ 
 MessageBox(NULL,TEXT("This function is exported from a DLL"),TEXT("DllFoo"),MB_OK) ;
 return ;
}
 

  

          __declspec      “C       ”(C Extended Storage-Class Attributes),                          ,   thread,naked,dllimport dllexport. [MSDN  :The extended attribute syntax for specifying storage-class information uses the __declspec keyword, which specifies that an instance of a given type is to be stored with a Microsoft-specific storage-class attribute (thread, naked, dllimport, or dllexport).]          CALLBACK。 DllMain dll      。      。DllMain    TRUE,              “       ”   。      ,         dlldemo.dll      dlldemo.lib。

5.  dll      

     : load-time dynamic linking 
      dll        , dll      (import library,.lib  )    。                 #include ,            dlldemo.dll       。 

     : run-time dynamic linking 
               ,         LoadLibrary LoadLibraryEx     dll。
       ( demodll.dll  ): 


1) typedef           。
 typedef void (CALLBACK* DllFooType)(void) ;
 DllFooType pfnDllFoo = NULL ; 
2)   LoadLibrary  dll,   dll    
 HINSTANCE dllHandle = NULL ;
 ... 
 dllHandle = LoadLibrary(TEXT("dlldemo.dll"));  
3)   GetProcAddress  dll      
 pfnDllFoo = (DllFooType)GetProcAddress(dllHandle,TEXT("DllFoo")) ;
    GetProcAddress                   。 
4)      ,             
 if(pfnDllFoo!=NULL)
 DllFoo() ; 
5)  FreeLibrary  dll
 FreeLibrary(dllHandle) ; 

    run-time dynamic linking     ,      (    )。MSDN      DLLs the    Dynamic Way    c        pDll         ,              pDll           。 
                  dll  ,Windows       dll  :

1)        。 
2)    。 
3)Windows 9x : System  ; Windows 2000/NT : System32    
4)Windows 2000/NT System  。 
5)Windows    。 
6)    path    。 

          dll  ,     dll        “       ”   ,   “        dll  -XXX.dll”。 

6           (Run-Time Dynamic Linking)     ? 

              (Load-Time Dynamic Linking), dll     ,   dll        ,       “       ”   。         dll,          ,      “  ”     。 
    dll   ,  load-time dynamic linking        ,   run-time dynamic linking               dll         。 

7      dll

        c        DllMain,          ,     dll  。   dll        ,      .lib  ,         LoadLibrary  。 

8  dll       (  programming windows) 

// shared memory section (requires /SECTION:shared,RWS in link options)
#pragma data_seg ("shared")
 int iTotal = 0 ;
 WCHAR szStrings [MAX_STRINGS][MAX_LENGTH + 1] = { '\0' } ;
#pragma data_seg () 

#pragma comment(linker,"/SECTION:shared,RWS") 

     #pragma       ,     shared。                   。    #pragma                 shared    。   #pragma        。                ,                           shared 。           「shared」     。 「Project Settings」      「Link」    。  「STRLIB」  「Project Options」  ( Release Debug     ),         : /SECTION:shared,RWS   RWS      、      。  ,       DLL         ,     STRLIB.C  : #pragma comment(linker,"/SECTION:shared,RWS")