How to Use Multi-Threaded DLLs in Visual Studio 2012
Multi-threaded DLLs are dynamic-link libraries that use the multithread-specific and DLL-specific version of the run-time library. They can improve the performance and scalability of your applications by allowing multiple threads to execute concurrently within a single process. However, using multi-threaded DLLs also requires some special considerations and settings in Visual Studio 2012. In this article, we will explain how to create and use multi-threaded DLLs in Visual Studio 2012.
Creating a Multi-Threaded DLL Project
To create a multi-threaded DLL project in Visual Studio 2012, follow these steps:
Select File > New > Project from the menu.
In the New Project dialog box, select Visual C++ > Win32 > Win32 Project from the templates.
Enter a name and location for your project and click OK.
In the Win32 Application Wizard, select DLL as the application type and check Empty project as the additional option. Click Finish.
Add a new source file to your project by right-clicking on the Source Files folder in the Solution Explorer and selecting Add > New Item. Choose C++ File (.cpp) as the item type and enter a name for your file. Click Add.
In the source file, write the code for your DLL functions. You can use any C++ features or libraries that are compatible with the run-time library you choose. You also need to declare your functions with __declspec(dllexport) to export them from the DLL.
To specify the run-time library for your DLL project, open the project's Property Pages dialog box by right-clicking on the project name in the Solution Explorer and selecting Properties. Select Configuration Properties > C/C++ > Code Generation from the left pane. In the right pane, choose one of the following options from the Runtime Library drop-down list:
/MD: Use this option to link with the multithread-specific and DLL-specific version of the run-time library (MSVCRT.lib). This option defines _MT and _DLL and causes the compiler to place the library name MSVCRT.lib into the .obj file. The actual working code is contained in MSVCR versionnumber .DLL, which must be available at run time to applications linked with MSVCRT.lib[^1^].
/MDd: Use this option to link with the debug multithread-specific and DLL-specific version of the run-time library (MSVCRTD.lib). This option defines _DEBUG, _MT, and _DLL and causes the compiler to place the library name MSVCRTD.lib into the .obj file[^1^].
Click OK to save your settings.
Using a Multi-Threaded DLL in an Application
To use a multi-threaded DLL in an application, follow these steps:
Create or open an application project in Visual Studio 2012 that is compatible with the run-time library of your DLL. For example, if your DLL uses /MD or /MDd, your application should also use /MD or /MDd respectively.
Add a reference to your DLL project by right-clicking on the References folder in the Solution Explorer and selecting Add Reference. In the Add Reference dialog box, select Projects from the left pane and check your DLL project from the right pane. Click OK.
Include the header file that contains the declarations of your DLL functions in your application source files. You can use #include \"filename.h\" if your header file is in the same directory as your source files, or #include if your header file is in a different directory.
Call your DLL functions from your application code as you would normally do. You can also use LoadLibrary and GetProcAddress functions to dynamically load and access your DLL functions at run time.
Tips and Tricks
Here are some tips and tricks for using multi-threaded DLLs in Visual Studio 2012:
All modules passed to a given invocation of the linker must have been compiled with the same run-time library compiler option (/MD, /MT, aa16f39245