First, DLL Overview:
The dynamic-link library (Dynamic-link Library), referred to as a DLL, is a very important component of Windows programming. There are many advantages to using DLLs:
(1) A dynamic connection using a DLL is not a copy of the library code, only the entry points and interfaces for functions are recorded in the program, the library code is loaded into memory when the program is executed, and if more than one program uses the same DLL, it saves memory overhead by simply loading the DLL in memory.
(2) DLL is a windows-based program module that contains not only executable code, but also data, various resources, and expands the scope of use of library files.
(3) For a large, constantly updated application, many repetitive functions can be written into a DLL, called by the main program, so that both reduce the workload of development, but also improve access speed.
(4) DLLs enrich the programming capabilities of the PowerScript language, and can use functions outside of the PowerScript function set. For example, a user may write an image processing system to browse the file and store it in a database, the application needs to use a browser, but the PowerScript language does not provide a function to do this kind of work, in order to manipulate the device, the environment needs to communicate with the device itself, the environment is generally provided by the
(5) DLLs are independent of the programming language, and most Windows programming environments allow the main program to call functions in the DLL. That is, VC + +, VB, PowerBuilder, Delphi, assembly language, and so on to establish a DLL, and then in different languages compiled applications to call it.
The following is an example to illustrate the connection between PowerBuilder program and VC + + program through a DLL.
Second, VC + + Create DLL
vc++5.0 can be automatically generated with MFC AppWizard WIN32 DLL and MFC AppWizard DLL, after the start of VC, from File | New menu item, select the projects tag in the newer dialog box, select the Project as MFC AppWizard (DLL), enter project name Pbdll, click OK button, pop Up Figure 1-1 dialog box, select DLL type: Regular DLL using Shared MFC DLL, then click the Finish button. In this way, AppWizard will automatically create a DLL framework that contains the basics, and then add the code manually.
Open the Pbdll.cpp file and add the following function:
extern "C" _declspec (dllexport)
int PASCAL out_num (int x)
{
int y;
Char msg[30];
Y=x > >2;
sprintf (msg, "calculated result:%o!", y);
:: MessageBox (NULL, MSG, "Information", MB_OK);
return 0;
}
The above declaration function Out_num (), the input parameter is an integer number, after the shift operation, uses the dialog box to display the calculation result. Then open the Pbdll.def file, enter the function description Out_num under Exports, and the file reads as follows:
LIBRARY "Pbdll"
DESCRIPTION ' Pbdll Windows Dynamic Link Library '
Exports
; Explicit exports can go
Out_num @1
The final compilation generates the dynamic link library Pbdll.dll.
Third, in the PowerBuilder call VC + + created DLL function
In order to invoke the function Out_num () that resides in the dynamic link library Pbdll.dll in the PowerBuilder application, you need to declare it in advance. Be clear when declaring:
(1) Whether the function scope is global or local. A global declaration enables the function to be used for the entire application, and the local declaration makes the function valid only in the script of the object.
(2) You must indicate the type of data returned by the function, the number and type of parameters passed, and tell PowerBuilder the name of the dynamic link library that holds the function.
After the declaration is complete, the application can call the function Out_num (), and the code that calls the function is the same as calling any standard PowerScript function, except that the function is stored in a dynamic-link library, and the dynamic link library is loaded into memory when it is invoked, and the function executes.
The following is the declaration of use of the invoked function in PowerBuilder:
Function int out_num (int irow) LIBRARY "Pbdll.dll"
The function out_num () code called in PowerBuilder is as follows:
int Irow,retcode
Irow=dw_1.rowcount ()
Retcode=out_num (IRow)
Iv. Loading DLLs
When an PowerBuilder application calls an external function, the dynamic link library is loaded into memory. The operating system looks for dynamic link libraries in the following locations:
(1) EXE file running directory.
(2) The system directory of Windows.
If the DLL is not found, the application generates a run-time error, displaying a dialog box, shown in Figure 1-2 (abbreviated). If the DLL is found, it is mapped to the program's memory space.
V. Concluding remarks
DLLs provide a convenient connection between applications in different programming environments, save memory, improve speed, and enrich the programming capabilities of PowerScript languages.