Creating c , c++ dll file in windows platform, example program and tutorials
Share
C and C++Microsoft Visual C++ (MSVC) provides a number of extensions to standard C++ which allow
functions to be specified as imported or exported directly in the C++ code; these have
been adopted by other Windows C and C++ compilers, including Windows versions of GCC.
These extensions use the attribute __declspec before a function declaration. When
external names follow the C naming conventions, they must also be declared as
extern "C" in C++ code, to prevent them from using C++ naming conventions.
Besides specifying imported or exported functions using __declspec attributes,
they may be listed in IMPORT or EXPORTS section of the DEF file used by the project.
The DEF file is processed by the linker, rather than the compiler, and thus it is not
specific to C++.
DLL compilation will produce both DLL and LIB files. The LIB file is used to link
against a DLL at compile-time; it is not necessary for run-time linking. Unless your
DLL is a COM server, the DLL file must be placed in one of the directories listed in
the PATH environment variable, in the default system directory, or in the same directory
as the program using it. COM server DLLs are registered using regsvr32.exe, which places
the DLL's location and its globally unique ID (GUID) in the registry. Programs can then
use the DLL by looking up its GUID in the registry to find its location.
#include <windows.h>
// DLL entry function (called on load, unload, ...)
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
return TRUE;
}
// Exported function - adds two numbers
extern "C" __declspec(dllexport) double AddNumbers(double a, double b)
{
return a + b;
}
C and C++Make sure you include Example.lib file(assuming that Example.dll is generated) in the
project (Add Existing Item option for Project!) before static linking. The file
Example.lib is automatically generated by the compiler when compiling the DLL. Not
executing the above statement would cause linking error as the linker would not know
where to find the definition of AddNumbers. You also need to copy the DLL Example.dll
to the location where the .exe file would be generated by the following code.
#include <windows.h>
#include <stdio.h>
// Import function that adds two numbers
extern "C" __declspec(dllimport) double AddNumbers(double a, double b);
int main(int argc, char *argv[])
{
double result = AddNumbers(1, 2);
printf("The result was: %f\n", result);
return 0;
}
// C and C++
#include <windows.h>
#include <stdio.h>
// DLL function signature
typedef double (*importFunction)(double, double);
int main(int argc, char **argv)
{
importFunction addNumbers;
double result;
// Load DLL file
HINSTANCE hinstLib = LoadLibrary("Example.dll");
if (hinstLib == NULL) {
printf("ERROR: unable to load DLL\n");
return 1;
}
// Get function pointer
addNumbers = (importFunction)GetProcAddress(hinstLib, "AddNumbers");
if (addNumbers == NULL) {
printf("ERROR: unable to find DLL function\n");
FreeLibrary(hinstLib);
return 1;
}
// Call function.
result = addNumbers(1, 2);
// Unload DLL file
FreeLibrary(hinstLib);
// Display result
printf("The result was: %f\n", result);
return 0;
}
Comments:
|
Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
|