Code Examples
  Home arrow Code Examples arrow Page 7 - Dynamic Link Libraries Inside-Out
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Dynamic Link Libraries Inside-Out
By: Digvijay Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 56
    2004-09-15

    Table of Contents:
  • Dynamic Link Libraries Inside-Out
  • Advantages and Disadvantages of Using DLLs
  • What Does the DLL Contain?
  • The DLL Entry Point Function: DllMain
  • Types of DLL Linking
  • Resource Only DLLs
  • Dynamic Link Library Redirection
  • Points of Interest
  • Troubleshooting and Common Errors

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Dynamic Link Libraries Inside-Out - Dynamic Link Library Redirection


    (Page 7 of 9 )

    Starting with Windows 2000, you can ensure that your application uses the correct version of a DLL by creating a redirection file. The redirection file must be named as “appname.local”. For example; an application c:myappmyapp.exe calls LoadLibrary using the path c:program filescommon filessystemmydll.dll. If c:myappmyapp.exe.local and c:myappmydll.dll exist, LoadLibrary will load c:myappmydll.dll.

    Otherwise, LoadLibrary will load c:program filescommon filessystemmydll.dll.

    Writing your own DLLs

    I’ll explain briefly how to write your own DLLs and use them in your applications using VC++ 6.0 and Visual Basic 6.0. The source code files for the examples can be downloaded from the last page of this article. So let’s get started!

    There are 3 steps: Creating Source Files, Exporting Symbols, and Creating an Import Library.

    1. Creating Source Files

    • Fire up Microsoft Visual C++ IDE and create a Blank Workspace with the name DLLsTraining.
    • Add a new “Win32 Dynamic-Link Library” project with name “LoadTimeDLL” to the workspace “DLLsTraining”.
    • Choose an empty DLL project
    • Add a new file named “LoadTimeDLL.cpp” and “LoadTimeDLL.h” to LoadTimeDLL project.
    • Add code shown below to “LoadTimeDLL.cpp” and “LoadTimeDLL.h”

    Read the comments so as to find out why are you writing this.

    LoadTimeDLL.cpp

    #include <windows.h>
    // Define the symbol LOAD_TIME_DLL_EXPORTS before including
    // "LoadTimeDll.h". This makes the __declspec(dllexport) visible to the
    // implementing .cpp file. When using this header in other modules, don’t define
    // this symbol. Then those modules will see __declspec(dllimport). This makes
    // those modules to import the function form the DLLs.

    #define LOAD_TIME_DLL_EXPORTS

    #include "LoadTimeDLL.h"
     
    void SayLoadTimeDLL()
    {
    MessageBox(NULL, TEXT(“Information"), TEXT("I am a load time DLL"),MB_OK);
    }

    LoadTimeDLL.h

    //Add the include guards to protect from cyclic an redundant inclusions
    #ifndef _LOAD_TIME_DLL_H
    #define _LOAD_TIME_DLL_H
     
    #ifdef LOAD_TIME_DLL_EXPORTS
    // This is syntax that has to be followed to export a function that can be used
    // from other module, which loaded this dll. “__declspec(dllexport)” tells the
    // compiler to export the definition of this function.
    extern __declspec(dllexport) void SayLoadTimeDLL() ;
    #else
    // This is syntax that has to be visible for the module that uses this function.
    // “__declspec(dllimport)” tells the compiler to bring the definition of this
    // function from a DLL.
    extern __declspec(dllimport) void SayLoadTimeDLL() ;
    #endif
     
    #endif

    Now Compile the project LoadTimeDLL. The out put should look like:
    -------------Configuration: LoadTimeDLL - Win32 Debug------------
    Compiling...
    LoadTimeDLL.cpp
    Linking...

    Creating library Debug/LoadTimeDLL.lib and object Debug/LoadTimeDLL.exp

    LoadTimeDLL.dll - 0 error(s), 0 warning(s)

    If the generation of “LoadTimeDLL.lib” is absent, it means no functions were exported and DLL cannot be loaded or used.

    (To test, change the name of the SayLoadtimeDLL to SayLoadtimeDll. Observe case.)

    What you’ve done is creating a library that you can implicitly link to.

    Now perform the following steps to create a library that you’ll link to explicitly:

    • Add a new “Win32 Dynamic-Link Library” project with name “RunTimeDLL” to the workspace “DLLsTraining”.
    • Choose an empty DLL project
    • Add a new file named “RunTimeDLL.cpp”, “RunTimeDLL.h” and “RunTimeDLL.def” to RunTimeDLL project.
    • Add code shown below to “RunTimeDLL.cpp” and “RunTimeDLL.h”

    RunTimeDLL.cpp

    #include <windows.h>

    #include "RunTimeDLL.h"

    // Definition of the function which will be exported to other modules.

    void SayRunTimeDLL()
    {
        MessageBox( NULL,
      TEXT("I am a run time DLL"),
      TEXT(“Information"),
      MB_OK );
    }
     


    RunTimeDLL.h

    // Add the include guards to protect from cyclic an redundant inclusions

    #ifndef _RUN_TIME_DLL_H
    #define _RUN_TIME_DLL_H

    // Here the function will be exported using .def file. So no need to export using the
    // declspec. This is the way to export the function from an dll that can be loaded at
    // runtime and asked for the addresses of the function.

    void SayRunTimeDLL() ;
     
    #endif

    RunTimeDLL.def

    LIBRARY "RunTimeDLL"

    EXPORTS
     SayRuntimeDLL

    Now Compile the project RunTimeDLL. The output should look like:

    -------------Configuration: RunTimeDLL - Win32 Debug--------------
    Compiling...
    RunTimeDLL.cpp
    Linking...

    Creating library Debug/RunTimeDLL.lib and object Debug/RunTimeDLL.exp

    RunTimeDLL.dll - 0 error(s), 0 warning(s)

    If the generation of “RunTimeDLL.lib” is absent, it means no functions were exported and DLL cannot be loaded used.

    (To test change the name of the SayRunTimeDLL to SayRunTimeDll in .def file. Observe case.)


    2. Exporting Symbols

    As I explained earlier that by using the __declspec (dllexport) keyword you made that dll export the particular symbol. Alternatively as in the RunTimeDLL when you create a .def file you state the function names to be exported.

    3. Creating an Import Library

    When you build the projects in the IDE.The .lib file produced with the DLL is the import library that your applications may link to if they need to use the function that you wrote in the DLL.

    More Code Examples Articles
    More By Digvijay Chauhan


       · hi,Its Ravi Verma from India. This article on dll is really a master piece....
       · hi,Its Ravi Verma from India. This article on dll is really a master piece....
       · hey you indian,you have my job. stop outsourcing now
       · Where are the pictures in this article?Best RegardsHolland
       · I Jon't have a job either
       · Companies hire only talented deserving people......
       · Yes very True,That's why 'I QUIT' and don't have a job. They don't know how to...
       · the article could have been better if mapping of dll to address space have been...
       · Great article, thanks for sharing...
     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT