Windows Dynamic-Link Libraries 7

 

 

 

 

 

Using Load-Time Dynamic Linking Example

 

After you have created a DLL, you can use the functions it defines in an application. The following is a simple console application that uses the myPuts() function exported from Myputs.dll (or imported by the application). Because this example calls the DLL function explicitly, the module for the application must be linked with the import library Myputs.lib. For other compilers, to build DLLs, see 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.

 

Using Load-Time Dynamic Linking Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Select an empty console mode application project. Click the Finish button.

 

Using Load-Time Dynamic Linking Example: Selecting an empty Win32 project

 

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

 

Using Load-Time Dynamic Linking Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Next, add the following source code.

 

#include <windows.h>

#include <stdio.h>

 

// a function from a DLL

extern "C" int myPuts(LPWSTR);

 

int wmain(void)

{

    int Ret = 1;

 

    wprintf(L"In the executable...\n");

 

    Ret = myPuts(L"Message sent to the DLL function...");

    wprintf(L"Back to the executable, Ret = %d\n", Ret);

    return Ret;

}

 

Using Visual Studio IDE, we have two ways to tell the compiler where to find the library file (.lib). The first one is the Visual C++ project wide. In this case invoke the Tools > Options menu.

 

 

Using Load-Time Dynamic Linking Example: Invoking the Visual studio Options menu

 

Select the Visual C++ Directories subfolder for the Projects and Solutions folder. For Show directories for: drop down box, select Library files.

 

Using Load-Time Dynamic Linking Example: Setting additional libraries file for the Visual C++ project

 

Then add the library folder.

 

Using Load-Time Dynamic Linking Example: Adding the additional library path

 

Using Load-Time Dynamic Linking Example: Selecting the library path

 

Take note that if your path of the library (lib file) is different, change it accordingly, your project (your machine) should have different path. Another way telling the compiler where to find the library file is through the project’s setting. While the project was opened, invoke the project properties page.

 

Using Load-Time Dynamic Linking Example: Invoking the Visual C++ project's properties page

 

Under the Linker folder, select the General link. For the Additional Library Directories field add the path to the library/dll.

 

Using Load-Time Dynamic Linking Example: The additional library directories path

 

Using Load-Time Dynamic Linking Example: The additional library directories path

 

Using Load-Time Dynamic Linking Example: The additional library directories path

 

Then, we need to tell the compiler the library file name that needs to be linked. Under the Linker folder, select the Input link. For the Additional Dependencies field, add the library file name. Click OK.

 

Using Load-Time Dynamic Linking Example: Adding the additional dependencies library file

 

Next, we need to copy the DLL file to the application’s Debug folder (for this case).

 

Using Load-Time Dynamic Linking Example: Copying the generated DLL file to the current project folder

 

Well, now we are ready to run the project. Run the application project.

 

Using Load-Time Dynamic Linking Example: The DLL file has been copied and the EXE program is ready to be run

 

The following screenshot shows a sample output which indicates the function in the DLL (can be exported) was successfully 'imported' by the application program.

 

Using Load-Time Dynamic Linking Example: A sample console program output in action

 

 

 

 

< Windows Dynamic Link Library 6 | Windows Dynamic Link Library | Win32 Programming | Windows Dynamic Link Library 8 >