![]() |
![]() |
Writing a managed extension DLL (Dynamic-Linked Library) in Visual C++ IDE is a bit cumbersome, but once you set the right compiler and linker options, the DLL can be shared by any other project, including managed extension DLL so that you can incorporate the newly added functionalities of the extended VC++. This page will go through the whole process of writing a DLL.
A DLL is a library module which is a set of classes, interface, structure, methods, variables, etc that can be shared with other applications. There are a few rules in writing those DLL's.
__declspec(dllexport) prefix in the DLL headers.
Likewise, __declspec(dllimport) prefix must be specified
in the main project headers.
.lib file) must be specified in the linker options.
First, select a DLL project. Go to File menu, select
Add Project, and click New Project. Select
the project type such as Managed Class Library.
This DLL project will create a DLL file and a library file. Select the managed DLL project if you want to use the managed extensions.
__declspec
The classes and function declaration in the DLL project headers must carry
__declspec(dllexport) prefix.
extern "C" __declspec(dllexport) int LoadXMLFile(char* fileName, CLabelsXML*);
The header files for the
main project must carry __declspec(dllimport) prefix.
extern "C" __declspec(dllimport) int LoadXMLFile(char* fileName, CLabelsXML*);
Build the DLL project, and a library file (.lib) and a DLL file (.dll)
will be created in the Release/Debug directory depending on the
configuration. The DLL file must be copied to the main project
directory. Here is a trick to do this without manually copying the
file to the directory -- specify a shell command to copy the DLL file
to the main project directory. Go to Custom Build Step
section, and write the copy command. The
Description entry is for the message that will be
presented as the command is executed. The name of the DLL file should
be specified in the Outputs directory.
The library file must be specified in the main project's linker
option. First, set the library path. Go to the Linker
option in the project property dialog. Under the General
option, specify the DLL project's output directory in
Additional Library Directories entry.
Then, specify the library file. In the same Linker
option, select Input option. Enter the name of the
library file in the Additional Dependencies.
Once the DLL file is copied and the linker options are set, the project is all set to go.