Link of dynamic libraries in C ++ Builder

Source: Internet
Author: User
Tags import database

 

From: http://www.80diy.com/home/20010802/23/221314.html

Link of dynamic libraries in C ++ Builder

Xie Weicheng and Wang Bin

There are two main ways to link a dynamic library to an application: implicit link and explicit link. Implicit links are commonly used.

If the application and dynamic library are compiled on different development platforms, the dynamic library Import and Export (lib file) it may be incompatible with the import/export format required by the application development platform, so that the program Link error: contains invalid OMF record appears when the application is implicitly linked to the dynamic library. For example, the above error occurs when you connect a dynamic library created by Visual C ++ on the C ++ Builder development platform. To solve this problem, you can use the explicit connection method and the Import and Export generate tool provided in C ++ Builder.

Explicit connection: the explicit connection does not need to add the Import and Export files and corresponding header files to the project. Instead, you only need to put the dynamic library under the specified directory. In an application, you can call a function to explicitly load and unload the DLL, and call the export function of the DLL through the function pointer.

Steps:

1. Call the LoadLibrary function to load the DLL and obtain the module handle;

2. Call the GetProcAddress function to obtain the pointer of the specified export function;

3. Use the pointer of this function to call this function;

4. Use FreeLibrary to release the DLL.

Example:

Use VC ++ to create a dynamic library:

Select new → Project → Win32-Dynamic-Link Library, add addit. cpp

Extern "C ″

{Void-declspec (dllexport) addit (int a, int B, int * c)

{* C = a + B ;}

}

Addit. h

Extern "C ″

{Void addit (int A, int B, int * C );}

Compile the link to generate addit. dll and addit. Lib. Call the addit function in the C ++ builder program.

In the C ++ builder program:

{Hinstance handle; // handle of the dlla Module

Farproc lpfarproc;

Void (* lpaddit) (INT, Int, int *); // pointer to the addit Function

Int ntemp;

Handle = loadlibrary ("addit. dll"); // load addit. DLL to obtain the handle of this database.

// Addit. dll is located in the current directory

Lpfarproc = getprocaddress (handle, "addit"); // obtain the pointer to the addit function.

Lpaddit = (void (-cdecl *) (INT, Int, int *) lpfarproc; // pointer type conversion

Lpaddit (2, 3, & ntemp); // use the addit Function

Freelibrary (handle); // remove addit. dll from the program

}

This program is passed in VC ++ 5.0 and C ++ builder 3.0.

Use the Import and Export generate tool provided in C ++ Builder: pre-processing and implicit link.

Steps:

1. Use the implib.exe tool of c00000000builder.exe to regenerate the Import and Export (xxx. lib) of the dynamic library (xxx. dll ). The command is as follows:

Implib addit. lib addit. dll.

Addit. dll is an existing dynamic library, and addit. lib is the imported database to be generated. The resulting import-to-database addit. lib format is compatible with the C ++ Builder development platform;

2. In the header file addit. h of the dynamic library, re-describe its output function. The statement is as follows:

Extern-stdcall void addit (int a, int B, int * c );

3. then use the implicit link method to re-generate the import database (addit. lib) and the re-described header file (addit. h) add it to the project of the C ++ Builder application for compilation and connection.

Addit. dll follows the preceding steps and passes

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.