Write a. Def file so that the connected database calls a unified naming rule

Source: Internet
Author: User

<! -- Startfragment -->
If you want to write your own dynamic library for more programming languages, you can use the definition file and winapi macro to write your own dynamic api library. You will find that using such dynamic library output functions is just like using API functions.

I. Why do I need to use the def file?

Because Microsoft's MFC dynamic library is created using def files.
The answer is good enough, but it's a bit overwhelming. Next, let me talk about how to use the def file.
No matter how C or C ++ is used to compile a dynamic library, the compiler generates a corresponding modifier name for each function or even variable (I translated it like this. The original file is the decorated names). The connector connects the compiled target code to a DLL, and the output function name or variable name is still the modified name after compilation. The modifier name is related to the compiler. That is to say, your source program is C and the modifier name generated is the same. If your source program is C ++, the modified name is a different form. (For the discussion of modifier names, I will put them in a separate chapter. Please wait. The grain and grass are not active, and the advertisement goes first. Really...) and our application habit is to directly use the function name, rather than modifying the name, which we always do when using the API. The problem arises. For example, if you use a dynamic library written by vc6 in VB6: You first use the function name in VB6 to describe the function you want to call, and then write the call code, in the next run, your VB6 will tell you that it cannot find the entry point of the function you just described in the dynamic library, and your program refuses to execute. What should I do? There are at least two ways to solve the problem: 1. Modify the description of the dynamic library output function in your VB6 code, and add the modified name of the dynamic library output function in the alias column. 2. Modify your dynamic library and add a Def file, use the exports item of the def file to output your dynamic library functions (simply list the function names you want to output ). Though, the problem is solved (not yet solved? Is it possible? It is possible. But stop it here, and then let's talk about it .), However, it is necessary for us to further discuss this issue. Most of the purposes of writing dynamic libraries are to make our dynamic libraries more widely used than a programming language. I like to use VB to write interfaces, VC is used to do more important work, such as data analysis and access to hardware ports. In addition, the dynamic library written by VC is rarely changed, and the interface written by VB is changed. There is only one purpose to say so much. Writing a dynamic database should focus on the "big picture" and everything should comply with the "standard ". What is the overall situation? The overall situation is the path to sustainable development, that is, reuse (as if political reports are being prepared ). What are standards? It is compliant with the API standard (that is, using the def file output function, just like Microsoft's MFC dynamic library ).
In fact, the main purpose of using the def file to output functions is to remove the function modifier name generated by the compiler and use a more natural, understandable, and easily remembered name, instead of modifying the name to output the function. The name here can not be a function name. In this case, the name format of the def file must be used. However, due to habits, in most cases, only the function name is used, because this is the simplest and easier way. Is there any suspicion of laziness? In my understanding, the name format of the definition file is essentially the same for listing function names and other output names. Directly listing the function name is equivalent to "function name" = "function modifier name", but you can ignore the part after the equal sign, and the connector automatically completes function entry matching and setting. Once you decide to use non-letter
For multiple other name output functions, the complete format must be written, that is, "function output name" = "function modifier name to be output". The part after the equal sign must be correctly written, otherwise, the connection will fail. For example, assume that a function in the dynamic library is described as follows,
Int winapi testadd (int A, int B)
{
Return (A + B );
}

The exports section of the def file is described as follows,
Exports
Testadd
Add =? Testadd @ yghhh @ Z

Here, testadd and add actually point to the same entry. If testadd and add are called in the vbprogram, the result is the same.

2. Why use winapi Macros?
Take a look at the example above and add a winapi macro in front of the function. This is very important. It is directly related to the modified name of the function output. Using the testadd function of the winapi macro, the corresponding output modifier name is "? Testadd @ yghhh @ Z ".
Why use winapi? This involves another feature of the dynamic library, the call protocol (calling convention ). Without a certain protocol, dynamic library calls are unimaginable. Common dynamic library call protocols include:
_ Cdecl
_ Stdcall
_ Fastcall
These protocols have their respective strengths, which are not described here. When talking about how to solve the dynamic library written by Vc by using vbprogram, we listed two solutions, but they may not be implemented. They also depend on the call protocol used. VB follows the Pascal protocol. If the corresponding protocol is not used in the dynamic library, the vbprogram will report "Call Protocol Error" during execution ". The Pascal protocol has been deprecated in vc6 and replaced by _ stdcall, the standard call protocol, which is also a common protocol supported by most 32-bit programming languages. In Windows. H, winapi is also defined as _ stdcall. The reason for using winapi is that it can express more information-the output function (the Call Protocol) defined in this way is the same as the Windows API function (the call protocol.
Other reasons for using the winapi macro: you only need to add the macro before the defined function, and you do not need to ignore various settings related to the call Protocol each time you connect. Besides, you may not need to output all the defined functions. To improve the execution speed, you may use _ fastcall to define functions that are not output. To use variable parameters, you may use _ cdecl to define some non-output functions, or some other reasons ...... note that the default call protocol of VC is _ cdecl. If you directly use the def file output function without modifying the call protocol, the compilation connection will not go wrong, but the VB call will definitely go wrong. If you use a winapi macro, you do not have to pay attention to this. The compiler automatically uses the definition of winapi to replace the related settings in the integration environment. Here, the priority of the description before the function is the highest.
Let's look at the above discussion about the modifier of the output function. The modifier mentioned above is related to the language, and it is also related to the call protocol. If you need to use a non-function name for output, you must be clear about the call protocol and language type you are using, that is, you must be clear about the naming rules for modifying the name, or you are using some techniques, make dumpbin. exe tool to help.

Iii. Summary
In a word, if you want to create your own standard API dynamic library, we recommend that you use winapi to describe the function you want to output and then use the definition file to output it.

 

 

What I want to say:

Add the XXX. Def content in the working root directory (not Debug), for example:

Library dll2

Exports
Add
Subtract

In this way, the database can be dynamically connected without writing the inverted ID. If the name is changed, the file is taken away.

For more call methods.Inside the C/C ++ function call Method

 

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.