first, the creation of a DLL
Create Project: Win32->win32 project, Name: Mydll
Select DLL (D)-> complete.
1. New header file testdll.h
The testdll.h code is as follows:
| 1 2 3 4 5 6 7 8 9 10 11 12-13 |
#ifndef testdll_h_ #define TESTDLL_H_ #ifdef mylibdll #define mylibdll extern "C" _declspec (dllimport) #else #define Myli bdll extern "C" _declspec (dllexport) #endif mylibdll int Add (int plus1, int plus2); Can also write like this://extern "C" {//_declspec (dllexport) int Add (int plus1, int plus2); #endif |
2. New source file Testdll.cpp
The Testdll.cpp code is as follows:
| 1 2 3 4 5 6 7 8 9 |
#include "stdafx.h" #include "testdll.h" #include <iostream> using namespace std; int Add (int plus1, int plus2) {int add_result = Plus1 + plus2; return add_result;} |
3. New module definition file mydll.def
The MYDLL.DEF code is as follows:
| 1 2 3 |
LIBRARY "Mydll" exports Add @1 |
4. VS2010 automatically creates the Dllmain.cpp file, which defines the entry point of the DLL application.
The Dllmain.cpp code is as follows:
Dllmain.cpp: Defines the entry point for a DLL application.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
#include "stdafx.h" BOOL apientry DllMain (hmodule hmodule, DWORD Ul_reason_for_call, LPVOID lpreserved) {switch (ul_reason_for_call) {case Dll_process_attach:case dll_thread_a Ttach:case dll_thread_detach:case Dll_process_detach:break; return TRUE; } |
Finally, the compilation generates MyDLL.dll files and MyDLL.lib files.
1>------Started Build: Project: Mydll, configuration: Debug Win32------
1> Dllmain.cpp
========== Generation: 1 success, 0 failures, latest 0, skipping 0 ==========
1>------Started Build: Project: Mydll, configuration: Debug Win32------
1> Stdafx.cpp
1> Testdll.cpp
1> MyDLL.cpp
1> is generating code ...
1> is creating a library D:\Visual c++\ project \libaray\mydll\debug\mydll.lib and object D:\Visual c++\ engineering \libaray\mydll\debug