Abstract: This dynamic link library module and the methods used in the rehabilitation of its VC calls, and gives a complex used by data encryption specific examples.
Key words VC DLL modules multiplexing data encryption
INTRODUCTION
Modular thinking runs through every stage in the development of software engineering, module reuse building large-scale systems is an important idea. Module multiplexing method: Function, libraries, and dynamic link libraries, COM. Are based on its modular basic idea. Function is the most simple modular thinking behind the approach is the basis of, or even the basis of an application. Library is a combination of function, the general will be a function of some similar functions together as a library, this library is usually called static libraries, the way links are static. COM that the Component Object Model, is an integrated technology, so that procedures can be in the running various software programs not related to the mix without having to consider these procedures are not relevant what language prepared, it is a standard or call agreement, will be responsible for a software module and another linking software. Dynamic link library DLL (Dynamic Link Library) is an application can be shared by other procedures module, which encapsulates some of the routines can be shared and resources, their approach is dynamic links. Dynamic link library files is generally dll extension, and may also be fon, sys and dry, and executable files (. Exe) are very similar, although the difference is in DLL contains the executable code are not carried out separately, but should be Windows applications directly or indirectly call. Windows operating systems contain a large number of dynamic link library, the most important is KERNEL32.DLL, USER32.DLL, GDI32.DLL.
DLL calls
DLL calls, first of all, need to DLL files image to the user process's address space, before a function call, the function and process within the general function to be called the same way. Windows DLL will be provided both to the image of the process address space:
1, the loading implicit links
This method needs DLL works compiler generated LIB documents, this document contains DLL allows applications to transfer all functions of the list to find links for applications deployed LIB document sets out a certain function, will be in the Application procedures for the executable file of the document image to add some information, which pointed out this function contains the name of the DLL files. When the application is running, it is executable image files generated by the operating system, the system will check the image files of information on the DLL, and then the DLL file image to process address space.
System, through the DLL name, trying to load this file to the process address space, it has to find the DLL path in accordance with the order as follows:
Running the directory, the file can be executed directory;
The current working directory procedures
System directory: For Windows95/98, GetSystemDirectory function can be called for WindowsNT/2000, referring to the 32-bit Windows System directory, and can call to get GetSystemDirectory function, the value received for the SYSTEM32.
Windows directory
Included in the PATH environment variable of all the directory
VC DLL loaded in the LIB files with the following three methods:
① LIB files directly into the project file list
VC in an open File View, selected works, click the right mouse button, and then select "Add Files to Project" menu, in the pop-up dialog box in the paper selected to join the LIB DLL files.
② set up the Project Settings works to load a DLL file LIB
Open projects Project Settings menu, select Link, and then Object / library modules text box under the LIB DLL files.
③ way through the code
Join pre-compiler directive # pragma comment (lib, "*. lib"), the advantages of this method can be used pre-conditions compiler directive LIB links different versions of the document. Because, in Debug mode, the LIB document is Debug version, such as Regd.lib in Release mode, the LIB document Release version, such as Regr.lib.
When the application DLL on the LIB document loading, the need to head the corresponding DLL files (*. h) to include them in this paper are first defined in the DLL function prototype, and then a statement.
2, the explicit link operation
Although implicit links to achieve relatively simple, but in addition to *. dll documents also need *. DLL *. h lib files and documents, provided only in those occasions *. dll files can not be used, but only by significantly linked manner. This way by calling the API function of the DLL to complete the loading and unloading, can be more effective in its use of memory, in the preparation of large-scale applications are often used this way. This approach to achieve specific programming steps are as follows:
① use Windows API function Load Library or MFC will be provided by the DLL modules AfxLoadLibrary image to the process of memory space, the DLL modules dynamically loaded.
② be used to call GetProcAddress function in the DLL function pointer.
③ not DLL, the function or use Free Library AfxFreeLibrary function from the process address space Explicit unloading DLL.
Call examples in VC
Data encryption is an important field of computer security, the basic idea is to transform information through the forms to protect sensitive information to unauthorized persons could not understand the contents of the protected information. Common data encryption algorithms: DES, IDEA, RSA, ECC, AES, MD5, SHA, etc..
"Sharing software encryption algorithms library" is a personal, business development sharing software encryption tools in support of the Windows platform under various development tools: VC, VB, Delphi, PB, VFP, the algorithm integration algorithm are: BlowFish , MD5, Secret16, AES, SHA, CRC32, RSA, DES, string and / decryption, File / decryption, and other powerful algorithms. Provide a DLL files - Reg.dll, you can reuse it for data encryption and decryption.
1, implicit links
Reg.h provided with the implicit links Reg.lib two necessary documents, it can be used this way.
① VC in an open File View, selected works, click the right mouse button, and then select "Add Files to Project" menu, in the pop-up dialog box, select the document to join Reg.lib.
② VC in an open File View, select Header files, click the right mouse button, and then select "Add Files to Folder" menu, in the pop-up dialog box in the document to join Reg.h selected, and then The first project in the document corresponding to join # include "Reg.h." Reh.h first in the paper is defined in the DLL function prototype and statements.
Such as: encryption function prototype and statements extern "C" BOOL WINAPI File Encrypt (LPCTSTR lpInputFileName, LPCTSTR lpOutputFileName, LPCTSTR lpKey, LPCTSTR lpRegisterCode); decryption function prototype and statements extern "C" BOOL WINAPI File Decrypt (LPCTSTR lpInputFileName, LPCTSTR lpOutputFileName , LPCTSTR lpKey, LPCTSTR lpRegisterCode). For one WINAPI macro, it added to the former definition of function prototype, the system it will be translated into the appropriate call, in Win32, it is translated into calls _ stdcall way.
③ called directly by the needs of encryption and decryption functions, such as call File Encrypt () function to achieve text files and binary files encrypted call File Decrypt () function to achieve binary and text files declassified documents, at the time of call parameters to the function definition parameters consistent.
2, explicit links
If only Reg.dll a document, then to be used this way.
① encryption modules: File Encrypt call () function to achieve text files and binary files encrypted.
/ / Load encryption / decryption DLL
HINSTANCE hdll =:: Load Library ( "Reg.dll");
/ / Typedef type definition language to define the function pointer types
Typedef BOOL (lpFileEncrypt _stdcall *) (LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR);
/ / Function declaration
LpFileEncrypt FileEncrypt1;
/ / File Encrypt access to encryption function of the function pointer
FileEncrypt1 = (lpFileEncrypt):: GetProcAddress (hdll, "FileEncrypt");
/ / Call DLL function in the File Encrypt encrypted document encryption, user-12345678, software registration
FileEncrypt1 (encrypted source files were generated target file encryption, passwords, "user-12345678");
/ / DLL modules released
:: AfxFreeLibrary (hdll);
② decryption modules: Call File Decrypt () function to achieve text files and binary documents declassified.
/ / Load encryption / decryption DLL
HINSTANCE hdll =:: Load Library ( "Reg.dll");
/ / Typedef type definition language to define the function pointer types
Typedef BOOL (lpFileDecrypt _stdcall *) (LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR);
/ / Function declaration
LpFileDecrypt FileDecrypt2;
/ / Decryption function access to the function pointer File Decrypt
FileDecrypt2 = (lpFileDecrypt):: GetProcAddress (hdll, "FileDecrypt");
/ / Decryption function in the DLL calls FileDecrypt on file encryption, user-12345678, software registration
FileDecrypt2 (source declassified documents were declassified documents generated objectives, passwords, "user-12345678");
/ / DLL modules released
:: AfxFreeLibrary (hdll);
Concluding remarks
Using DLL modules such multiplexing method can reduce the workload of the development of software engineering, enhanced code portability, reduce the complexity of the test module from the overall increase in the efficiency of the development of software engineering. |