A dynamic link library reflection method and system based on a C language preprocessor
By using a C language preprocessor to build a registry and generate proxy functions, the compatibility issues of dynamic link libraries during the linking process and runtime are resolved, enabling seamless integration of different version library modules.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SOUTH SURVEYING & MAPPING INSTR
- Filing Date
- 2023-03-09
- Publication Date
- 2026-07-10
AI Technical Summary
During the linking process of dynamic link libraries developed in C/C++, compatibility issues can arise due to different library modules using different syntax standards and version requirements, leading to unpredictable errors during compilation and runtime.
A registry is built using a C language preprocessor to generate proxy functions. Interface proxies for dynamic link libraries are automatically generated during compilation. Parameter types and names are forwarded using macro definitions, masking specific version differences of dynamic link libraries and thus resolving compatibility issues.
The compatibility issues between modules from different library versions were resolved during compilation and runtime, ensuring the software runs correctly.
Smart Images

Figure CN116400895B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of dynamic link library reflection technology, and more specifically, to a dynamic link library reflection method and system implemented based on a C language preprocessor. Background Technology
[0002] In the process of developing computer software, the linking process of dynamic link libraries developed using languages such as C / C++ has many uncontrollable aspects. This is because different library modules may use different syntax standards, different versions of products require different versions of library modules, different versions of compilers are needed for compilation, and different versions of runtime are required at runtime. This can lead to unpredictable errors in the compilation or runtime of computer software, especially when software compiled with an older version of compiler links to dynamic link libraries compiled with a newer version of compiler.
[0003] Existing technology discloses a compilation method and apparatus for compiling Java files. The method includes: obtaining a Java file package to be compiled; parsing the Java file package to obtain parsing information, wherein the parsing information is the composition information of each basic unit in the Java file package; performing an equivalent format conversion on the Java file package, wherein a custom identifier is added to the parsing information and sorted during the equivalent format conversion to obtain modification information; and compiling the modified information using a corresponding compiler. This existing technology performs compilation and conversion of Java files, but does not solve the compatibility issues that arise during runtime due to dynamic libraries in C language development during or after linking. Summary of the Invention
[0004] This invention provides a dynamic link library reflection method and system based on a C language preprocessor, which solves the compatibility problems caused by the dynamic library during or after linking.
[0005] The primary objective of this invention is to solve the aforementioned technical problems. The technical solution of this invention is as follows:
[0006] A dynamic link library reflection method implemented using a C language preprocessor includes the following steps:
[0007] S1: Obtain the filename of the dynamic link library to be proxied and the list of functions to be proxied. The list of functions includes: the function name, return type and parameter type of each function.
[0008] S2: Construct a registry using macros in the C language. The inputs of the registry are the filename of the dynamic link library, the name of the function to be proxied, the return type, and the parameter type.
[0009] S3: The compiler generates proxy functions using the registrar during the preprocessing phase of compilation.
[0010] S4: Using the registrar described in step 3, register the functions that need to be proxied in the source code;
[0011] S5: Call the function that needs to be proxied in S1 in the business, compile and run the software together with the registration code in step S4.
[0012] Furthermore, the specific process of step S3 is as follows:
[0013] S3.1: Use macro definitions to declare parameter type forwarders to implement parameter type forwarding;
[0014] S3.2: Use macro definitions to declare parameter forwarders to implement parameter forwarding;
[0015] S3.3: Use macro definitions to declare the registrar, taking the return value type, function name, parameter type, and dynamic link library name as input parameters to expand the proxy function.
[0016] Furthermore, the specific steps for declaring a parameter type forwarder using macro definitions are as follows:
[0017] S311: Implement a concatenation function that can concatenate parameters in multiple macro expansions, used to concatenate all the parameter types and parameter names after restoration;
[0018] S312: Implement a macro function that can obtain the length of the parameter list, used to determine the recursion depth for restoring the parameter list.
[0019] S313: Declare macro definitions for restoring parameter types and names for parameter lists of different lengths, recursively expand the parameter list using the recursion depth of step S312, and use the concatenation function of step S311 to concatenate the results during the recursion process to generate expanded content in a preset format.
[0020] Furthermore, the preset format mentioned in step S313 is Type1arg1, Type2arg2, Type3arg3,...TypeNargN, where N represents a positive integer.
[0021] Furthermore, the specific steps for using macro definitions to declare parameter forwarders are as follows: declare macro definitions to restore parameter names for parameter lists of different lengths, recursively expand the parameter list using the recursion depth in step S312, and use the concatenation function in step S311 to concatenate the results during the recursion process to generate expanded content in the second preset format.
[0022] Furthermore, the second preset format is: arg1, arg2, arg3, ... argM, where M represents a positive integer.
[0023] Furthermore, step S3.3 uses macro definition to declare the registrar, taking the return value type, function name, parameter type, and dynamic link library name as input parameters to expand the proxy function. The specific steps are as follows:
[0024] S331: Define a proxy function using the input return value type, function name, and a list of parameter types and names forwarded by the parameter type forwarder in step S3.1, and generate a proxy function in a preset format;
[0025] S332: In the proxy function defined in step S331, typedef is used to declare the function pointer type according to the return value type and function name input in step 3.3, and generate a function pointer type in a preset format;
[0026] S333: Load the dynamic link library using LoadLibrary based on the filename of the dynamic link library entered in step S3.3, and obtain the handle of the dynamic link library;
[0027] S334: Based on the function name entered in step S3.3 and the handle of the dynamic link library obtained in step S333, use GetProcAddress to obtain the address of the function in the dynamic link library;
[0028] S335: Convert the function address obtained in step S334 into the function pointer type declared in step S332, and obtain the function pointer object;
[0029] S336: Using the parameter value forwarded by the parameter forwarder in step S3.2, call the function pointer obtained in step S335, and return the return value returned by calling the function pointer.
[0030] Furthermore, the default format of the proxy function is: int FUNC(Type1arg1,Type2arg2,Type3arg3,...TypeN argN){}, where int represents the data type, FUNC represents the function name, Type represents the parameter type, and arg represents the function parameter.
[0031] Furthermore, the default function pointer type is: typedef int(*_FUNC)(Type1,Type2,Type3,...TypeN).
[0032] A second aspect of the present invention provides a dynamic link library reflection system implemented based on a C language preprocessor. The system includes a memory and a processor. The memory includes a dynamic link library reflection method program implemented based on a C language preprocessor. When the processor executes the dynamic link library reflection method program implemented based on a C language preprocessor, it performs the following steps:
[0033] S1: Obtain the filename of the dynamic link library to be proxied and the list of functions to be proxied. The list of functions includes: the function name, return type and parameter type of each function.
[0034] S2: Construct a registry using macros in the C language. The inputs of the registry are the filename of the dynamic link library, the name of the function to be proxied, the return type, and the parameter type.
[0035] S3: The compiler generates proxy functions using the registrar during the preprocessing phase of compilation.
[0036] S4: Using the registrar described in step 3, register the functions that need to be proxied in the source code;
[0037] S5: Call the function that needs to be proxied in S1 in the business, compile and run the software together with the registration code in step S4.
[0038] Compared with the prior art, the beneficial effects of the technical solution of the present invention are:
[0039] This invention solves the compatibility problem caused by using a C language preprocessor to register the interface of a dynamic link library, automatically generating a proxy implementation of the interface during compilation, and using the proxy implementation to replace the dynamic link library in the linking process. Attached Figure Description
[0040] Figure 1 This is a flowchart of a dynamic link library reflection method implemented using a C language preprocessor, according to an embodiment of the present invention.
[0041] Figure 2 The flowchart shows the process of compiling and generating proxy functions for the functions registered by the registrar in this embodiment of the invention.
[0042] Figure 3 This is a flowchart illustrating the use of macro definitions to declare parameter type forwarders in an embodiment of the present invention.
[0043] Figure 4 This is a flowchart illustrating how a macro definition is used to declare a registrar and how proxy functions are expanded, as described in an embodiment of the present invention. Detailed Implementation
[0044] To better understand the above-mentioned objectives, features, and advantages of the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. It should be noted that, unless otherwise specified, the embodiments and features described in these embodiments can be combined with each other.
[0045] Many specific details are set forth in the following description in order to provide a full understanding of the invention. However, the invention may also be practiced in other ways different from those described herein, and therefore the scope of protection of the invention is not limited to the specific embodiments disclosed below.
[0046] Example 1
[0047] like Figure 1 As shown, a dynamic link library reflection method implemented based on a C language preprocessor includes the following steps:
[0048] S1: Obtain the filename of the dynamic link library to be proxied and the list of functions to be proxied. The list of functions includes: the function name, return type and parameter type of each function.
[0049] It should be noted that the dynamic link library and its symbol library of this invention do not actually participate in the linking step of the software compilation process; instead, a proxy implemented by the C language preprocessor participates in the linking.
[0050] S2: Construct a registry using macros in the C language. The inputs of the registry are the filename of the dynamic link library, the name of the function to be proxied, the return type, and the parameter type.
[0051] It should be noted that this invention uses macro definitions in the C language to implement a registrar. This registrar implements an interface in a reflection dynamic link library by inputting the interface name, return value type, and parameter type. The registrar implemented using C language macro definitions can generate a function implementation of a proxy interface with the same name, return value, and parameter type as the interface name, return value, and parameter type, and supports the registration of parameters with different lengths and variable-length parameter types.
[0052] Example 2
[0053] Based on the above steps, this embodiment elaborates on the compilation of proxy functions using the registered functions registered by the registrar. It should be noted that the implementation of the registrar is the core component of this invention. Essentially, it is a function implemented using C language preprocessing, which can handle parameter types of different lengths and variable lengths. Functions registered through the registrar will automatically generate a proxy function during compilation.
[0054] S3: The compiler generates proxy functions using the registrar during the preprocessing phase of compilation.
[0055] It should be noted that the reflection mechanism implemented based on this registrar in this invention registers the interfaces that need to be accessed by proxy in the source code. The preprocessing in the C language compiler automatically generates proxy implementations to access the interfaces with the same name in the dynamic link library, and then participates in the compilation.
[0056] like Figure 2 As shown, the specific process of step S3 is as follows:
[0057] S3.1: Use macro definitions to declare parameter type forwarders to implement parameter type forwarding;
[0058] It should be noted that the parameter type forwarder is a key step in implementing the registrar. It is used to expand the list of parameter types in the registrar input, restore all parameter types, and generate parameter names for each parameter type in turn. It is a necessary condition for automatically generating proxy functions in step S3.
[0059] like Figure 3 As shown, the specific steps for declaring a parameter type forwarder using macro definitions are as follows:
[0060] S311: Implement a concatenation function that can concatenate parameters in multiple macro expansions, used to concatenate all the parameter types and parameter names after restoration;
[0061] S312: Implement a macro function that can obtain the length of the parameter list, used to determine the recursion depth for restoring the parameter list.
[0062] S313: Declare macro definitions for restoring parameter types and names for parameter lists of different lengths, recursively expand the parameter list using the recursion depth of step S312, and use the concatenation function of step S311 to concatenate the results during the recursion process to generate expanded content in a preset format.
[0063] The preset format mentioned in step S313 is Type1arg1, Type2arg2, Type3arg3,...TypeNargN, where N represents a positive integer.
[0064] S3.2: Use macro definitions to declare parameter forwarders to implement parameter forwarding;
[0065] It should be noted that the parameter forwarder is a key step in implementing the registrar. It is used to generate parameter names that are the same as those in the parameter list expanded in step S3.1, and is a necessary condition for automatically generating the proxy function in step S3.
[0066] The specific steps for using macro definitions to declare parameter forwarders are as follows: declare macro definitions to restore parameter names for parameter lists of different lengths, recursively expand the parameter list using the recursion depth in step S312, and use the concatenation function in step S311 to concatenate the results during the recursion process to generate expanded content in the second preset format.
[0067] The second preset format is: arg1, arg2, arg3, ... argM, where M represents a positive integer.
[0068] S3.3: Use macro definitions to declare the registrar, taking the return value type, function name, parameter type, and dynamic link library name as input parameters to expand the proxy function.
[0069] enter Figure 3 As shown, the specific steps are as follows:
[0070] S331: Define a proxy function using the input return value type, function name, and a list of parameter types and names forwarded by the parameter type forwarder in step S3.1, and generate a proxy function in a preset format;
[0071] The default format of the proxy function is: int FUNC(Type1arg1,Type2arg2,Type3arg3,...TypeNargN){}, where int represents the data type, FUNC represents the function name, Type represents the parameter type, and arg represents the function parameter.
[0072] S332: In the proxy function defined in step S331, typedef is used to declare the function pointer type according to the return value type and function name input in step 3.3, and generate a function pointer type in a preset format;
[0073] The default format for function pointers is: typedef int(*_FUNC)(Type1,Type2,Type3,...TypeN).
[0074] S333: Load the dynamic link library using LoadLibrary based on the filename of the dynamic link library entered in step S3.3, and obtain the handle of the dynamic link library;
[0075] S334: Based on the function name entered in step S3.3 and the handle of the dynamic link library obtained in step S333, use GetProcAddress to obtain the address of the function in the dynamic link library;
[0076] S335: Convert the function address obtained in step S334 into the function pointer type declared in step S332, and obtain the function pointer object;
[0077] S336: Using the parameter value forwarded by the parameter forwarder in step S3.2, call the function pointer obtained in step S335, and return the return value returned by calling the function pointer.
[0078] S4: Using the registrar described in step 3, register the functions that need to be proxied in the source code;
[0079] S5: Call the function that needs to be proxied in S1 in the business, compile and run the software together with the registration code in step S4.
[0080] It should be noted that dynamic link libraries are dynamically loaded at runtime, and the interfaces in the dynamic link libraries are accessed through proxy interfaces automatically generated by the C language preprocessor.
[0081] When accessing an interface, the interface name is obtained through a registration mechanism. The function address of the interface with the same name in the dynamic link library is dynamically obtained. Based on the interface name, return value, and parameter type, a function pointer object that is identical to the original function is constructed through the registration mechanism. The function address is then converted into a function pointer to complete the call.
[0082] Example 3
[0083] A second aspect of the present invention provides a dynamic link library reflection system implemented based on a C language preprocessor. The system includes a memory and a processor. The memory includes a dynamic link library reflection method program implemented based on a C language preprocessor. When the processor executes the dynamic link library reflection method program implemented based on a C language preprocessor, it performs the following steps:
[0084] S1: Obtain the filename of the dynamic link library to be proxied and the list of functions to be proxied. The list of functions includes: the function name, return type and parameter type of each function.
[0085] S2: Construct a registry using macros in the C language. The inputs of the registry are the filename of the dynamic link library, the name of the function to be proxied, the return type, and the parameter type.
[0086] S3: The compiler generates proxy functions using the registrar during the preprocessing phase of compilation.
[0087] S4: Using the registrar described in step 3, register the functions that need to be proxied in the source code;
[0088] S5: Call the function that needs to be proxied in S1 in the business, compile and run the software together with the registration code in step S4.
[0089] This invention uses C language preprocessing to dynamically reflect the interfaces in dynamic link libraries. By dynamically calling the interfaces of dynamic link libraries, it shields the dynamic link libraries and their symbol libraries from participating in the software linking process, thus solving the compatibility problems caused when products use different versions of library modules, or when different versions of the same product use the same library module.
[0090] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the present invention, and are not intended to limit the implementation of the present invention. Those skilled in the art can make other variations or modifications based on the above description. It is neither necessary nor possible to exhaustively describe all embodiments here. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the claims of the present invention.
Claims
1. A dynamic link library reflection method implemented using a C language preprocessor, characterized in that, Includes the following steps: S1: Obtain the filename of the dynamic link library to be proxied and the list of functions to be proxied. The list of functions includes: the function name, return type and parameter type of each function. S2: Construct a registry using macros in the C language. The inputs of the registry are the filename of the dynamic link library, the name of the function to be proxied, the return type, and the parameter type. S3: The compiler generates proxy functions using the registrar during the preprocessing phase of compilation; S4: Using the registrar described in step S3, register the functions that need to be proxied in the source code; S5: Call the function that needs to be proxied in S1 in the business, compile and run the software together with the registration code in step S4.
2. The dynamic link library reflection method based on a C language preprocessor as described in claim 1, characterized in that, The specific process of step S3 is as follows: S3.1: Use macro definitions to declare parameter type forwarders to implement parameter type forwarding; S3.2: Use macro definitions to declare parameter forwarders to implement parameter forwarding; S3.3: Use macro definitions to declare the registrar, taking the return value type, function name, parameter type, and dynamic link library name as input parameters to expand the proxy function.
3. The dynamic link library reflection method based on a C language preprocessor as described in claim 2, characterized in that, The specific steps for declaring a parameter type repeater using macro definitions are as follows: S311: Implement a concatenation function that can concatenate parameters in multiple macro expansions, used to concatenate all the parameter types and parameter names after restoration; S312: Implement a macro function that can obtain the length of the parameter list, used to determine the recursion depth of restoring the parameter list; S313: Declare macro definitions for restoring parameter types and names for parameter lists of different lengths, recursively expand the parameter list using the recursion depth of step S312, and use the concatenation function of step S311 to concatenate the results during the recursion process to generate expanded content in a preset format.
4. The dynamic link library reflection method based on a C language preprocessor as described in claim 3, characterized in that, The preset format mentioned in step S313 is Type1 arg1, Type2 arg2, Type3 arg3,...TypeNargN, where N represents a positive integer.
5. The dynamic link library reflection method based on a C language preprocessor as described in claim 3, characterized in that, The specific steps for using macro definitions to declare parameter forwarders are as follows: declare macro definitions to restore parameter names for parameter lists of different lengths, recursively expand the parameter list using the recursion depth in step S312, and use the concatenation function in step S311 to concatenate the results during the recursion process to generate expanded content in the second preset format.
6. The dynamic link library reflection method based on a C language preprocessor as described in claim 5, characterized in that, The second preset format is: arg1, arg2, arg3, ... argM, where M represents a positive integer.
7. The dynamic link library reflection method based on a C language preprocessor as described in claim 2, characterized in that, Step S3.3 uses macro definition to declare the registrar, taking the return value type, function name, parameter type, and dynamic link library name as input parameters to expand the proxy function. The specific steps are as follows: S331: Define a proxy function using the input return value type, function name, and a list of parameter types and names forwarded by the parameter type forwarder in step S3.1, and generate a proxy function in a preset format; S332: In the proxy function defined in step S331, typedef is used to declare the function pointer type according to the return value type and function name input in step 3.3, and generate a function pointer type in a preset format; S333: Load the dynamic link library using LoadLibrary based on the filename of the dynamic link library entered in step S3.3, and obtain the handle of the dynamic link library; S334: Based on the function name entered in step S3.3 and the handle of the dynamic link library obtained in step S333, use GetProcAddress to obtain the address of the function in the dynamic link library; S335: Convert the function address obtained in step S334 into the function pointer type declared in step S332, and obtain the function pointer object; S336: Using the parameter value forwarded by the parameter forwarder in step S3.2, call the function pointer obtained in step S335, and return the return value returned by calling the function pointer.
8. The dynamic link library reflection method based on a C language preprocessor as described in claim 7, characterized in that, The default format of the proxy function is: int FUNC(Type1 arg1, Type2 arg2, Type3 arg3,...){}, where int represents the data type, FUNC represents the function name, Type represents the parameter type, and arg represents the parameter.
9. The dynamic link library reflection method based on a C language preprocessor as described in claim 7, characterized in that, The default format for function pointers is: typedef int(*_FUNC)(Type1,Type2,Type3,...).
10. A dynamic link library reflection system implemented using a C language preprocessor, characterized in that, The system includes: a memory and a processor. The memory includes a dynamic link library reflection method program implemented based on a C language preprocessor. When the processor executes the dynamic link library reflection method program implemented based on the C language preprocessor, it performs the following steps: S1: Obtain the filename of the dynamic link library to be proxied and the list of functions to be proxied. The list of functions includes: the function name, return type and parameter type of each function. S2: Construct a registry using macros in the C language. The inputs of the registry are the filename of the dynamic link library, the name of the function to be proxied, the return type, and the parameter type. S3: The compiler generates proxy functions using the registrar during the preprocessing phase of compilation; S4: Using the registrar described in step 3, register the functions that need to be proxied in the source code; S5: Call the function that needs to be proxied in S1 in the business, compile and run the software together with the registration code in step S4.