Use of. Def files in DLL

Source: Internet
Author: User

There are two methods to declare the export function in dll:

One is to add_ Declspec (dllexport ),Examples are not provided here;

Another method is to use the module definition (. Def) file declaration, and the. Def file provides the link for the linkerProgram.

First create a DLL program, in. cpp
Int _ stdcall add (int numa, int numb)
{
Return (NUMA + numb );
}

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

Create a. Def file and add

; Dlltestdef. Lib: export the DLL Function
; Author :----
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.