Creation of non-MFC dynamic libraries ...
A lib.cpp, a lib.h.
/*lib.h*/ #ifndef lib_h #define LIB_H //declares the export function of add as DLL. extern "C" int _declspec (dllexport) Add (int x,int y); #endif
You can also add def files
; Lib.def: Export DLL function library Dlltestdef Exports add @ 1
Calls to non-MFC dynamic libraries
1. Static Calls
Static call DLL dynamic library, the DLL and LIB files have been placed under the corresponding working path #include <stdio. h> #pragma comment (lib, "DllTEST.lib")//. Tells the compiler where to put the Lib file associated with the DLL. extern "C" _declspec (dllimport) Add (int x,int y);//Declaration import Function int main () { int result=add (2,3); printf ("2+3=%d \ n", result); return 0; }
2. Dynamic invocation
Dynamic Call DLL dynamic-link library. Just put the DLL in the appropriate working directory to #include <stdio.h> #include <windows.h> // Defines a function pointer type that is the same as the add function parameter type and the return pointer type. The typedef int (*lpaddfun) (int,int) is then instantiated in the main function;//macro definition function pointer type int main () { hinstance hdll;//dll handle Lpaddfun addfun;//Instantiates the function pointer type hdll=loadlibrary ("./dlltest.dll") above, and//dynamically loads the DLL module and assigns the module handle of the DLL to hDLL. if (hdll!=null) { addfun= (lpaddfun) GetProcAddress (hDLL, "add"),//The address of the Add function in the DLL module is given to Addfun if ( Addfun!=null) { int result=addfun (2,3);//Addfun to invoke the Add function printf ("2+3=%d \ n", result); } FreeLibrary (hdll);//unload DLL dynamic module } return 0;
vc++6.0 creation and invocation of dynamic libraries (non-MFC DLLs)