Export from dll using def File

Source: Internet
Author: User

The Module definition (. def) file is a text file that contains one or more Module statements describing various DLL attributes. If you do not use the _ declspec (dllexport) keyword to export the DLL function, the DLL requires the. def file .. The def file must contain at least the following module definition statements: the first statement in the file must be a LIBRARY statement. This statement identifies the. def file as a DLL. The LIBRARY statement is followed by the DLL project name. The linker puts this name into the DLL Import and Export Database. The EXPORTS statement lists the names and, if possible, the serial number values of the DLL export function. Assign the sequence number to the function by adding the @ sign and a number after the function name. When the sequence number value is specified, the sequence number range must be from 1 to N, where N is the number of DLL export functions .. The comment 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. Create a dynamic link library DLL project 1. create a static project MathFucsDll select win32 project --> enter the MathFuncsDll project name-> next-> select DLL; select an empty project with additional options-> complete. (Or do not select an empty project with the additional options, that is, the default value) 2. create a library function [cpp] // MyMathFuncs. cpp int nDataBase = 1; // global variable int Add (int a, int B) {return (a + B) * nDataBase;} int _ stdcall Sub (int, int B) {return (a-B) * nDataBase;} int _ cdecl Multiply (int a, int B) {return a * B;} 3. create the def file [cpp] // MyMathFuncs. def // The LIBRARY and EXPORTS cannot be in lower case, and the name behind the LIBRARY must be the same as the project name; LIBRARY MathFuncsDll EXPORTS Add @ 3 Sub @ 5 Multiply @ 1 nDataBase DATA; nDataBase global variable, write-only (nDataBase); (nDataBase data); (nDataBase @ 2) create an application that references the dynamic link library. 1. create a console application that references the Dynamic Link Library: add the project MyExecRefsDll in the same solution: choose win32 console application> MyExecRefsDll Project Name> next> console application; select an empty project with additional options> finish 2. function projects that use dynamic link libraries in applications, reference-> general properties-> framework and reference-> Add reference-> the project name and project directory of MathFuncsDll are displayed-> OK (the simplest one) or, add library directory and additional library: Project, properties-> connector-> General-> additional library Directory: for example, $ (OutDir) project, property-> connector-> input-> additional dependency: MathFuncsDll. lib; or add: # pragma comment (lib, "MathFuncsDll. lib ") and the mathfuncsdll.dll.pdf and myexecrefsdll.exe files are in the same directory. program [cpp] # include "stdafx. h "# include <iostream> using namespace std; extern int _ declspec (dllimport) nDataBase; // reference the global variable in dll // declare the function, it must be consistent with the function definition in Dll (including its function call modifier) int Add (int a, int B); int _ stdcall Sub (int a, int B ); int _ cdecl Multiply (int a, int B); int _ tmain (int argc, _ TCHAR * argv []) {nDataBase = 2; cout <Add (1, 2) <endl; // 6 cout <Sub (3, 4) <endl; //-2 cout <Multiply (4, 5) <endl; // 20 return 0 ;} compare the use of _ declspec (dllexport) with the use. the similarities and differences between using the def file to export Dll functions are the same: creating a project and referencing a dynamic library are the same: when defining :. def needs to have its file (library exports), and the other needs to add _ declspec (dllexport) before its function :. def requires a function declaration before using the function, and the header file to be added when _ declspec (dllexport) is used

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.