|| 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 ||
|作者：佚名 文章来源：不详 点击数： 更新时间：2007-7-3|
DLLs: An Overview
Dynamic linking differs from static linking in that it allows an executable module (either a .DLL or .EXE file) to include only the information needed at run time to locate the executable code for a DLL function. In static linking, the linker gets all the referenced functions from the static link library and places it with your code into your executable. Using DLLs instead of static link libraries makes the size of the executable file smaller. If several applications use the same DLL, this can be a big savings in disk space and memory.
A .DLL file has a layout very similar to an .EXE file, with one important difference - a DLL file contains an exports table. The exports table contains the name of every function that the DLL exports to other executables. These functions are the entry points into the DLL; only the functions in the exports table can be accessed by other executables. Any other functions in the DLL are private to the DLL.
You can export functions from a DLL with two methods:
1) Create a module definition (.DEF) file and use the .DEF file when building the DLL.
The __stdcall calling convention is used to call Win32 API functions.
Argument-passing order Right to left.
Argument-passing convention By value, unless a pointer or
Stack-maintenance responsibility Called function pops its own
Name-decoration convention An underscore (_) is prefixed to the
Case-translation convention None
In the following example, use of __stdcall results in all WINAPI function types being handled as a standard call:
Export from a DLL Using .DEF Files
The most common methods for exporting a definition:
1.The __declspec(dllexport) keyword in the source code
You can export data, functions, classes, or class member functions from a DLL by using the __declspec(dllexport) keyword. If you use __declspec(dllexport), you do not need a .DEF file for exports.
void __declspec(dllexport) __cdecl Function1(void);
To export all the public data members and member functions in a class, the keyword must appear to the left of the class name as follows:
class __declspec(dllexport) CExampleExport : public CObject
When building your DLL, you typically create a header file that contains the function prototypes and/or classes you are exporting, and add the __declspec(dllexport) to the declarations in the header file. To make your code more readable, define a macro for __declspec(dllexport) and then use the macro with each symbol you are exporting:
#define DllExport __declspec( dllexport )
Pros and Cons of Using __declspec(dllexport)
Using __declspec(dllexport) is convenient because you do not have to worry about maintaining a .DEF file and obtaining the decorated names of the exported functions. However, you do not have control over the export ordinals that the compiler generates. This method is suitable if, for example, you are designing a DLL for use with an application that you control; if you rebuild the DLL with new exports, you will also have to rebuild the application.
Exporting functions using .DEF file
A module-definition (.DEF) file is a text file containing one or more module statements that describe various attributes of a DLL. If you are not using the __declspec(dllexport) keyword to export the DLL's functions, then the DLL requires a .DEF file.
A minimal .DEF file must contain the following module-definition statements:
The first statement in the file must be the LIBRARY statement. This statement identifies the .DEF file as belonging to a DLL. The LIBRARY statement is followed by the name of the DLL. The linker places this name in the DLL's import library.
For example, a DLL that contains the code to implement a binary search tree might look like the following:
If you are exporting functions in a C++ file, you will have to either place the decorated names in the .DEF file or define your exported functions with standard C linkage by using extern "C". If you need to place the decorated names in the .DEF file, you can obtain them by using the tool DUMPBIN or by using the linker switch /MAP. Note that the decorated names produced by the compiler are compiler specific. If you place the decorated names produced by the Visual C++ compiler into a .DEF file, applications that link to your DLL must also be built using the same version of Visual C++ so that the decorated names in the calling application match the exported names in the DLL's .DEF file.
Pros and Cons of Using .DEF Files
Exporting functions in a .DEF file gives you control over what the export ordinals are. When you add additional exported functions to your DLL, you can assign them higher ordinal values (higher than any other exported function). When you do this, applications using implicit linking do not have to relink with the new import library that contains the new functions. This is very important, for example, if you are designing a third-party DLL for use by many applications. You can continue to enhance your DLL by adding additional functionality while at the same time ensuring that existing applications will continue to work properly with the new DLL.
Another advantage to using a .DEF file is that you can export functions using the NONAME attribute, which places only the ordinal in the exports table in the DLL. For DLLs with a large number of exported functions, using the NONAME attribute can reduce the size of the DLL file.
To determine which method to use to export functions (a .DEF file or the __declspec(dllexport) keyword), answer the following questions:
1/ Will you be continuing to add additional exported functions?
Writing a source code
long __declspec(dllexport) __stdcall foo3(long mylong1, long mylong2,long mylong3)
long __declspec(dllexport) __stdcall foo3(long mylong1, long
//"Undecorates " functions names decorated by __stdcall
Note: the name of the function used in the declaration section should be the same as exported function name. If the .DEF file was not included within the .DLL, the function will be exported decorated and has to be prototyped decorated as well.
//Prototype function call in Local or Global External Functions Declaration section:
// Calling function foo4()
ll_arg1 = Long(sle_arg1.text)
//Calling function foo3()
ll_arg1 = Long(sle_arg1.text)
To call subroutine foo5(), which takes a structure by reference as a parameter, create a structure:
mystruct pb_struct //Instanciate mystruct
//Display results of subroutine calculations
Setting the Byte Alignment on MSVC++
Microsoft compilers provides two methods to specify structure packing: a command-line option (/Zp) and a pragma (pack).
Technical Document 44474: External Function Calls;
9 7 3 1 2 3 4 5 4 8 :
用PB5 设计 串口通讯程序
PB连接Microsoft SQL S…
|| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 ||