Background technology
Application program: executable file (Executable program) is meant in order to finish a certain or a few particular tasks and be developed the computer program that runs on the operating system.Certain form of its match operation system appointment, and be stored with binary form, operating system is responsible for loading and the operation to program.
Early stage program design does not have concept of component, and all functions all concentrate in the executable file.Now, application program is that standard is divided into a plurality of relatively independent assemblies and writes usually with the function.Each assembly is relatively independent, and inter-module is realized mutual calling by interface.When a functional requirement changes,, the code of other assembly is remained unchanged keeping revising the functional module that changes under the constant prerequisite of interface.After each assembly was write and finished, application program can be linked to assembly, the interface of invocation component dynamically when needing the function of assembly.Thereby reduce the size and the start-up time of application program; Simultaneously, this assembly also can be used by a plurality of application programs simultaneously.
In order to reduce the complicacy of software development, improve the reusability of assembly, existing operating system all provides the structure standard of assembly, and we claim that the assembly based on this standard is dynamic link library (DLL, Dynamic Link Library) under Windows; Under Linux, be called shared object file (So:Shared ObjectFile) etc.
With the dynamic link library under the windows operating system is example: after assembly C writes and finishes, can be compiled into a DLL.When operation, have only when the EXE program will be called these DLL assemblies, system just can be loaded into them in the memory headroom.This mode has not only reduced the size of EXE file and to the demand of memory headroom, and these DLL assemblies can be used by a plurality of application programs simultaneously.Simultaneously, in order to form a kind of unified assembly exploitation at windows platform and to call standard, realize the communication of inter-module and calling of inter-module, Microsoft proposes The Component Object Model (COM, Component Object Model) method, but since its call-by mechanism make between assembly and assembly, client and the assembly call realization before need to write a large amount of codes, realize complexity; And, because com component must register in registration table with securing component ID, when calling by searching the registration table charging assembly, yet, have only windows platform to have registration table, thereby the assembly of developing based on COM can not carry out the transplanting of cross operating system.
Summary of the invention
The purpose of this invention is to provide the method that a kind of inter-module calls, this method can not rely on registration table, and realizes simple.
For solving the problems of the technologies described above, the objective of the invention is to be achieved through the following technical solutions: 1) set up the sign of determining and be used to create the function of assembly, and set up component interface for each assembly; 2) program is loaded into the shared drive that presets, and triggering is saved in shared drive with component identification and described function; 3) client component request call target element, the system searching target element identifies to call described function creation assembly, returns the target element interface, and client component is realized funcall according to described interface.
On the said method basis, 1) also comprises in and a plurality of described assemblies are compiled the constituent components collection.
In the said method, 3) behind the described function of system call, return the interface pointer of assembly in, client component obtains the interface of target element according to the interface pointer in the shared drive.2) after program was loaded into internal memory in, the component address that presets in the configuration file of system according to master routine read the assembly of determining under the catalogue, and component interface is added specific shared drive; System preserves the operation of component identification and described function according to the code that presets in the assembly structure function.
Above technical scheme as can be seen, information with assembly when program is loaded into internal memory among the present invention is saved in the internal memory, make realization of the present invention not need to rely on the special setting of operating system, do not rely on distinctive registration table in the Windows system when for example adopting method of the present invention to realize calling of inter-module, therefore calling of inter-module of the present invention do not rely on specific operating system, makes that method of the present invention is can stride operating platform to realize.And, find when in the practical programs exploitation, adopting method of the present invention that realize that with existing dependence particular system the method for component call is compared, calling of inter-module of the present invention is simple relatively, do not need a large amount of coding works, thereby improved the development efficiency of software.
The shared drive that the present invention has utilized system to provide by administering and maintaining specific module information structure in shared drive, thereby makes each assembly visit mutually in described shared drive, makes calling of inter-module convenient simple.
Further, can be compiled into an assembly to a plurality of assemblies among the present invention, facts have proved, among the present invention a plurality of assemblies are compiled into an assembly, in the process of reading assembly, reduce input-output operation, improve assembly and loaded and carry out efficient disk.And, find in the practice, owing to each assembly all can be registered in the internal memory automatically, thereby, under this method, a plurality of assemblies are compiled as an assembly, the dirigibility of debugging and issue is provided to software development.
Embodiment
The present invention relates to the method that a kind of inter-module calls.Its core is: after executable program is loaded into internal memory, automatically all component information under the assigned catalogue (function pointer of assembly ID and this assembly of establishment) is loaded in the shared drive of executable program establishment, when calling some assemblies, the information of preserving according to assembly in the internal memory, create component object, thereby realize that assembly function calls.
A below explanation preferred embodiment of the present invention, the principle of this embodiment is: when a process begins, carry out body DLL or EXE and be loaded into internal memory, the principle of utilizing the constructed fuction of the global object of C++ programming language to carry out prior to C Runtime Library (C-Runtime Library), message block in the constructed fuction of certain global object in the handle component (assembly ID, the establishment body of assembly) is stored on the internal memory of process in the operating system, makes any assembly of process and CLIENT PROGRAM can both get access to these message block and create component object according to the establishment body in the message block.When calling an assembly, search the function of in internal memory, preserving that is used to create assembly by assembly ID, again by this function calls is created assembly.After assembly was created, interface that can invocation component was realized specific assembly function.
With reference to Fig. 1, the implementation method of preferred embodiment of the present invention is as follows.
Step 11:, and set up component interface for each assembly is given definite sign ID and is used to create the function of this assembly; Set up shared drive.
Described assembly also is called module, and its feature comprises: be encapsulation property, assembly must be hidden the realization details of its inside to the outside, makes the just interface that can see from outside; Assembly must dynamically be linked to together, needn't recompilate in the time of invoked.Give a unique sign for each assembly among the present invention and be used to create the function of this assembly.Compare with the COM method of prior art mentioned above, the present invention presets unique sign for each assembly, and is not as com component, and its ID is by obtaining after the registration in registration table.
Because assembly hidden its inner details to the outside, so the client just must promptly realize client and communication between components by certain method by certain mechanism will use assembly the time, and this just needs interface.So-called interface is exactly assembly " tie point " external, that service is provided to the external client.Outside client can not see the details of component internal, the just interface that it can be seen, and the client also is the service that comes securing component to provide by interface, and usually, the developer of assembly is responsible for realizing this interface, and the client then obtains service by interface.Client as herein described is meant the program or the assembly that will use some assemblies, that is to say, the client of this paper is a relative component.For each assembly provides an interface, be used for the function call of other assemblies among the present invention.
Set up a shared drive by application programming interfaces (API) application program, be used for the present invention to the management of module information and call.Described shared drive be established as techniques well known.
Step 12: each assembly is compiled, and be saved in designated directory; Also a plurality of assemblies can be compiled into an assembly collection as required, also be about to a plurality of assemblies and be compiled as a DLL or an EXE.
Concrete, each assembly is compiled, and be saved under the designated directory, the address information that identified component is preserved in the configuration file of master routine, the system that makes can arrive the correct following fetch program of catalogue when carrying out described configuration file and move required all component.
Step 13: when certain assembly is loaded into internal memory, triggers component identification and described function are saved in internal memory.
As indicated above, the principle of utilizing the constructed fuction of the global object of C++ programming language to carry out in the present embodiment prior to C Runtime Library (C-Runtime Library), message block in the constructed fuction of certain global object in the handle component (assembly ID, the establishment function of assembly) is stored on the internal memory of process in the operating system.
Each assembly all can provide an entry address, promptly apprizing system read behind this assembly the code that should carry out, described entry address refers generally to the address set to compiler.This address of C++ compiler meeting automatic setting, thus the constructed fuction of global object on this address, called.After assembly was loaded into internal memory, according to the entry address that assembly provides, system can remove to carry out constructed fuction automatically, and the present invention writes code in the constructed fuction of assembly, be used for preserving module information at shared drive.When process began, system was according to the reading assembly under the correct catalogue of the configuration file in the described master routine, so system carry out described constructed fuction with the unique identification of assembly and the function that is used for creating assembly be loaded into internal memory.
When loading the following module information of assigned catalogue, the information of an assembly of every loading will once travel through the operation of this catalogue, carry out input-output operation one time, waste system resource.Thereby, in the above-mentioned steps 12 a plurality of assemblies are compiled into an assembly (assembly collection), and then have reduced input-output operation during charging assembly, improved efficient.After a plurality of assemblies are compiled as an assembly collection, loading this assembly collection information in the process of internal memory, because, a different global object (grand use realizes by C++) is all arranged in each component code, the information of each assembly in this assembly collection is independent the preservation.
Step 14: generating assembly calls, and searches the target element sign and create assembly with call function in internal memory, returns the example of target element.By this example can invocation component each functional interface, realize funcall according to described interface.
When inter-module calls, by the module information of preserving in the shared drive, obtain the function pointer of creating assembly (target element) that be called, after assembly is created, return the component interface pointer, the client obtains the interface of target element by described component interface pointer, and then realizes the funcall of inter-module.
In the prior art, application program is loaded into internal memory after carrying out, and when application program was wanted invocation component, module information can be written in the internal memory.Supposing that application program need call 3 assemblies, is respectively d111, d112, d113, and program can be memory headroom of these three building components, for its writing information.Yet these three memory headrooms all are independently, and are irrelevant mutually, when causing three inter-modules to conduct interviews, realize loaded down with trivial details.
Adopted the mechanism of shared drive among the present invention.In the embodiment of the invention, shared drive of function creation that application program at first provides by system when the application call assembly, can be created a block space for each assembly in this piece shared drive, and each assembly can be visited mutually by component Name.Conducting interviews by component Name is a kind of naming mechanism that system provides for shared drive.
Usually, each assembly can only manage the space of oneself, and other assemblies and application program are all had no right to its management.For example: application call finishes d111, need to discharge this block space at d111 place, and application program haves no right to do this work.This defective causes in the component feature space management, and is very dumb.The present invention has set up unique sign ID for each assembly, by transfer assembly ID, can reach the purpose that inter-module carries out the memory headroom management.
With reference to Fig. 2, further specify the disposal route of component call process.
As shown in the figure, executable program EXE among the figure is made up of assembly 1, assembly 2, assembly 3, assembly 4, assembly 5.Process begins, when EXE is loaded into internal memory, assembly 1 is loaded in the shared drive of application program foundation respectively to assembly 5, the entry address that provides according to assembly, system automatically performs constructed fuction, and the information (unique ID and the function that is used for creating this assembly) of preserving each assembly is to shared drive.After module information is preserved, but the assembly releasing memory space that is loaded.Yet assembly is not the requisite operation of the present invention to the release of memory headroom.
When generating assembly calls, suppose assembly 1 invocation component 3, unique ID 3 according to assembly 3, find the function C oCreate3 () that is used to create assembly 3, after creating assembly 3, return the interface pointer of assembly 3, assembly 1 obtains this interface by the pointer of described assembly 3 interfaces, and passes through the power function of this interface interchange assembly 3.
More than a kind of call method between chip provided by the present invention is described in detail, used specific case herein principle of the present invention and embodiment are set forth, the explanation of above embodiment just is used for helping to understand method of the present invention and core concept thereof; Simultaneously, for one of ordinary skill in the art, according to thought of the present invention, the part that all can change in specific embodiments and applications, in sum, this description should not be construed as limitation of the present invention.