Windows Dynamic-Link Libraries 6

 

 

 

 

 

Using Dynamic-Link Libraries: Program Examples

 

The following examples demonstrate how to create and use a DLL.

 

  1. Creating a simple dynamic-link library
  2. Using load-time dynamic linking
  3. Using run-time dynamic linking
  4. Using shared memory in a dynamic-link library
  5. Using thread local storage in a dynamic-link library

 

Creating a Simple Dynamic-Link Library Example

 

The following example is the source code needed to create a simple DLL, Myputs.dll. It defines a simple string-printing function called myPuts(). The Myputs DLL does not define an entry-point function, because it is linked with the C run-time library and has no initialization or cleanup functions of its own to perform. To build the DLL, follow the directions in the documentation included with your development tools.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed. Click the OK button.

 

Creating a Simple Dynamic-Link Library (DLL) Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Click the Next button.

 

Creating a Simple Dynamic-Link Library (DLL) Example: Customizing the Win32 application project using the wizard

 

Select the DLL radio button for the Application type: and Empty project for the Additional options:. Click the Finish button.

 

Creating a Simple Dynamic-Link Library (DLL) Example: Customizing the Win32 application project using the wizard - selecting DLL application type and empty project

 

Then, add the source file and give it a suitable name.

 

Creating a Simple Dynamic-Link Library (DLL) Example: Invoking the Add New Item menu

 

Creating a Simple Dynamic-Link Library (DLL) Example: Adding new C++ source file for C++ source code to the existing C++ project

 

Next, add the following source code.

 

// The myPuts function writes a null-terminated string to

// the standard output device.

 

// The export mechanism used here is the __declspec(export)

// method supported by Microsoft Visual Studio, but any

// other export method supported by your development

// environment may be substituted.

#include <windows.h>

#include <stdio.h>

#define EOF (-1)

 

#ifdef __cplusplus    // If used by C++ code,

extern "C" {          // we need to export the C interface

#endif

 

__declspec(dllexport) int myPuts(LPWSTR lpszMsg)

{

    DWORD cchWritten;

    HANDLE hConout;

    BOOL fRet;

 

    // Get a handle to the console output device.

    hConout = CreateFileW(L"CONOUT$",

                         GENERIC_WRITE,

                         FILE_SHARE_WRITE,

                         NULL,

                         OPEN_EXISTING,

                         FILE_ATTRIBUTE_NORMAL,

                         NULL);

 

    if (INVALID_HANDLE_VALUE == hConout)

        return EOF;

 

    wprintf(L"I'm in DLL lor, with myPuts() that can be exported!\n");

    wprintf(L"I'm displaying a text sent to me by executable...\n");

 

    wprintf(L"\"");

    // Write a null-terminated string to the console output device

    while (*lpszMsg != L'\0')

    {

        fRet = WriteConsole(hConout, lpszMsg, 1, &cchWritten, NULL);

        if( (fRet == FALSE) || (cchWritten != 1) )

            return EOF;

        lpszMsg++;

    }

      wprintf(L"\"\n");

 

    return 1;

}

 

#ifdef __cplusplus

}

#endif

 

Build and run the project. The following screenshot is a sample window, asking the executable (application) to be run (at this stage, we do not have the executable yet, that can import or use the function in the DLL). Just dismiss the window.

 

Creating a Simple Dynamic-Link Library (DLL) Example: The DLL cannot be run independently

 

Under the project's Debug folder, the DLL and lib files should be generated and ready to be used.

 

Creating a Simple Dynamic-Link Library (DLL) Example: The DLL and LIB file have been generated successfully

 

 

 

 

< Windows Dynamic Link Library 5 | Windows Dynamic Link Library | Win32 Programming | Windows Dynamic Link Library 7 >