Use definition files and WinAPI macro to write your own API dynamic library

zhaozj2021-02-17  49

If you want your own dynamic libraries you can apply more other programming languages, you can use the definition file and WinAPI macro to write your own API dynamic library. You will find that using such a dynamic library output function is like using the API function. 1. Why use the DEF file? Because Microsoft's MFC dynamic library is created using a DEF file. This answered enough to have a share, but a bit of a big hat is taking the feeling of people. Below, I specifically use the DEF file. Regardless of the C language or C language to write dynamic libraries, the compiler generates a corresponding modification for each function or even variables (I translated it. The original is the decorated names), the connector will compile the target code. Connecting into a DLL, its output function name or variable name is still compiled. And the modification is related to the compiler, that is, your source program is C. The generated modifier is a look; if your source program is C , the generated modifier is another look. (About the discussion of the modified name, I will put it in a separate chapter, please wait. Food and grass, the advertisement first. True ...) and our application habits are direct use of the function name, not the name We have always been this when we use the API. Then, the problem is coming, such as using the dynamic library written in VC6 in VB6: You first use the function name in VB6 to describe the function you want to call, then write the call code, then run, your VB6 Will tell you that it can't find the entry point of the function you just described in the dynamic library, and your program refuses to execute. How to do? There are at least two ways to solve the problem: 1. Modify the description section of the dynamic library output function in your VB6 code, add the modulation name of the dynamic library output function to the alias column. 2. Modify your dynamic library, add a DEF file, and use the exports item of the DEF file to output your dynamic library function (simply listing the function name you want to output underneath). Although this problem is solved (there is still not solved? Is it possible? It is possible. But here to stop, then say.), But we need to discuss this issue further. Most of our purpose of writing a dynamic library is to let our dynamic library are not limited to a programming language. It is to be more extensively applied. I like to write interface with VB, and use VC to complete more important work, such as Analyze the data, access the hardware port, etc., and the VC written dynamic library is rarely changed, and the interface written by the VB is another change. Said so much, the purpose is only one, and the dynamic library should focus on the "overall situation". What is the overall situation? The overall situation is the road to sustainable development, which is multiplexed (as if it is doing political reports). What is the standard? It is the standard that conforms to the API (that is, using the DEF file output function, like Microsoft's MFC dynamic library). In fact, one of the most important purposes using the DEF file to output functions is: Remove the functions generated by the compiler, use more natural, easy to understand, easy memory name instead of the modification name to output functions. The name here may not be a function name, and the Name format of the DEF file is required. But because of habits, most cases, only function names are used because it is the easiest thing to save. Is there a lazy suspicion? My understanding, whether it is a log list, or other output name, it is the same, that is, the Name format of the definition file.

Direct Russencing function name, it is equivalent to "function name" = "function modifier", but can only ignore the part behind the equal number, and the connector automatically completes the matching and setting of the function portal. Once you decide other name output functions of the non-function name, you must write a complete format, that is, "function output name" = "function modifier to output", the part behind it must be written correctly, otherwise, when connected It is not possible. For example: Assume a function in the dynamic library describes the following, int WinAPI TestAdd (Int A, INT B) {RETURN (A B);} The exports section of its DEF file is described below, exports testadd add =? Testadd @@ yghhh @Z Here TestAdd and add actually point to the same entry. If TestAdd and Add in the VB program, the result is the same. Second, why do you want to use WinAPI macro? Take a look at the example above, add a WinApi macro before the function. This is important. It directly related to the modification of the function output, using the WinApi macro TestAdd function, the corresponding output modifier is "? Testadd @@ yghhh @ z". Why use WINAPI? This involves another feature of the dynamic library, calling the protocol (CALLING Convention). If there is no certain agreement, the call to the dynamic library is unimaginable. Generally commonly used dynamic library call protocols are: __cdecl __stdcall __fastcall These protocols have their own strengths, which are not described here. The above is talked about the dynamic library written by the VB program to call VC, but it has been enumerated, but it does not necessarily be implemented, it also depends on the call protocol used. The VB follows the Pascal protocol. If the corresponding protocol is not used in the dynamic library, the VB program will report "call protocol error" when the VB program is executed. The Pascal protocol has been discarded in VC6, replaced by __stdcall, namely standard call protocol, which is a general protocol for most 32-bit programming language support. WinAPI is also defined as __stdcall. The reason here is proposed herein is here, it can express more information - the definition output function (call protocol) is the same as the Windows API function (call protocol). Other reasons for using WINAPI macros: You only need to add this macro before the defined function, you don't have to pay attention to the settings related to the calling protocol when you connect. Moreover, you may not need to output all the defined functions, in order to improve the execution speed, you may use the function of the function without the output __fastcall, in order to use change, you may use __cdecl to define some non-output A function, or such a reason ... need to be reminded that the VC default call protocol is __cdecl. If you use the DEF file output function without modifying the call protocol, the compile connection will not be wrong, but when VB is called, it is definitely wrong. And if you use WinApi macros, you don't have to pay more. The compiler automatically uses WinAPI definitions to replace the relevant settings in the integrated environment, and the description priority before the function is highest. Going back and then look at the discussion of the modified name of the output function, which is mentioned above and the language is related to the language, in addition, it is also related to the calling protocol.

转载请注明原文地址:https://www.9cbs.com/read-30460.html

New Post(0)