Module definition file. Def

Source: Internet
Author: User

Role 1

There are two methods to declare the exported function in dll: one is to add _ declspec (dllexport) to the function declaration, which is not described here; another way is to use the module definition (. def) file declaration ,. the def file provides the linker with information about export, attributes, and other aspects of the linked program.

In the linker stage, you can use the/DEF (specified module definition file) linker option to call the. Def file. If the generated. EXE file is not exported, using the. Def file will increase the output file and reduce the loading speed.

IICause

In VC ++, you can generate a DLL without using the. Def file. You only need to add _ declspec (dllexport) before the Function Definition of VC ++. However, using _ declspec (dllexport) is different from using a. Def file. If the DLL is provided to VC ++ users, you only need to provide the. Lib generated during DLL compilation to users. It can easily call your DLL. However, if your DLL is used by other programs such as VB, Delphi, and. Net users, it will cause a small headache. Because VC ++ converts the name of a function declared by _ declspec (dllexport), the following functions are provided:

_ Declspec (dllexport) int _ stdcalliswinnt ()

Will be converted to [email protected], so that you must declare in VB as follows:

Declare function iswinnt lib "My. dll" alias "[email protected]" () as long

The number following @ may vary depending on the parameter type. This is obviously inconvenient. If you want to avoid this conversion, you must use the. Def file method.

The number after exports can be left blank, and the system will automatically allocate a number. For VB, Pb, and Delphi users, the method of calling by name is usually used. This number does not matter, but is used. for the VC program linked to Lib, it is not called by name, but by this number, so it is best to give.

3.Use

Example 1: Use vc6.0 to create a DLL (Dynamic Link Library) without using the. Def file.

# Ifndef lib_h

# Define lib_h extern "C" int _ declspec (dllexport) add (int x, int y );

# Endif

For the. Def file, you can add privat to library "xxx_dll" exports.

Example 2: Create a DLL program in. cpp.

Int _ stdcall sub (int numa, int numb)
{
Return (NUMA-Numb );
}

Create a. Def file and add

; Dlltestdef. Lib: export the DLL Function
;----
Library dlltestdef
Exports
Add @ 1
Sub @ 2

Finally, create a test program:. cpp file as follows:
# Include <iostream>
# Include <windows. h>

Using namespace STD;

Typedef int (_ stdcall * Fun) (INT, INT );
Hinstance;
Fun and fun;

Int main ()
{
Hinstance = loadlibrary ("dlltestdef. dll ");
If (! Hinstance)
Cout <"not find this DLL" <Endl;
Fun = (fun) getprocaddress (hinstance, makeintresource (1 ));
If (! Fun)
{
Cout <"not find this fun" <Endl;
}
Cout <fun (1, 2) <Endl;
Freelibrary (hinstance );
Return 0;
}



Note:
The. Def file rules are as follows:

(1) The Library statement describes the DLL corresponding to the. Def file;

(2) Name of the function to be exported after the exports statement. You can add @ n after the export function name in the. Def file to indicate that the sequence number of the function to be exported is n (this sequence number will play its role during function calling );

(3) The annotation in the def file is specified by the semicolon (;) at the beginning of each comment line, and the comment cannot share a line with the statement.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.