|
The Win32 DLL are the original Dynamic
Linked Libraries introduced with Windows. The Win32 API is made of a
collection of DLLs of this type. These DLLs consist of C++ code and a
collection of exported C++ symbols. Note that you can have .NET or any
kind of code in these DLLs, but if you want to be able to use them
across all Windows platforms, make sure your code is C++ compliant code
or code that uses C++ compliant libraries.
Initially, Win32 DLLs only supported exporting functions, but after
years of evolulion of Microsoft specific C++ code, current Win32 DLLs
support the export of functions, variables and classes. Luckily, this
type of DLL is supported by all versions of Windows programs.
In this demo, only functions are exported using the original Definition
file, but it is simple to export more symbols other than functions when
not using the definition file. Read
Microsoft MSDN
article about DLLs in Visual C++ to find out more.
Creating a Win32 DLL with Visual Studio 2013
- New
- Visual C++ > Win32 Project
- Enter Name, Location and Solution Name
- In dialog "Application Settings" select:
a. DLL
b. Export Symbols
- Right click project name.
- Select: Add -> New Item
- In dialog "Add New Item" select:
Code > Module-Definition File
(.def)
- Enter Name. Make sure Location is appropriate.
- Open the new def file and fill out the following
template:
LIBRARY
{library name}
DESCRIPTION "{description}"
EXPORTS
{export_function_1}
@1
{export_function_2}
@2
{export_function_3}
@3
...
e.g.:
LIBRARY
EUCLID
DESCRIPTION "Implements Euclidian algorithms to find gcd and remainder
inverses."
EXPORTS
GCD @1
INV @2
- Add the functions you want to export into the header
file as such:
extern "C" {return_value} __stdcall {export_function_1}([{parameters}]);
extern
"C" {return_value} __stdcall {export_function_2}([{parameters}]);
...
e.g.:
extern
"C" int __stdcall GCD(long n1, long n2);
extern "C" int __stdcall INV(long n1, long n2);
- Implement the functions in the source file by
including your header file after any precompiled header.
extern
"C" {return_value} __stdcall {export_function_1}([{parameters}])
{
{code here}
}
extern
"C" {return_value} __stdcall {export_function_2}([{parameters}])
{
{code here}
}
...
e.g.:
extern
"C" int __stdcall GCD(long n1, long n2)
{
//your code here
}
extern
"C" int __stdcall INV(long n1, long n2)
{
//your code here
}
- Compile your project.
Where to go next?
|