Interface conversion method and device
By creating a custom encapsulation class and an intermediate layer conversion mechanism, C++ interfaces are converted into Java interfaces, solving the problem of high development complexity in existing technologies and achieving efficient interface conversion and concise code design.
Patent Information
- Application Number
- CN202510984995.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-17
- Publication Date
- 2025-11-21
AI Technical Summary
In existing technologies, converting C++ interfaces to Java interfaces is highly complex, especially for COM interfaces and callback functions, which requires a deep understanding of JNI structures, increasing development time and difficulty.
By creating a custom wrapper class, the target C++ interface is encapsulated as a member variable. The composition method is used to hide C++-specific features. A custom C++ callback interface is constructed through function parameter mapping rules and intermediate layer conversion. The interface wrapper software is used to generate a Java interface, avoiding the direct use of JNI.
It reduces programming complexity, improves development efficiency, simplifies code maintainability and scalability, and reduces development time and error rate.
Smart Images

Figure CN120994197A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of data processing, in particular to an interface conversion method and device. BACKGROUND
[0002] In the field of software development, the demand for wrapping C++ (C++ high-level programming language) interfaces into Java (Java high-level programming language) interfaces is increasing, which is mainly due to the cross-platform nature of Java language and the high efficiency of C++ language in system-level programming. However, since Java code runs in a virtual machine, while C++ code runs directly on the local, the interaction between the two needs to solve the problem of data structure conversion. Currently, the main solution on the market is to use the Java Native Interface (Java Native Interface) toolkits provided by Java to achieve this conversion.
[0003] JNI (Java Native Interface) as a programming framework allows Java code to interoperate with code developed in C, C++, or other native languages. However, the use of JNI requires a deep understanding of special structures such as Java Native Interface Environment (Native Method Invocation Environment Pointer) in order to read or write external data from the virtual machine. This puts a very high requirement on the level of developers, and also increases the time and difficulty of development. In addition, as the complexity of interface functions increases, such as callback interfaces, custom data structures, and platform-dependent data types, the programming complexity of JNI also increases significantly.
[0004] In particular, for Component Object Model (Component Object Model, a object-oriented programming model proposed by Microsoft, used to implement interoperability between software components) interfaces, since it is a special interface for Windows platform (Microsoft developed Windows operating system) and depends on the program's manifest file or registry, Java as a cross-platform language cannot directly provide support for COM (Component Object Model) interfaces. At the same time, the callback functions and complex data types in COM interfaces, such as HWND (Window Handle, used to uniquely identify a window), Variant (a general data type widely used in COM programming), etc., make the programming of JNI (Java Native Interface) extremely complex.
[0005] Therefore, there is an urgent need for an interface conversion method to reduce programming complexity, improve development efficiency, and solve the deficiencies in the prior art. SUMMARY
[0006] In view of the problems in the prior art, the application provides an interface conversion method and device, which can improve development efficiency and reduce programming complexity.
[0007] In order to solve at least one of the above problems, the application provides the following technical solutions:
[0008] In a first aspect, the application provides an interface conversion method, comprising:
[0009] A custom encapsulation class is created, and a target C++ interface is encapsulated as a member variable in the custom encapsulation class in a combined manner, wherein the custom encapsulation class does not inherit an external interface, and is used to hide C++ specific features of the target C++ interface, the function types of the target C++ interface include callback functions and non-callback functions, the callback functions are stored in a target C++ callback interface in the form of virtual functions, and the target C++ callback interface is an interface in the target C++ interface for implementing a callback mechanism;
[0010] A function parameter mapping rule is defined, intermediate function parameters of the non-callback functions are mapped according to the function parameter mapping rule, corresponding intermediate function parameters are determined, a corresponding non-callback function Java interface is determined according to the intermediate function parameters and preset interface wrapping software, and the non-callback function Java interface is used to enable a user to call non-callback function functions of the target C++ interface;
[0011] A custom C++ callback interface is constructed according to the callback functions, callback parameters generated by the target C++ callback interface are forwarded to the custom C++ callback interface through setting of an intermediate layer, interface conversion of the custom C++ callback interface is performed according to the interface wrapping software, a corresponding callback function Java interface is determined, and the callback function Java interface is used to enable a user to call callback function functions of the target C++ interface, wherein the custom C++ callback interface does not inherit the target C++ callback interface, and only includes callback function functions.
[0012] Further, the custom C++ callback interface is constructed according to the callback functions, and the method comprises the following steps.
[0013] A custom C++ interface without COM dependency is created;
[0014] The callback functions are converted according to the function parameter mapping rule, corresponding intermediate callback function parameters are determined, the intermediate callback function parameters are written into the custom C++ interface, and a corresponding custom C++ callback interface is determined.
[0015] Further, before forwarding the callback parameter generated by the target C++ callback interface to the custom C++ callback interface through the setting intermediate layer, comprising:
[0016] Creating an initial intermediate layer for the class requiring the callback interface, rewriting all virtual functions of the target C++ callback interface in the initial intermediate layer, and determining a corresponding inheritance callback interface intermediate layer;
[0017] Adding an interface pointer of the custom C++ callback interface in the inheritance callback interface intermediate layer, and determining a corresponding setting intermediate layer, wherein the interface pointer is used to synchronize the callback parameter of the target C++ callback interface to the custom C++ callback interface.
[0018] Further, the definition function parameter mapping rule comprises:
[0019] Directly mapping the basic data type in C++ to the corresponding basic type in Java;
[0020] Converting the complex data type in C++ to determine a corresponding intermediate type, wherein the intermediate type is a C++ type supported by the interface wrapping software.
[0021] Further, the converting the complex data type in C++ to determine a corresponding intermediate type comprises:
[0022] Converting the string type to std::wstring or std::string;
[0023] Converting the platform-specific data type to uintptr_t or long long, wherein the specific data type includes HWND and HANDLE.
[0024] Further, the converting the complex data type in C++ to determine a corresponding intermediate type further comprises:
[0025] For the VARIANT type, detecting the actual data type of the VARIANT, and calling a corresponding conversion method according to the actual data type;
[0026] Converting the GDI handle of HBITMAP to uintptr_t.
[0027] Further, the converting the complex data type in C++ to determine a corresponding intermediate type further comprises:
[0028] detecting an actual data type of the VARIANT, if the actual data type is an integer type, directly mapping to a corresponding type of Java;
[0029] if the actual data type is a string, converting to std::wstring;
[0030] if the actual data type is an array, converting to std::vector.
[0031] In a second aspect, the application provides an interface conversion device, comprising:
[0032] a hidden layer construction module, configured to create a custom encapsulation class, and encapsulate a target C++ interface as a member variable in the custom encapsulation class in a combined manner, wherein the custom encapsulation class does not inherit an external interface, and is configured to hide C++-specific features of the target C++ interface, the function types of the target C++ interface include callback functions and non-callback functions, the callback functions are stored in a target C++ callback interface in the form of virtual functions, and the target C++ callback interface is an interface in the target C++ interface for implementing a callback mechanism;
[0033] a non-callback function implementation module, configured to define a function parameter mapping rule, perform intermediate parameter type mapping on function parameters of the non-callback functions according to the function parameter mapping rule, determine corresponding intermediate function parameters, and determine corresponding non-callback function Java interfaces according to the intermediate function parameters and preset interface wrapping software, so that a user can call non-callback function features of the target C++ interface according to the non-callback function Java interfaces;
[0034] a callback function implementation module, configured to construct a custom C++ callback interface according to the callback functions, forward callback parameters generated by the target C++ callback interface to the custom C++ callback interface through a set intermediate layer, perform interface conversion on the custom C++ callback interface according to the interface wrapping software, and determine corresponding callback function Java interfaces, so that a user can call callback function features of the target C++ interface according to the callback function Java interfaces, wherein the custom C++ callback interface does not inherit the target C++ callback interface, and only includes callback function features.
[0035] In a third aspect, the application provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the interface conversion method when executing the program.
[0036] In a fourth aspect, the application provides a computer readable storage medium, which stores a computer program, and the computer program implements the steps of the interface conversion method when executed by a processor.
[0037] In a fifth aspect, the present application provides a computer program product comprising computer programs / instructions which, when executed by a processor, implement the steps of the interface conversion method.
[0038] From the above technical solution, the present application provides an interface conversion method and device. By creating a custom packaging class, the target C++ interface is encapsulated as a member variable in the custom packaging class in a combined manner. The non-callback function type of the target C++ interface is mapped according to a rule to obtain an intermediate function type that can be processed by interface packaging software. A non-callback function Java interface is obtained according to the interface packaging software and the intermediate function type, which is used to call the non-callback function of the target C++ interface. A custom C++ callback interface is constructed. The callback parameters generated by the target C++ callback interface are forwarded to the custom C++ callback interface through a middle layer. The custom C++ callback interface is converted according to the interface packaging software to determine the corresponding callback function Java interface, and the callback function of the target C++ interface is called. In this way, the development efficiency can be improved and the programming complexity can be reduced. BRIEF DESCRIPTION OF DRAWINGS
[0039] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed in the embodiments or prior art description will be briefly introduced below. Obviously, the drawings in the following description are some embodiments of the present application, and other drawings can also be obtained by those skilled in the art without creative labor.
[0040] Figure 1 One of the flowcharts of the interface conversion method in the embodiments of the present application;
[0041] Figure 2 The structural diagram of the interface conversion device in the embodiments of the present application;
[0042] Figure 3 The structural diagram of the electronic device in the embodiments of the present application.
[0043] Reference signs:
[0044] Electronic device 9600, central processing unit 9100, memory 9140, communication module 9110, input unit 9120, audio processor 9130, display 9160, power supply 9170, buffer memory 9141, application / function storage unit 9142, data storage unit 9143, driver program storage unit 9144, antenna 9111, speaker 9131, microphone 9132. DETAILED DESCRIPTION
[0045] In order to make the purposes, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are some but not all of the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work fall within the protection scope of the present application.
[0046] The acquisition, storage, use, processing and the like of data in the technical solutions of the present application comply with relevant provisions of national laws and regulations.
[0047] In view of the increasing demand for wrapping a C++ (C++ high-level programming language) interface into a Java (Java high-level programming language) interface, but the existing conversion scheme requires high requirements for developers and the conversion method is complex, the present application provides an interface conversion method and device, which creates a custom wrapper class, encapsulates a target C++ interface as a member variable in the custom wrapper class in a combined manner, performs rule mapping on a non-callback function type of the target C++ interface, obtains an intermediate function type that can be processed by interface wrapping software, obtains a non-callback function Java interface according to the interface wrapping software and the intermediate function type, and is used to call a non-callback function function of the target C++ interface; a custom C++ callback interface is constructed, a callback parameter generated by the target C++ callback interface is forwarded to the custom C++ callback interface through setting of an intermediate layer, interface conversion of the custom C++ callback interface is performed according to the interface wrapping software, a corresponding callback function Java interface is determined, and a callback function function of the target C++ interface is called, thereby improving development efficiency and reducing programming complexity.
[0048] In order to improve development efficiency and reduce programming complexity, an embodiment of an interface conversion method is provided in the present application, as shown in Figure 1 The interface conversion method specifically includes the following contents:
[0049] Step S101: a custom wrapper class is created, and a target C++ interface is encapsulated as a member variable in the custom wrapper class in a combined manner, wherein the custom wrapper class does not inherit an external interface and is used to hide C++ special features of the target C++ interface, function types of the target C++ interface include callback functions and non-callback functions, the callback functions are stored in a target C++ callback interface in the form of virtual functions, and the target C++ callback interface is an interface in the target C++ interface for implementing a callback mechanism;
[0050] Optionally, in the present embodiment, the purpose of the present embodiment is to hide C++ special features of a C++ interface by creating a custom wrapper class.
[0051] First, using the "combination" way, write a class that belongs to yourself, which can be completely controlled, and the old target C++ interface is included in the member variable of this class. In this way, the complexity of the original C++ interface can be shielded. It conforms to the scientific principle of code design, that is, "combination is better than inheritance".
[0052] The specific implementation process is as follows:
[0053] Suppose the original C++ interface is ICalculate:IUnknown, which has the function add(x,y). For a pure C++ environment, when using, you can construct its subclass or directly create itself. For our class, we will use it as a member variable of a new class class MyNewClass{CComPtr <icalculate>m_spCalc;} ; In this way, our class will hide the original inheritance complexity in its own member variables, and itself does not inherit any class, keeping simple.
[0054] It can be understood that the above steps encapsulate the original C++ interface (such as the COM interface) as a member variable in the new class, avoid direct inheritance, reduce interface complexity, make the new class completely controllable, and facilitate subsequent data type conversion and interface adaptation.
[0055] Step S102: defining a function parameter mapping rule, mapping the function parameters of the non-callback function to an intermediate parameter type according to the function parameter mapping rule, determining the corresponding intermediate function parameters, and determining the corresponding non-callback function Java interface according to the preset interface wrapping software and the intermediate function parameters, so that the user can call the non-callback function of the target C++ interface according to the non-callback function Java interface.
[0056] Optionally, in the embodiment, because we want to avoid directly using the JNI mode for interface conversion programming, we choose to use the SWIG interface code generation tool to achieve rapid generation without directly writing JNI. Although SWIG is powerful, it provides limited data type conversion, and cannot provide direct mapping of types such as HWND and BSTR. If mapping is required, part of the JNI code can be written in SWIG to perform type conversion by itself. This cannot achieve our goal.
[0057] Based on the above problems, in this step, the C++ special data types (such as BSTR, HWND, etc.) are converted to intermediate types that can be processed by SWIG, and then further mapped to corresponding types in Java through SWIG, to build a Java interface, so that the user can call the non-callback function of the C++ interface according to the Java interface. This ternary mapping (C++ special type → intermediate type → Java type) avoids directly writing JNI code.
[0058] Specifically, for the non-callback function of the C++ interface, first we need to process the function parameter type to get the intermediate type, and when processing the variable type, define a function parameter mapping rule.
[0059] Specifically, the function parameter mapping rule defines how to map the function parameter type in the target C++ interface to the corresponding type in Java. Specifically, for each non-callback function, according to its function parameter type, it is mapped to an intermediate parameter type according to the predefined mapping rule. These intermediate parameter types are types that can be automatically processed by the preset interface wrapping software (such as SWIG), thereby avoiding complex type conversion directly using JNI.
[0060] The function parameter mapping rule includes but is not limited to:
[0061] Specifically, the mapping of basic data types, the C++ basic data types (such as int, float, double) are directly mapped to the corresponding Java basic types (such as int, float, double), and the conversion is automatically completed by SWIG.
[0062] For example:
[0063] int(C++)→int(Java);
[0064] double(C++)→double(Java);
[0065] Specifically, the conversion of string types, for BSTR (Windows COM string) in C++, it is first converted to std::wstring or std::string, and then mapped to Java String; for char* or wchar_t*, it is first encapsulated as std::string or std::wstring, and then converted to Java String by SWIG.
[0066] Preferably, for multi-byte strings (char*) and wide character strings (wchar_t*), they are uniformly converted to UTF-8 encoded std::string in the encapsulation class to ensure cross-platform compatibility.
[0067] For example:
[0068] BSTR(COM string)→std::wstring(intermediate type)→String(Java);
[0069] BSTR is first converted to std::wstring (SWIG supports), and then Java String is automatically generated.
[0070] Specifically, the conversion of platform-specific data structures, for the data structures specific to the Windows platform (such as HWND, HANDLE), they are converted to uintptr_t or long long, and then mapped to Java long; for VARIANT (COM variant type), according to the actual stored data type (such as integer, string, array), it is disassembled and converted to an intermediate type (such as std::vector, std::string) that can be processed by SWIG, and finally mapped to the corresponding Java type (such as int[], String).
[0071] For handle types such as HWND, only pass as long in Java side, no direct operation, avoid illegal access; for VARIANT type, implement type detection and conversion in C++ wrapper class, ensure the data type received in Java side is correct.
[0072] For example:
[0073] Detect the actual data type of VARIANT (such as VT_I4, VT_BSTR, VT_ARRAY)
[0074] If VARIANT stores an integer (VT_I4), directly map it to Java int;
[0075] If it stores a string (VT_BSTR), first convert it to std::wstring, and then map it to Java String;
[0076] If it stores an array (VT_ARRAY), decompose it into std::vector, and then map it to the corresponding array type in Java.
[0077] Specifically, the conversion of custom structures, for C++ custom structures (MyStruct), use %include or %typemap in the SWIG interface file to define the conversion rules, so that the corresponding POJO (Plain Old Java Object) class is generated in the Java side; if the structure contains pointers or dynamic memory, implement memory management in the C++ wrapper class to ensure that the Java side does not need to manually release.
[0078] For example:
[0079] MyStruct (C++) → decomposed into int and std::string fields → generate Java MyStruct class (contains int and String fields);
[0080] Manually implement the conversion of MyStruct to basic types in custom classes.
[0081] Specifically, the processing of variable-length arrays and buffers, for dynamic arrays in C++ (such as int*, float*), first encapsulate them as std::vector <t>For non-callback functions, the function parameters are mapped to intermediate types, and the return value is mapped to a Java interface. For binary data buffers (such as BYTE*), they are wrapped as std::vector<uint8_t> and mapped to a Java byte[].
[0082] Specifically, based on the intermediate parameter type, a corresponding Java interface is generated using a preset interface wrapping software (SWIG). SWIG is an open-source software development tool that automatically generates interface wrapping code between low-level languages such as C and C++ and high-level languages such as Python, Java, and Ruby. Through SWIG, non-callback functions of the target C++ interface can be automatically generated as Java interfaces, thereby achieving interoperability between C++ and Java.
[0083] After the above mapping, all non-callback function types can be defined as intermediate types that can be automatically processed by SWIG. Based on the intermediate parameter type, a corresponding Java interface is generated using a preset interface wrapping software (SWIG). SWIG is an open-source software development tool that automatically generates interface wrapping code between low-level languages such as C and C++ and high-level languages such as Python, Java, and Ruby. Through SWIG, non-callback functions of the target C++ interface can be automatically generated as Java interfaces, thereby achieving interoperability between C++ and Java. By doing so, types that cannot be processed by SWIG are transferred to the functions of the custom class by writing JNI code in the SWIG-specific.i file, greatly reducing the complexity.
[0084] Since the generation of the Java interface is based on the intermediate parameter type and the preset interface wrapping software, users do not need to be concerned about the complexity of the underlying C++ interface and can directly call it through Java code. This not only simplifies the development process but also improves the maintainability and scalability of the code.
[0085] The above scheme can achieve the following technical effects: By defining function parameter mapping rules and mapping the function parameters of non-callback functions to intermediate parameter types, complex type conversions using JNI can be effectively avoided, significantly reducing development complexity and reducing development time and error rates. By generating a Java interface using a preset interface wrapping software (such as SWIG), users do not need to be concerned about the complexity of the underlying C++ interface and can directly call it through Java code. This not only simplifies the development process but also improves the maintainability and scalability of the code.
[0086] Step S103: constructing a custom C++ callback interface according to the callback function, forwarding the callback parameter generated by the target C++ callback interface to the custom C++ callback interface through setting an intermediate layer, performing interface conversion on the custom C++ callback interface according to the interface package software, determining a corresponding callback function Java interface, so that the user calls the callback function function of the target C++ interface according to the callback function Java interface, wherein the custom C++ callback interface does not inherit the target C++ callback interface and only includes the function of the callback function.
[0087] Optionally, in the embodiment, this step processes the callback function, and realizes interface calling (the callback function exists in the target C++ callback interface in the form of a virtual function in the target C++ interface).
[0088] Optionally, in the embodiment, because the interface type of the callback interface is used as the type of a function parameter. And the target C++ callback interface still adopts an inheritance implementation manner. When SWIG generates an interface, it cannot and does not generate binding code for the IUnknown interface of the Windows system.
[0089] Based on the above problems, the scheme adds a new intermediate layer to the class requiring the callback interface, hides the original callback interface with a complex inheritance relationship, and only provides a separate intermediate layer class to realize the function of the original target C++ callback interface.
[0090] First, one of the functions of the intermediate layer.
[0091] In the new constructed intermediate layer, all original callback functions are re-implemented in the custom class (here, some handwritten code generation tools are used), and the same named interface is transferred. In this way, SWIG can process the callback interface, and the Java end can directly inherit the interface to realize the virtual function that must be implemented.
[0092] It can be understood that, in the embodiment, based on the above steps, one of the functions of the intermediate layer can be realized: the intermediate layer directly realizes the C++ callback interface in the manner of the C++ user. The effect of the intermediate layer is to hide the original callback interface with a complex inheritance relationship.
[0093] Next, the second function of the intermediate layer.
[0094] Firstly, a custom C++ callback interface (a clean C++ interface, such as IMyCallback) is constructed. The custom interface does not inherit the target C++ callback interface, and only includes the functions of the callback function, so that we can modify the variable types of the original interface. That is, in the custom C++ callback interface newly constructed by us, the variables of the target C++ callback interface are modified according to the mapping rule of step S102, and it is noted that the custom interface does not directly have any inheritance relationship with the original interface, and the IUnknown dependency of the original interface is removed. The function type in the custom C++ callback interface after variable modification has been changed to an intermediate type that can be automatically processed by SWIG, so that SWIG can process the callback interface, automatically generate the custom C++ callback interface into a Java interface, and the Java end can directly inherit the interface to implement the virtual function that must be implemented.
[0095] Secondly, a pointer of the custom C++ callback interface (the pointer is initially empty and is set when the user calls the callback function) is set on the basis of the custom C++ callback interface. The pointer is placed in the above-mentioned intermediate layer, and the second function of the intermediate layer is achieved, that is, the callback parameters generated by the target C++ callback interface are forwarded to the custom C++ callback interface.
[0096] Specifically, the intermediate layer class inherits the original C++ callback interface and holds the pointer of the custom C++ callback interface, so that the intermediate layer can implement the functions of the target C++ callback interface, forward the parameters in the callback function, and call the functions of the custom callback interface through the pointer when the callback is performed.
[0097] Next, the interface wrapping software (SWIG) is used to perform interface conversion on the custom C++ callback interface to generate a corresponding Java interface, and the user can call the callback function of the target C++ interface through the generated Java interface.
[0098] Specifically, the above-mentioned pointer of the custom C++ callback interface.
[0099] Since the custom interface is wrapped by SWIG, a Java interface class can be generated, and the user implements the Java interface class, and the user can implement it by using his own Java class. At this time, we can get the pointer of the custom C++ interface in the CMyWrapper class (when the user calls FinishAdd), and set it to the intermediate layer. After that, the intermediate layer is responsible for receiving the callback every time the software has a callback. Then it forwards the parameters of the callback to the function with the same name, and through the custom interface pointer held by it, the Java end can also receive the callback. In this way, the callback is completed.
[0100] The technical effect realized by the above scheme is that: by constructing a custom C++ callback interface and forwarding callback parameters by using an intermediate layer, complex type conversion and callback processing by directly using JNI can be effectively avoided. This method can significantly reduce development complexity, reduce development time and error rate. By designing the custom C++ callback interface and the intermediate layer, the complexity of the target C++ callback interface can be effectively hidden, while the code simplicity and scalability are maintained. Users do not need to care about the implementation details of the underlying C++ interface, and can directly call through the Java interface, thereby improving the maintainability and scalability of the code.
[0101] The technical solution realizes cross-language interoperation between C++ and Java. In particular, for the processing of callback functions, a simple and effective solution is provided, so that the Java side can call the callback function function of the target C++ interface through the generated Java interface.
[0102] This example shows how the embodiments hide the special features of the C++ interface according to the custom wrapper class, and on this basis, the intermediate parameter processing of the callback function and non-callback function of the C++ interface is performed respectively, and the corresponding Java interface is constructed, so that users can call the function of the C++ interface according to the Java interface, improve the development efficiency, and reduce the programming complexity.
[0103] From the above description, it can be seen that the interface conversion method provided by the embodiments of the application can create a custom wrapper class, encapsulate the target C++ interface as a member variable in the custom wrapper class in a combined manner, perform rule mapping on the non-callback function type of the target C++ interface, obtain an intermediate function type that can be processed by interface wrapping software, obtain a non-callback function Java interface according to the interface wrapping software and the intermediate function type, and call the non-callback function function of the target C++ interface; a custom C++ callback interface is constructed, the callback parameters generated by the target C++ callback interface are forwarded to the custom C++ callback interface by setting an intermediate layer, the custom C++ callback interface is converted by the interface wrapping software, a corresponding callback function Java interface is determined, and the callback function function of the target C++ interface is called, thereby improving the development efficiency and reducing the programming complexity.
[0104] In an embodiment of the interface conversion method of the application, the following content can also be specifically included:
[0105] Step S201: creating a custom C++ interface without COM dependency;
[0106] Step S202: performing function parameter conversion on the callback function of the target C++ interface according to the function parameter mapping rule, determining a corresponding intermediate callback function parameter, writing the intermediate callback function parameter into the custom C++ interface, and determining a corresponding custom C++ callback interface.
[0107] Optionally, in the embodiment, a custom C++ callback interface (a clean C++ interface, such as IMyCallback) is constructed. The custom interface does not inherit the target C++ callback interface and only includes the function of the callback function, so that the variable type of the original interface can be modified. That is, in the custom C++ callback interface constructed by us, the variable of the target C++ callback interface is modified according to the mapping rule of step S102. Note that the custom interface does not have any inheritance relationship with the original interface directly, and the IUnknown dependency of the original interface is removed. The function type in the custom C++ callback interface after variable modification has become an intermediate type that can be automatically processed by SWIG. In this way, SWIG can process the callback interface, automatically generate the custom C++ callback interface into a Java interface, and the Java end can directly inherit the interface to implement the virtual function that must be implemented.
[0108] Through step S202, the custom C++ callback interface constructed by the embodiment lays a foundation for subsequent Java interface conversion through the interface.
[0109] In an embodiment of the interface conversion method of the present application, the following content can also be specifically included:
[0110] Step S301: creating an initial intermediate layer for a class requiring a callback interface, rewriting all virtual functions of the target C++ callback interface in the initial intermediate layer, and determining a corresponding inherited callback interface intermediate layer.
[0111] Step S302: adding an interface pointer of a custom C++ callback interface to the inherited callback interface intermediate layer, and determining a corresponding setting intermediate layer, wherein the interface pointer is used to synchronize the callback parameters of the target C++ callback interface to the custom C++ callback interface.
[0112] Optionally, in the embodiment, the interface type of the callback interface will be used as the type of the function parameter. The target C++ callback interface still adopts an inheritance implementation manner. When generating an interface, SWIG cannot and does not generate binding code for the IUnknown interface of the Windows system.
[0113] Based on the above problems, the scheme adds a new intermediate layer to the class requiring the callback interface, hides the original callback interface with complex inheritance relationship, and only provides a separate intermediate layer class to implement the function of the original C++ callback interface.
[0114] Specifically, in the newly constructed intermediate layer, all original callback functions are re-implemented in the custom class (some hand-written code generation tools are used here). Based on this step, one of the functions of the intermediate layer can be achieved: the intermediate layer simulates the way of C++ users to directly implement the C++ callback interface. The effect of the intermediate layer is to hide the original callback interface with complex inheritance relationship.
[0115] Specifically, based on the custom C++ callback interface constructed in step S202, a pointer of the custom C++ callback interface (which is initially empty and is set when the user calls the callback function) is set, and the pointer is placed in the above-mentioned intermediate layer. Based on this step, the second function of the intermediate layer is achieved: the callback parameter generated by the target C++ callback interface is forwarded to the custom C++ callback interface.
[0116] Through step S302, the intermediate layer is successfully constructed to hide the complexity of the original callback interface, connect the original callback interface and the custom callback interface, and lay a solid data foundation for the conversion and mutual call of the C++ interface and the Java interface.
[0117] In an embodiment of the interface conversion method of the present application, the following contents can also be specifically included:
[0118] Step S401: directly map the basic data type in C++ to the corresponding basic type in Java;
[0119] Step S402: perform data conversion on the complex data type in C++, and determine the corresponding intermediate type, wherein the intermediate type is a C++ type supported by the interface wrapping software.
[0120] Optionally, in the present embodiment, this step converts the C++ special data type (such as BSTR, HWND, etc.) into an intermediate type that can be processed by SWIG through type mapping, and further maps it to the corresponding type in Java through SWIG, to construct a Java interface, so that the user can call the non-callback function of the C++ interface according to the Java interface. This ternary mapping (C++ special type → intermediate type → Java type) avoids directly writing JNI code.
[0121] Through step S402, the present embodiment successfully avoids directly writing JNI code through ternary mapping, can achieve simple interface conversion, and improves the efficiency of interface conversion.
[0122] In an embodiment of the interface conversion method of the application, the following can also be specifically included:
[0123] Step S501: for string type, convert to std::wstring or std::string;
[0124] Step S502: for platform-specific data type, convert to uintptr_t or long long, wherein the specific data type includes HWND and HANDLE.
[0125] Specifically, the conversion of string type, for BSTR (Windows COM string) in C++, is first converted to std::wstring or std::string, and then mapped to Java String; for char* or wchar_t*, it is first encapsulated as std::string or std::wstring, and then converted to Java String by SWIG.
[0126] Preferably, for multi-byte string (char*) and wide character string (wchar_t*), they are uniformly converted to std::string of UTF-8 encoding in the encapsulation class to ensure cross-platform compatibility.
[0127] For example:
[0128] BSTR (COM string) -> std::wstring (intermediate type) -> String (Java);
[0129] BSTR is first converted to std::wstring (SWIG supports), and then Java String is automatically generated.
[0130] Specifically, the conversion of platform-specific data structure, for data structure specific to Windows platform (such as HWND, HANDLE), is converted to uintptr_t or long long, and then mapped to Java long;
[0131] Preferably, for handle type such as HWND, it is only passed as long on the Java side and is not directly operated to avoid illegal access.
[0132] Through the S502 step, the data conversion rule is successfully constructed, which lays a solid data foundation for subsequent interface conversion.
[0133] In an embodiment of the interface conversion method of the application, the following can also be specifically included:
[0134] Step S601: For the VARIANT type, detecting the actual data type of the VARIANT, and calling the corresponding conversion method according to the actual data type;
[0135] Step S602: For the GDI handle of HBITMAP, converting into uintptr_t.
[0136] Optionally, in the embodiment, for the VARIANT (COM variant type), according to the actual stored data type (such as an integer type, a string, an array), the VARIANT is disassembled and converted into a SWIG processable intermediate type (such as std::vector, std::string), and finally mapped into a corresponding type of Java (such as int[], String).
[0137] For the VARIANT type, type detection and conversion are implemented in the C++ encapsulation class, so as to ensure that the data type received by the Java end is correct.
[0138] For the GDI handle of HBITMAP, HDC and the like, the handle is converted into uintptr_t and mapped into long of Java. Direct operation of the handle is prohibited in the Java end, and all GDI related methods are executed through the C++ encapsulation layer agent.
[0139] Through the step S602, the embodiment successfully constructs the data conversion rule, and lays a solid data foundation for subsequent interface conversion.
[0140] In an embodiment of the interface conversion method of the application, the following content can also be specifically included:
[0141] Step S701: Detecting the actual data type of the VARIANT, and if the actual data type is an integer type, directly mapping into the corresponding type of Java;
[0142] Step S702: If the actual data type is a string, converting into std::wstring; if the actual data type is an array, converting into std::vector.
[0143] For the VARIANT type, type detection and conversion are implemented in the C++ encapsulation class, so as to ensure that the data type received by the Java end is correct.
[0144] For example:
[0145] Detecting the actual data type (such as VT_I4, VT_BSTR, VT_ARRAY) of the VARIANT
[0146] If the VARIANT stores an integer type (VT_I4), directly mapping into int of Java;
[0147] If the storage string (VT_BSTR) is converted into std::wstring first, and then mapped to Java String;
[0148] If the storage array (VT_ARRAY) is disassembled into std::vector first, and then mapped to the corresponding array type in Java.
[0149] Through the S702 step, the embodiment successfully constructs the data conversion rule, and lays a solid data foundation for subsequent interface conversion.
[0150] In order to improve the development efficiency and reduce the programming complexity, the application provides an embodiment of an interface conversion device for implementing all or part of the interface conversion method, which is shown in Figure 2 , which specifically includes the following contents:
[0151] The hidden layer construction module 10 is used to create a custom wrapper class, and the target C++ interface is encapsulated as a member variable in the custom wrapper class in a combined manner, wherein the custom wrapper class does not inherit an external interface, and is used to hide C++ specific features of the target C++ interface, the function types of the target C++ interface include callback functions and non-callback functions, the callback functions are stored in a target C++ callback interface in the form of virtual functions, and the target C++ callback interface is an interface in the target C++ interface for implementing a callback mechanism.
[0152] The non-callback function implementation module 20 is used to define a function parameter mapping rule, to perform intermediate parameter type mapping on function parameters of the non-callback function according to the function parameter mapping rule, to determine corresponding intermediate function parameters, to determine a corresponding non-callback function Java interface according to the intermediate function parameters and the preset interface wrapping software, and to enable a user to call non-callback function functions of the target C++ interface according to the non-callback function Java interface.
[0153] The callback function implementation module 30 is used to construct a custom C++ callback interface according to the callback function, to forward callback parameters generated by the target C++ callback interface to the custom C++ callback interface through a set intermediate layer, to perform interface conversion on the custom C++ callback interface according to the interface wrapping software, to determine a corresponding callback function Java interface, and to enable a user to call callback function functions of the target C++ interface according to the callback function Java interface, wherein the custom C++ callback interface does not inherit the target C++ callback interface, and only includes callback function functions.
[0154] From the above description, the interface conversion device provided by the embodiment of the application can encapsulate the target C++ interface as a member variable in a custom encapsulation class by creating the custom encapsulation class in a combined manner, perform rule mapping on a non-callback function type of the target C++ interface, obtain an intermediate function type that can be processed by interface wrapping software, obtain a non-callback function Java interface according to the interface wrapping software and the intermediate function type, and call a non-callback function function of the target C++ interface; a custom C++ callback interface is constructed, a callback parameter generated by the target C++ callback interface is forwarded to the custom C++ callback interface through setting of an intermediate layer, interface conversion of the custom C++ callback interface is performed according to the interface wrapping software, a corresponding callback function Java interface is determined, and a callback function function of the target C++ interface is called, so that the development efficiency can be improved and the programming complexity can be reduced.
[0155] In order to further illustrate the present scheme, the application further provides a specific application example of an interface conversion method using the above interface conversion device, and specifically includes the following contents.
[0156] The application designs the following form
[0157] 1. First, a "combination" is used to write a class that belongs to oneself and can be completely controlled, and the old interface is included in the member variable of the class. In this way, the complexity of the original C++ interface can be shielded. It is scientific to meet the code design, that is, combination is better than inheritance.
[0158] Specifically, it is as follows:
[0159] For example, the original interface is ICalculate:IUnknown, which has a function add(x, y). For a pure C++ environment, when used, a subclass thereof can be constructed or itself can be directly created. For our class, we regard it as a member variable class MyNewClass{CComPtr <icalculate>m_spCalc;}; In this way, our class hides the complexity of the original inheritance in its own member variables, and does not inherit from any class, thus keeping it simple.
[0160] 2. For non-callback functions, we can directly proceed to point three: handling variable types. When handling variable types, we define variable type mappings. For example, a special type of string like BSTR is mapped to Java's String, while Variant is more complex, requiring mapping to a variable-length array or a variable-length string depending on its type. An HWND handle is actually a pointer, which can be mapped to Java's long on x64 platforms.
[0161] 3. Because we want to avoid directly using JNI in our programming, I chose to use SWIG, an interface code generation tool, for rapid generation without needing to write JNI code directly. While SWIG is powerful, its built-in data type conversions are limited; it cannot provide direct mappings for types like HWND and BSTR. If mapping is needed, SWIG can handle the type conversion manually by writing some JNI code, which doesn't meet our requirements.
[0162] 4. Therefore, combining points 1, 2, and 3, we can define a ternary mapping. For example, BSTR (C++) – std::wstring (C++) – String (Java), where the first element is the parameter type of the original C++ interface function, and the second element is the parameter type of the function of the custom class we implemented in step 2. Since the new class is completely under our control, we can define all its types as types that SWIG can automatically handle. In this way, SWIG can directly generate Java types based on the second element. This shifts the handling of types that SWIG cannot process from writing JNI code in SWIG's dedicated .i files to functions in the custom class, greatly reducing complexity.
[0163] 5. Based on the above four points, everything except callback interfaces can be implemented. This is because the interface type of the callback interface will be used as the type of the function parameter. The original callback interface still uses inheritance. However, SWIG cannot generate binding code for the Windows system's built-in `IUnknown` interface when generating interfaces. By using the custom class designed in step 1, we can hide the complex inheritance relationship of the original callback interface and only provide a single class to the outside world. All the original functions are reimplemented in the custom class (using some hand-written code generation tools) and then called from the interface with the same name. In this way, SWIG can handle callback interfaces, and the Java side can directly inherit from this interface to implement the required virtual functions.
[0164] 6、For callback interface. Since it is also a COM interface, but originally designed by user to inherit and implement. Now we define a clean C++ interface, such as IMyCallback, so that we can modify the original interface variable type. Note that this custom interface does not have any direct inheritance relationship with the original interface. Then, we add an intermediate layer to the class that needs to callback interface. This intermediate layer can do two things, one is to simulate the way of C++ user, directly implement the C++ callback interface. The other thing is to wait to be set a custom C++ interface pointer. Since the custom interface is wrapped by SWIG, it can generate a Java interface class, and the user implements this Java interface class, and the user can implement it with his own Java class. At this time, we can get the custom C++ interface pointer in the CMyWrapper class (when the user calls FinishAdd), and set it to the intermediate layer. After that, the intermediate layer is responsible for receiving the callback every time the software has a callback. Since it implements the original callback, it will receive the callback. Then it forwards the parameters of the callback to the same function through the custom interface pointer it holds, so that Java can also receive the callback. In this way, the callback is completed.
[0165] From the hardware level, in order to improve the development efficiency and reduce the programming complexity, an embodiment of an electronic device for implementing all or part of the interface conversion method is provided, and the electronic device specifically includes the following content:
[0166] A processor, a memory, a communications interface, and a bus; wherein the processor, the memory, the communications interface, and the bus complete the communication among each other; the communications interface is used to realize the information transmission between the interface conversion method and the core business system, the user terminal, and the related database and other related devices; the logic controller can be a desktop computer, a tablet computer, a mobile terminal, and the like, and the embodiment is not limited thereto. In the embodiment, the logic controller can be implemented by referring to the embodiments of the interface conversion method and the embodiments of the interface conversion method, the contents of which are incorporated herein, and the repeated parts will not be described.
[0167] It can be understood that the user terminal can include a smart phone, a tablet electronic device, a network set-top box, a portable computer, a desktop computer, a personal digital assistant (PDA), a vehicle-mounted device, a smart wearable device, and the like. The smart wearable device can include smart glasses, a smart watch, a smart bracelet, and the like.
[0168] In actual application, part of the interface conversion method can be executed on the electronic device as described above, or all operations can be completed in the client device. Specifically, the selection can be made according to the processing capability of the client device and the restriction of the user's use scenario, etc. The present application does not limit this. If all operations are completed in the client device, the client device can further include a processor.
[0169] The client device described above can have a communication module (i.e., a communication unit) and can be communicatively connected with a remote server to realize data transmission with the server. The server can include a server on the side of a task scheduling center, and in other implementation scenarios, can include a server of an intermediate platform, such as a server of a third-party server platform communicatively linked with the server of the task scheduling center. The server can include a single computer device, or a server cluster composed of multiple servers, or a server structure of a distributed device.
[0170] Figure 3 A schematic block diagram of a system configuration of the electronic device 9600 of an embodiment of the present application is shown in FIG. 9. As shown in the figure, the electronic device 9600 can include a central processor 9100 and a memory 9140; the memory 9140 is coupled to the central processor 9100. It is to be noted that the structure shown in the figure is exemplary; other types of structures can also be used to supplement or replace the structure to realize telecommunication functions or other functions. Figure 3 Figure 3 The structure shown in the figure is exemplary; other types of structures can also be used to supplement or replace the structure to realize telecommunication functions or other functions.
[0171] In an embodiment, the interface conversion method function can be integrated into the central processor 9100. The central processor 9100 can be configured to perform the following control:
[0172] Step S101: A custom encapsulation class is created, and a target C++ interface is encapsulated as a member variable in the custom encapsulation class in a combined manner, wherein the custom encapsulation class does not inherit an external interface, and is used to hide C++-specific features of the target C++ interface; the function types of the target C++ interface include callback functions and non-callback functions; the callback functions are stored in a target C++ callback interface in the form of virtual functions; and the target C++ callback interface is an interface in the target C++ interface used to implement a callback mechanism.
[0173] Step S102: A function parameter mapping rule is defined, an intermediate parameter type is mapped for a function parameter of the non-callback function according to the function parameter mapping rule, a corresponding intermediate function parameter is determined, a corresponding non-callback function Java interface is determined according to a preset interface wrapping software and the intermediate function parameter, so that a user can call a non-callback function function of the target C++ interface according to the non-callback function Java interface.
[0174] Step S103: constructing a custom C++ callback interface according to the callback function, forwarding the callback parameters generated by the target C++ callback interface to the custom C++ callback interface through setting an intermediate layer, performing interface conversion on the custom C++ callback interface according to the interface wrapping software, determining a corresponding callback function Java interface, so that the user calls the callback function function of the target C++ interface according to the callback function Java interface, wherein the custom C++ callback interface does not inherit the target C++ callback interface and only includes the function of the callback function.
[0175] From the above description, the electronic device provided by the embodiments of the present application creates a custom packaging class, encapsulates the target C++ interface as a member variable in the custom packaging class in a combined manner, performs rule mapping on the non-callback function type of the target C++ interface, obtains an intermediate function type that can be processed by the interface wrapping software, obtains a non-callback function Java interface according to the interface wrapping software and the intermediate function type, and is used to call the non-callback function function of the target C++ interface; a custom C++ callback interface is constructed, the callback parameters generated by the target C++ callback interface are forwarded to the custom C++ callback interface through setting an intermediate layer, the custom C++ callback interface is subjected to interface conversion according to the interface wrapping software, a corresponding callback function Java interface is determined, and the callback function function of the target C++ interface is called, thereby improving the development efficiency and reducing the programming complexity.
[0176] In another embodiment, the interface conversion method can be configured separately from the central processor 9100, for example, the interface conversion method can be configured as a chip connected with the central processor 9100, and the interface conversion method function is realized through the control of the central processor.
[0177] As shown in Figure 3 , the electronic device 9600 can also include a communication module 9110, an input unit 9120, an audio processor 9130, a display 9160, and a power supply 9170. It should be noted that the electronic device 9600 does not necessarily include all the components shown in Figure 3 ; in addition, the electronic device 9600 can also include components not shown in Figure 3 , which can refer to prior art.
[0178] As shown in Figure 3 , the central processor 9100, also known as a controller or operation control, can include a microprocessor or other processor device and / or logic device, which receives input and controls the operation of each component of the electronic device 9600.
[0179] The memory 9140, for example, can be one or more of a buffer, a flash memory, a hard drive, a removable media, a volatile memory, a non-volatile memory, or other suitable device. The above-mentioned information related to failure can be stored, and in addition, a program for executing the information related to failure can be stored. The central processing unit 9100 can execute the program stored in the memory 9140 to achieve information storage or processing, and the like.
[0180] The input unit 9120 provides input to the central processing unit 9100. The input unit 9120 is, for example, a key or a touch input device. The power supply 9170 is used to supply power to the electronic device 9600. The display 9160 is used to display display objects such as images and characters. The display can be, for example, an LCD display, but is not limited thereto.
[0181] The memory 9140 can be a solid state memory such as a read only memory (ROM), a random access memory (RAM), a SIM card, and the like. It can also be a memory that retains information even when power is off, can be selectively erased, and is provided with more data, and examples of such a memory are sometimes referred to as an EPROM, and the like. The memory 9140 can also be some other type of device. The memory 9140 includes a buffer memory 9141 (sometimes referred to as a buffer). The memory 9140 can include an application / function storage section 9142 for storing application programs and function programs or for storing a flow for executing the operation of the electronic device 9600 by the central processing unit 9100.
[0182] The memory 9140 can also include a data storage section 9143 for storing data such as contacts, digital data, pictures, sounds, and / or any other data used by the electronic device. A driver storage section 9144 of the memory 9140 can include various drivers of the electronic device for a communication function and / or for executing other functions of the electronic device such as a messaging application, a phonebook application, and the like.
[0183] The communication module 9110 is a transmitter / receiver that transmits and receives signals via an antenna 9111. The communication module 9110 is coupled to the central processing unit 9100 to provide input signals and receive output signals, which can be the same as in the case of a conventional mobile communication terminal.
[0184] Based on different communication technologies, multiple communication modules 9110, such as a cellular network module, a Bluetooth module, and / or a wireless local area network module, etc., can be provided in the same electronic device. The communication modules 9110 are also coupled to a speaker 9131 and a microphone 9132 via an audio processor 9130 to provide audio output via the speaker 9131 and to receive audio input from the microphone 9132, thereby enabling typical telecommunication functions. The audio processor 9130 can include any suitable buffers, decoders, amplifiers, etc. In addition, the audio processor 9130 is also coupled to the central processor 9100, thereby enabling recording on the local device via the microphone 9132 and enabling playing of stored sounds on the local device via the speaker 9131.
[0185] The embodiments of the present application further provide a computer readable storage medium capable of implementing all steps of the interface conversion method in which the execution subject is the server or the client in the above-mentioned embodiments. The computer readable storage medium stores a computer program, and the computer program, when executed by a processor, implements all steps of the interface conversion method in which the execution subject is the server or the client in the above-mentioned embodiments. For example, the processor executes the computer program to implement the following steps:
[0186] Step S101: a custom encapsulation class is created, and a target C++ interface is encapsulated as a member variable in the custom encapsulation class in a combined manner, wherein the custom encapsulation class does not inherit an external interface, and is used to hide C++ specific features of the target C++ interface. Function types of the target C++ interface include callback functions and non-callback functions. The callback functions are stored in a target C++ callback interface in the form of virtual functions. The target C++ callback interface is an interface in the target C++ interface used to implement a callback mechanism.
[0187] Step S102: a function parameter mapping rule is defined, intermediate parameter types of function parameters of the non-callback functions are mapped according to the function parameter mapping rule, corresponding intermediate function parameters are determined, a corresponding non-callback function Java interface is determined according to a preset interface wrapping software and the intermediate function parameters, so that a user can call non-callback function functions of the target C++ interface according to the non-callback function Java interface.
[0188] Step S103: constructing a custom C++ callback interface according to the callback function, forwarding the callback parameters generated by the target C++ callback interface to the custom C++ callback interface through setting an intermediate layer, performing interface conversion on the custom C++ callback interface according to the interface wrapping software, determining a corresponding callback function Java interface, so that the user calls the callback function function of the target C++ interface according to the callback function Java interface, wherein the custom C++ callback interface does not inherit the target C++ callback interface and only includes the function of the callback function.
[0189] As can be seen from the above description, the computer readable storage medium provided by the embodiments of the present application creates a custom packaging class, encapsulates the target C++ interface as a member variable in the custom packaging class in a combined manner, performs rule mapping on the non-callback function type of the target C++ interface to obtain an intermediate function type that can be processed by the interface wrapping software, obtains a non-callback function Java interface according to the interface wrapping software and the intermediate function type, and calls the non-callback function function of the target C++ interface; a custom C++ callback interface is constructed, the callback parameters generated by the target C++ callback interface are forwarded to the custom C++ callback interface through setting an intermediate layer, interface conversion is performed on the custom C++ callback interface according to the interface wrapping software, a corresponding callback function Java interface is determined, and the callback function function of the target C++ interface is called, thereby improving the development efficiency and reducing the programming complexity.
[0190] The embodiments of the present application also provide a computer program product capable of implementing all steps of the interface conversion method in which the execution subject in the above embodiments is a server or a client. The computer program / instruction is executed by a processor to implement the steps of the interface conversion method, for example, the computer program / instruction implements the following steps:
[0191] Step S101: creating a custom packaging class, encapsulating a target C++ interface as a member variable in the custom packaging class in a combined manner, wherein the custom packaging class does not inherit an external interface and is used to hide C++ special features of the target C++ interface, the function type of the target C++ interface includes a callback function and a non-callback function, the callback function is stored in a target C++ callback interface in the form of a virtual function, and the target C++ callback interface is an interface in the target C++ interface for implementing a callback mechanism;
[0192] Step S102: defining a function parameter mapping rule, mapping an intermediate parameter type of a function parameter of the non-callback function according to the function parameter mapping rule, determining a corresponding intermediate function parameter, and determining a corresponding non-callback function Java interface according to the intermediate function parameter and the preset interface wrapping software, so that a user can call a non-callback function function of the target C++ interface according to the non-callback function Java interface.
[0193] Step S103: constructing a custom C++ callback interface according to the callback function, forwarding a callback parameter generated by the target C++ callback interface to the custom C++ callback interface through a set intermediate layer, performing interface conversion on the custom C++ callback interface according to the interface wrapping software, determining a corresponding callback function Java interface, so that a user can call a callback function function of the target C++ interface according to the callback function Java interface, wherein the custom C++ callback interface does not inherit the target C++ callback interface and only includes a callback function function.
[0194] From the above description, it can be known that the computer program product provided by the embodiment of the present application creates a custom encapsulation class, encapsulates the target C++ interface as a member variable in the custom encapsulation class in a combined manner, performs rule mapping on a non-callback function type of the target C++ interface, obtains an intermediate function type that can be processed by the interface wrapping software, obtains a non-callback function Java interface according to the interface wrapping software and the intermediate function type, and calls a non-callback function function of the target C++ interface; a custom C++ callback interface is constructed, a callback parameter generated by the target C++ callback interface is forwarded to the custom C++ callback interface through a set intermediate layer, interface conversion is performed on the custom C++ callback interface according to the interface wrapping software, a corresponding callback function Java interface is determined, and a callback function function of the target C++ interface is called, thereby improving the development efficiency and reducing the programming complexity.
[0195] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a device, or a computer program product. Therefore, the present application can adopt a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can adopt a computer program product in the form of being implemented on one or more computer usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer usable program codes.
[0196] The computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart Figure 1 one or more functions specified in the flowchart or multiple flows and / or blocks. Figure 1 one or more functions specified in the flowchart or multiple flows and / or blocks.
[0197] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including an instruction device that implements the function specified in the flowchart Figure 1 one or more functions specified in the flowchart or multiple flows and / or blocks. Figure 1 one or more functions specified in the flowchart or multiple flows and / or blocks.
[0198] These computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart Figure 1 one or more functions specified in the flowchart or multiple flows and / or blocks. Figure 1 one or more functions specified in the flowchart or multiple flows and / or blocks.
[0199] The principles and implementation manners of the present application are described in the specific embodiments, and the above embodiment descriptions are only used to help understand the method of the present application and its core idea; meanwhile, for those skilled in the art, according to the idea of the present application, the specific implementation manners and application ranges can be changed, and the above description should not be understood as the limitation of the present application.< / icalculate> < / t> < / icalculate>
Claims
1. An interface conversion method, characterized in that, The method includes: A custom wrapper class is created, and the target C++ interface is encapsulated as a member variable in the custom wrapper class using a composition method. The custom wrapper class does not inherit the external interface and is used to hide the C++ specific characteristics of the target C++ interface. The function types of the target C++ interface include callback functions and non-callback functions. The callback functions are stored in the target C++ callback interface in the form of virtual functions. The target C++ callback interface is the interface within the target C++ interface used to implement the callback mechanism. Define function parameter mapping rules, perform intermediate parameter type mapping on the function parameters of the non-callback function of the target C++ interface according to the function parameter mapping rules, determine the corresponding intermediate function parameters, and determine the corresponding non-callback function Java interface according to the preset interface packaging software and the intermediate function parameters, so that the user can call the non-callback function function of the target C++ interface according to the non-callback function Java interface; A custom C++ callback interface is constructed based on the callback function of the target C++ interface. An intermediate layer is set to forward the callback parameters generated by the target C++ callback interface to the custom C++ callback interface. The custom C++ callback interface is then converted by the interface packaging software to determine the corresponding callback function Java interface, so that the user can call the callback function of the target C++ interface according to the callback function Java interface. The custom C++ callback interface does not inherit the target C++ callback interface and only includes the functionality of the callback function.
2. The interface conversion method according to claim 1, characterized in that, The step of constructing a custom C++ callback interface based on the callback function of the target C++ interface includes: Create a custom C++ interface that has no COM dependencies; According to the function parameter mapping rules, the callback function of the target C++ interface is converted to determine the corresponding intermediate callback function parameters. The intermediate callback function parameters are then written into the custom C++ interface to determine the corresponding custom C++ callback interface.
3. The interface conversion method according to claim 1, characterized in that, Before forwarding the callback parameters generated by the target C++ callback interface to the custom C++ callback interface through the setting of an intermediate layer, the following steps are included: Create an initial intermediate layer for the class of the required callback interface, override all virtual functions of the target C++ callback interface in the initial intermediate layer, and determine the corresponding intermediate layer that inherits the callback interface. In the intermediate layer of the inherited callback interface, an interface pointer of a custom C++ callback interface is added to determine the corresponding intermediate layer. The interface pointer is used to synchronize the callback parameters of the target C++ callback interface to the custom C++ callback interface.
4. The interface conversion method according to claim 1, characterized in that, The defined function parameter mapping rules include: Basic data types in C++ are directly mapped to their corresponding basic types in Java; Data conversion is performed on complex data types in C++ to determine the corresponding intermediate type, wherein the intermediate type is a C++ type supported by the interface wrapper software.
5. The interface conversion method according to claim 1, characterized in that, The process of converting complex data types in C++ to determine the corresponding intermediate type includes: For string types, convert to std::wstring or std::string; For platform-specific data types, convert to uintptr_t or long long, where the specific data types include HWND and HANDLE.
6. The interface conversion method according to claim 1, characterized in that, The method of performing data conversion on complex data types in C++ to determine the corresponding intermediate type also includes: For VARIANT type, detect the actual data type of VARIANT and call the corresponding conversion method according to the actual data type; For the GDI handle of HBITMAP, convert it to uintptr_t.
7. The interface conversion method according to claim 1, characterized in that, For the VARIANT type, the actual data type of the VARIANT is detected, and the corresponding conversion method is called according to the actual data type, including: The actual data type of the VARIANT is detected. If the actual data type is an integer, it is directly mapped to the corresponding Java type. If the actual data type is a string, it is converted to std::wstring; if the actual data type is an array, it is converted to std::vector.
8. An interface conversion device, characterized in that, The device includes: The hidden layer construction module is used to create custom wrapper classes. The target C++ interface is encapsulated as member variables in the custom wrapper class using a composition method. The custom wrapper class does not inherit the external interface and is used to hide the C++ specific characteristics of the target C++ interface. The function types of the target C++ interface include callback functions and non-callback functions. The callback functions are stored in the target C++ callback interface in the form of virtual functions. The target C++ callback interface is the interface within the target C++ interface used to implement the callback mechanism. The non-callback function implementation module is used to define function parameter mapping rules, perform intermediate parameter type mapping on the function parameters of the non-callback function according to the function parameter mapping rules, determine the corresponding intermediate function parameters, and determine the corresponding non-callback function Java interface according to the preset interface packaging software and the intermediate function parameters, so that the user can call the non-callback function function of the target C++ interface according to the non-callback function Java interface; The callback function implementation module is used to construct a custom C++ callback interface based on the callback function, forward the callback parameters generated by the target C++ callback interface to the custom C++ callback interface through a middleware layer, and perform interface conversion on the custom C++ callback interface according to the interface packaging software to determine the corresponding callback function Java interface, so that the user can call the callback function function of the target C++ interface according to the callback function Java interface. The custom C++ callback interface does not inherit the target C++ callback interface and only includes the functionality of the callback function.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the steps of the interface conversion method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the interface conversion method according to any one of claims 1 to 7.