One, generate DLL file
1. Menu item " file "-" new "-" Project "-"Win32 dynamic-link Library". Create a project and take a name of "test" for the project. Here I choose "Create a simple DLL", at this time in the VC + + interface will have a tree-shaped file structure.
2, see this file structure is probably understand, source files is put source, that is, the. cpp suffix file; header files are files that are placed in a header file, that is, a. h suffix. This StdAfx.h and StdAfx.cpp are automatically generated, this StdAfx.h is some default configuration, this does not need us to move it. Anyway, with this file, we just need to introduce this header file in our source file (like Test.cpp).
The default files for Test.cpp are as follows:
" stdafx.h " BOOL apientry DllMain (HANDLE hmodule, DWORD ul_reason_for_call, lpvoid lpreserved ) { return TRUE;}
3, suppose now I want to implement a a+b function, and package This function as a dynamic link library (DLL) for other programs to call. The code is as follows:
#include stdafx.h " extern " _declspec (dllexport) int Fun ( int a,int b); BOOL apientry DllMain (HANDLE hmodule, DWORD ul_reason_for_call, LPVOID LpR eserved) { return int Fun (int a,int b) { return a+b;}
The extern "C" _declspec (dllexport) int fun (int a,int b) is the way to export this method, which is to provide this interface to other programs. Of course, if you want to define other methods, use this statement to expose the interface.
4, at this time, we "build" project, that is, the formation of a bit. If successful, a "Test.dll" file can be found under the Debug directory in the project directory.
Second, call DLL file
1. Menu item " file "--" project "-"Win32 Console application". Create a console app that is named "calling" for this app. At this point, the VC + + interface will appear a property file structure.
2. We put the Test.dll file generated above into the calling directory of our application, and then fill in the Calling.app file with the following code:
1#include"stdafx.h"2typedefint(*pfun) (int,int);3 4 intMainintargcChar*argv[])5 {6Hmodule Hmodule=::loadlibrary ("Test.dll");7Pfun newfun= (pfun):: GetProcAddress (Hmodule," Fun");8 intI=newfun (1,2);9printf"The result is%d\n", i);Ten :: FreeLibrary (hmodule); One return 0; A}
3, the final step to compile, run, normal to pass, but compiled pass, said "hmodule" Such a keyword is not defined. To open the standard configuration header file stdafx.h, the content defaults to the following:
#if !defined (afx_stdafx_h__94a5270f_2e93_4d92_8279_bdf69332d688__included_)#define afx_stdafx_ H__94a5270f_2e93_4d92_8279_bdf69332d688__included_#if _msc_ver >#pragma once#endif // _msc_ver >#define win32_lean_and_mean // Exclude Rarely-used stuff from Windows headers<stdio.h>
We add this file as follows:
#if !defined (afx_stdafx_h__9cd74ea8_ac0b_4487_98d5_e8704b271e95__included_)#define afx_stdafx_ H__9cd74ea8_ac0b_4487_98d5_e8704b271e95__included_#if _msc_ver >#pragma once#endif // _msc_ver >#if def_debug#define win32_lean_and_mean // Exclude rarely-used stuff from Windows headers#endif<stdio.h> <Windows.h>
This will make it possible for the application to invoke the method defined in the DLL file.
vc++6.0 Generating DLLs