Patent Publication Number: US-6665735-B1

Title: Method of changing a dynamic link library function efficiently and a computer system for executing the same

Description:
BACKGROUND OF THE INVENTION 
     1. Field of the Invention 
     The present invention relates to a method of changing a dynamic link library function efficiently and a computer system for executing the same. 
     2. Description of the Related Art 
     A linker is a program which links object modules, acquired by separate compiling, into a single loadable module by adjusting a reference relationship among the object modules or addresses thereof. To adjust a reference relationship among modules, a specific module registered in an object module library or a load module library is loaded as needed. In a case where a plurality of modules are linked, it is necessary to adjust the modules in such a way as to hold the adequate reference relationship among them and to allow execution of the modules to progress without problems. It is the biggest role of a linker to ensure such adjustment and link a plurality of modules into a single executable load module. 
     An object module does not consist only of a machine language program (the text portion), but also contains directory information called an external symbol directory and a relocation directory. Those directories are the important information sources for a linker. Registered in external symbol directory are external names and external references. The “external name” is a symbol name which can be referred to by another program module. The “external reference”, is a symbol name which is not defined in a local module. As an external name and external reference can be discriminated by an instruction in a source program, a language interpreting program identifies them and puts them in an object module. Each item registered in the external symbol directory contains an associated symbol name and information on where in the associated module that symbol name is used, i.e., an address at which the symbol name is used. 
     A linker collates the external name of a desired object module with an external reference, and gives the address for an external name to the external reference if the same name is found. This establishes linkage of both the external name and the external reference. In some cases, however, a match may not be found by merely referring to the external symbol directory of an input object module so that an unresolved external reference remains. In such a case, the linker searches an object module library or a load module library for a module to which a matching external name belongs and loads the module into a load module, and tries to find a matching external reference at the same time. 
     A set of object files of a previously prepared external function is called a library. An object file is a file which results from combining an external function and is stored in the form of an object program module. A library which is automatically linked by, for example, a C compiler is called a standard library. 
     A program in a library may take the form of a source program or may take the form of an object file. A program is linked to another program portion before compiling in the form of a source program, and after compiling in the form of an object file. In the case of linking a program after compiling, the linking action is called a linkage. There are two linkages: one is a static linkage which links a program module before executing it and the other is a dynamic linkage which carries out linking when a program is called after its execution has started. DLL, which is the latter linkage, is called a dynamic link library function where components or the like of Windows, the operating system which has substantially become standard in the field of personal computers, are stored. DLL is a library associated with a program, and can be shared by a plurality of program modules. A program module loads a DLL module into memory and uses it, whenever necessary. This DLL file helps reduce the program size an external memory device or memory, and enjoys merit of a shorter development time because correction or the like of software bugs can be implemented by rewriting only a target DLL file. 
     To alter the process of a dynamic link library function (X) to be used by a program module, conventionally, a dynamic link library function (Y) which includes a process equivalent to that of the dynamic link library function (X) is generated and the program module should be so modified as to call that function. 
     However, a dynamic link library including the dynamic link library function (Y) should have functions equivalent to all the dynamic link library functions included in a dynamic link library which includes the dynamic link library function (X). When there is no available means which acquires source codes of the dynamic link library function (X) and makes modification based on the source codes or when the total number of dynamic link library functions included in a dynamic link library is large, an extensive developing cost is required to alter the process of the dynamic link library function (X). 
     SUMMARY OF THE INVENTION 
     Accordingly, it is an object of the present invention to efficiently alter a dynamic link library function, at the time of generating a dynamic link library including a dynamic link library function (Y), by suppressing, as much as possible, the amount of codes to be newly added or generated for other portions of the dynamic link library function (Y) than those which are to be changed. 
     It is another object of this invention to provide a method of changing an external function to be referred, without recompiling a program module, by altering external function reference information described in the program module, and a computer system which executes the method. 
     According to one aspect of this invention, in a program module, which implements dynamic linkage to an external function and executes the external function, a function name described in a header portion of a program module and a function name to be referred to by the program module are replaced with other names, and functions having the same names as the replaced function names are linked and are executed in place of an original function. Further, an original function is executed using the functions having the same names as the replaced function names, and a preprocess or a postprocess for the original function is described and is executed to thereby expand a function without altering the original function. 
     According to another aspect of this invention, in a library A to be dynamically linked at-a time of executing a program module, a library B having the same name as the function A and including functions having the same names as functions included in the library A is dynamically linked and executed at the time of executing the program module, a library name described in the original library A and a function name, which can externally be referred to, are replaced with other names, and a function in the original library A is executed from a function in the library B and at the same time a preprocess or a postprocess is executed, whereby the preprocess or postprocess is added to a library function. 
     With the above structure, at the time of generating a dynamic link library including a dynamic link library function (Y), the amount- of codes to be newly added or generated for other portions of the dynamic link library function (Y) than those which are to be changed can be suppressed as much as possible. This can ensure efficient alteration of a dynamic link library function. 
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS 
     The accompanying drawings, which form a part of this specification, illustrate the presently preferred embodiments and are used to explain the principle of the invention as well as to give the general description that has been given above and the detailed description of the preferred embodiments that will be discussed below. 
     FIG. 1 is a diagram illustrating the structure of a computer system capable of efficiently changing a dynamic link library function; 
     FIG. 2 is a diagram showing a program structure according to a first embodiment of the invention, developed on a memory; 
     FIG. 3 is a diagram depicting a program structure according to a second embodiment of the invention, developed on a memory; 
     FIG. 4 is a diagram illustrating a program structure according to a third embodiment of the invention, developed on a memory; 
     FIG. 5 is a diagram showing a program structure according to a fourth embodiment of the invention, developed on a memory; 
     FIG. 6 is a diagram depicting a first symbol referring method, developed on a memory, which is to a to be used in each embodiment of the invention; 
     FIG. 7 is a diagram showing a second symbol referring method, developed on a memory, which is to a to be used in each embodiment of the invention; 
     FIG. 8 is a flowchart illustrating control procedures according to the first embodiment of the invention; 
     FIG. 9 is a flowchart illustrating control procedures according to the second embodiment of the invention; 
     FIG. 10 is a flowchart illustrating control procedures according to the third embodiment of the invention; 
     FIG. 11 is a flowchart illustrating control procedures according to the fourth embodiment of the invention. 
    
    
     DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS 
     The structures and operations of preferred embodiments of this invention will now be described while illustrating the developing environment on Windows 95 by Microsoft Corporation, which is substantially a standard operating system (OS) for personal computers. FIG. 1 exemplifies the structure of a computer system to be used in this invention. In the figure, a CPU  1 , the control center, reads programs including an OS, which are loaded and stored in a main memory device  2 , one at a time and executes them to perform operational control or control various devices connected to the computer system. In a program module for implementing dynamic linkage to an external function, which is loaded and stored in the main memory device  2  as needed, and executing the external function, the CPU  1  cooperates with the OS to replace a function name described in the header portion of the program module and a function name to be referred to by the program module with other names, link functions having the same names as the replaced function names and execute the linked functions instead of the original function. Further, the original function is executed using the functions having the same names as the replaced function names, and a preprocess or a postprocess is executed to thereby expand a function without altering the original function. 
     Windows 95, which is installed as a resident OS in the main memory device  2 , consists mainly of the following six parts, and is allocated and stored in the main memory device  2  together with an application program (APL) to be executed. Those six parts are a resident part (kernel  21 ), the main body of Windows 95, a part(device driver  22 ) which is closer to hardware and directly exchanges commands and data with the hardware, a part (registry  23 ) which sets the hardware and carries set information, a control part  24  for reading and writing files, a part (virtual machine  25 ) for controlling the usage of devices by an application, and a part (explorer  26 ) which receives a command from a user or an application and returns the results. Each part is further separated, function by function, into program files and data files in the forms of modules, which are in turn allocated and stored as Windows&#39; folders in the main memory device  2 . 
     Various application programs and data are stored in a large-capacity hard disk unit  3  and a CD-ROM drive unit  4 . The hard disk unit  3  is connected to the system via an IDE interface, and the CD-ROM drive unit  4  via an SCSI interface. Files stored in the large-capacity hard disk unit  3  and the CD-ROM drive unit  4  are read out via the file read/write control part  24  of the OS. The control part  24  also intervenes file writing in the hard disk unit  3 . When one opens the Windows 95 or the large-capacity hard disk unit  3  using the explorer  26 , multiple folders and/or files appear. A DLL file, which will be discussed later, is one of such files. 
     A mouse-equipped keyboard unit  5  and a printer  6  are connected to the system via a keyboard controller (KBC  8 ) and a parallel interface, respectively. Both units are managed by a file, called the virtual machine  25 , in such a way as not to be owned by a single application, thus ensuring multi-tasking, and their operations are controlled by a file called the device driver  22 . A display monitor  7  is connected to the system via a graphics display control LSI (GDC)  9 . The graphics display control LSI transfers graphics data, developed on a working area in the main memory device  2 , to a bit map memory  10 , and reads and displays desired data in synchronism with the display timing. The aforementioned six parts of the OS do not operate independently, but function in cooperation with one another. A system bus  20  consists of a plurality of lines for the address, data and control. The aforementioned units  1 ,  2 ,  3 ,  4 ,  6 ,  8  and  9  are connected to one another by this bus. 
     Before going to a description of the structures and operations of the embodiments of the invention, a symbol reference relationship between an application program and a dynamic link library will be discussed. Means by which a program module refers to symbols of a dynamic link library during its execution is classified into the following two ways. 
     FIG. 6 shows a first symbol referring method. As source codes  61  or  62  of a program or a dynamic link library are compiled and linked, an object module  63  is generated. The object module  63  consists of a header  631 , a text  632 , data  633 , an export section  634  and an import section  635 . The export section  634  includes information of function symbols which allow the module to refer to another module. The import section  635  includes information (DLL 1 , func 1 ; a set of the name of a library module which refers to a symbol and the name of the symbol) based on which the module refers to a function symbol from a library module. When a program directly calls a symbol (func 1 ) which the dynamic link library DLL 1  exports, a module  64  called an LIB file, which is generated as a result of compiling and linking the source codes of the DLL 1 , should be designated at the time of linkage. 
     Described in the LIB file are information of the library module name (DLL 1 ) and the name of a symbol (func 1 ) to be exported. As the program designates the LIB file at the time of linkage, information of the DLL 1  and func 1  are described in the import section  635  of the object module. When a program module is loaded on the memory during execution, the library module (DLL 1 ) is also loaded according to the information described in the import section  635 , and the load address of the func 1  is written in the import section  635  of the program module. As a result, when the func 1  is called during execution of the program module, the func 1  of the DLL 1  is executed in accordance with the address written in the import section  635 . 
     A second symbol referencing method will be discussed using FIG.  7 . As program module  73  executes a function (LoadLibrary) for dynamically loading a dynamic link library to load a library module (DLL 1 ), and executes a function (GetProcAddress) for acquiring the address of a symbol which the loaded library module will export, the func 1  can be executed. In this case, a program source code  71  need not designate the LIB file of the DLL 1  at the time of linkage, and information of the library module name “DLL 1 ” and the symbol name “func 1 ” are not described in an import section  735 . In the diagram, “ 71 ” denotes the source code of the program module and “ 73 ” denotes a program module (object program) which is generated by compiling the source code. The program module  73  consists of a header  731 , text  732 , data  733 , export section  734  and import section  735 , the same structure as used in the first symbol referring method illustrated in FIG.  6 . 
     FIG. 2 is an operational concept diagram illustrating one embodiment of the invention with the operation shown developed on the memory. Specifically, FIG. 2 shows an example of adding a preprocess or a postprocess to a function func 1 , without altering the module of the dynamic link library DLL 1  in a case where the program module has called the function func 1  which the dynamic link library DLL 1  will export, using the first referring method shown in FIG. 6. A preprocess or a postprocess is a routine which is added to expand a function, like data saving or data restoration. 
     In the figure, “ 1 ” is a CPU, “ 2 ” is a main memory device and “ 3 ” is an external memory device, like a large-capacity hard disk. First, the CPU  1  generates a dynamic link library  22  (DLL 2 ) including a function func 2  which is a preprocess or a postprocess added to a program  21  (func 1 ). Hereinafter, a function which is a preprocess or a postprocess to an original function is called a jacket routine, and a dynamic link library including a jacket routine is called a jacket library  22 . As the source codes of the program  21  or the dynamic link library  22  or  23  are compiled and linked, a program module  25 , a jacket library module  26  or an original library module  27  is generated. 
     As a LIB file  24  of the dynamic link library  23  (hereinafter called “original library (DLL 1 )”) is designated at the time of linkage, the jacket library  22  (DLL 2 ) imports the func 1  that is to be exported by the original library DLL 1  ( 23 ). As the module name “DLL 1 ” and the symbol name “func 1 ” described in an import section  255  of the program module  25  are respectively changed to DLL 2  and func 2 , the jacket library  22  (DLL 2 ) is loaded instead of the DLL 1  and the address of the func 2  is written in the import section  255 , when the program module  25  is loaded into the main memory device  2 . Accordingly, at the time of executing the program module, the func 2  is called instead of the func 1  and a preprocess or a postprocess can be added to the process of the func 1 . 
     FIG. 8 illustrates an operational flow when a program module is executed. As the import section is changed in the above-described procedures (step S 82 ), and the program module  25 , the jacket library  22  and the original library  23  are executed (steps S 81 , S 83  and S 84 ), it is possible to dynamically link the dynamic link library function and execute the program module. 
     Specifically, a preprocess or a postprocess can be added to a dynamic link library function and executed without changing the dynamic link library function, by replacing the dynamic link library name described in the header portion of the program module and.a function name, which is to be referred to by the program module, with other names, linking and executing a dynamic link library, which includes a function whose name is the same as the replaced function name and has the same name as the replaced library name, instead of the original dynamic link library, executing the original dynamic link library function using the function having the same name as the replaced function name, and executing the preprocess or postprocess. 
     FIG. 3 shows an example of adding a preprocess or a postprocess to a function func 1  without altering the program module in a case where the program module has called the function func 1 , which the dynamic link library DLL 1  will export, using the first referencing method shown in FIG.  6 . First, the jacket library DLL 1  ( 32 ) that has the same name as the original dynamic link library DLL 2  ( 33 ) is generated. As the name of the library ( 33 ) becomes identical to the name of the jacket library DLL 1  ( 32 ), the name is of the original library is changed to DLL 2 . The jacket library DLL 1  ( 32 ) exports the function func 1  having the same name as the function func 1  which the original library DLL 2  ( 33 ) will export. The func 1  which the jacket library DLL 1  ( 32 ) will export internally calls the func 1  which the original library DLL 2  ( 33 ) will export, and adds a preprocess or a postprocess to the called func 1 . Since functions with the same name cannot be called, the symbol name “func 1 ” described in an export section  394  of the original library DLL 2  ( 33 ) is changed to func 2 . For the func 1  which the jacket library DLL 1  ( 32 ) will export, therefore, func 2  should be called. 
     Referring to the func 2 , however, requires the LIB file  35  where the library module name “DLL 2 ” and the symbol name “func 2 ” are described. Because the module name “DLL 1 ” and the symbol name “func 1 ” are described in the LIB file  35  of the original library DLL 2  ( 33 ), however, it is insignificant to link this LIB file  35 . It is thus necessary to prepare an LIB file  31  which includes the library module name “DLL 2 ” and the symbol name “func 2 .” In this respect, a dummy dynamic link library  34  which has the library module name “DLL 2 ” is prepared. The dummy library  34  (DLL 2 ) includes a dummy function func 2 . Because the dummy function func 2  is needed only to describe the symbol name “func 2 ” in the LIB file  31 , it can be empty. As the module name “DLL 2 ” and the symbol name “func 2 ” are described in the LIB file  31 , which is acquired as a resulting of compiling the dummy library DLL 2  ( 34 ), this LIB file  31  can be designated at the time of linking the jacket library DLL 1  ( 32 ). When the func 1  is called at the time of executing the program module, therefore, the func 1  of the jacket library DLL 1  is called so that a preprocess or a postprocess can be added to the process of the func 1 . 
     FIG. 9 shows the operational flow. It becomes possible to dynamically link the dynamic link library function and execute the program by executing the program module (step S 91 ), generating the dummy library in the above-described procedures (step S 92 ) followed by the execution of the jacket library (step S 93 ), and then changing the export section in the above-described procedures (step S 94 ) followed by the execution of the original library (step S 95 ). 
     Specifically, in a dynamic link library (A) which is dynamically linked at the time of executing a program module  37 , a dynamic link library (B) which has the same name as the dynamic link library (A) and includes functions having the same names as those of functions included in the dynamic link library (A) is dynamically linked and executed at the time of executing the program module  37 , the dynamic link library name and an externally referable function name, described in the original dynamic link library (A), are replaced with other names, a function in the original dynamic link library (A) is executed from a function in the dynamic link library (B), and a preprocess or a postprocess is executed. This allows a preprocess or postprocess to be added to the dynamic link library function and executed without altering the program module  37 . 
     FIG. 4 shows an example of adding a preprocess or a postprocess to a function func 1  without altering both the program module and the module of the dynamic link library DLL 1  in a case where the program module has called the function func 1  which the dynamic link library DLL 1  will export, using the first referring method shown in FIG.  6 . 
     First, the jacket library DLL 1  ( 42 ) which has the same name as the original dynamic link library DLL 1  ( 43 ) is generated. The jacket library DLL 1  ( 42 ) exports the function func 1  having the same name as the function func 1  which the original library ( 43 ) will export. As the name of the original library ( 43 ) becomes identical to the name of the jacket library DLL 1  ( 42 ), the name is of the original library is changed to DLL 2 . The func 1  which the jacket library DLL 1  ( 42 ) will export executes a function LoadLibrary to dynamically load a dynamic link library, thereby loading the original library DLL 2  ( 43 ), and executes a function GetProcAddress for acquiring the address of a symbol which the loaded dynamic link library will export. Accordingly, the func 1  which the jacket library DLL 1  ( 42 ) will export can internally call the func 1  which the original library DLL 1  ( 43 ) will export, and can add a preprocess or a postprocess to the called func 1 . 
     When the func 1  is called at the time of executing a program module  45 , therefore, the func 1  of the jacket library DLL 1  is called so that a preprocess or a postprocess can be added to the process of the func 1 . FIG. 10 shows the operational flow. It is possible to dynamically link and execute the dynamic link library function by executing the individual steps of executing the program module first (step S 101 ), then executing the jacket library (step S 102 ), loading the original library (step S 103 ) and executing the original library (step S 104 ). 
     Specifically, in a dynamic link library (A) which is dynamically linked at the time of executing the program module, a dynamic link library (B) which has the same name as the dynamic link library (A) and includes functions having the same names as those of functions included in the dynamic link library (A) is dynamically linked and executed at the time of executing the program module, the dynamic link library (A) is dynamically loaded from a function in the dynamic link library (B) (step S 103 ), a function in the original dynamic link library (A) is executed and a preprocess or a postprocess is executed by using a function for acquiring the execution address of the dynamic link library function included in the loaded dynamic link library (A). This way allows a preprocess or postprocess to be added to the dynamic link library function and executed without altering the program module and the dynamic link library module. 
     FIG. 5 shows an example of adding a preprocess or a postprocess to a function func 1  without altering the module of the dynamic link library DLL 1  in a case where the program module has called the function func 1  which the dynamic link library DLL 1  will export, using the second referring method shown in FIG.  7 . To accomplish the addition, a process of changing the designated dynamic link library name to the jacket library name and a process of changing the designated symbol name to a symbol name which the jacket library will export are respectively added a function LoadLibrary for dynamically loading a dynamic link library and a function GetProAddress for acquiring the address of a symbol which the loaded dynamic link library will export, by using the method illustrated in FIG. 2 or FIG.  3 . 
     First, a jacket library Kernel 32 X ( 52 ) of a kernel  32  which is a dynamic link library including the functions LoadLibrary and GetProAddress is prepared. The jacket library Kernel 32 X ( 52 ) includes a function LoadLibraryX where a process of internally calling the function LoadLibrary and changing the given dynamic link library name to a jacket name is added, and a function GetProAddressX where a process of internally calling the function GetProAddress and changing the given symbol name to a jacket routine name is added. 
     Then, the module name “Kernel 32 ” described in an import section  555  of a program module  55  is rewritten to the jacket library name “Kernel 32 X,” and the symbol names “LoadLibrary” and “GetProcAddress” to “LoadLibraryX” and “GetProcAddressS” respectively. Accordingly, when the functions LoadLibrary and GetProAddress are called during execution of the program module  55 , the jacket routines LoadLibraryX and GetProcAddressS are executed. By replacing the dynamic link library name “DLL 1 ” or the symbol name “func 1 ”, shown in, for example, FIG. 6, is replaced with the jacket name “DLL 2 ” or the jacket routine name “func 2 ” when the name “DLL 1 ” or “func 1 ” is given to the function LoadLibraryX or GetProAddressX, therefore, the function func 2  which the jacket library DLL 2  will export is executed when the function func 1  which the dynamic link library DLL 1  will export is called during execution of the program module  55 . It is thus possible to add a preprocess or a postprocess to the process of the func 1 . 
     FIG. 11 shows the operational flow. It is possible to dynamically link the dynamic link library function and execute the preprocess or postprocess specifically by changing the import section according to the above-described procedures (step S 111 ) followed by the execution of the program module (step S 112 ), loading the jacket library (step S 113 ), executing the jacket library (step S 114 ), and executing the original library (step S 115 ). 
     Specifically, in a program module which, at the time of execution, dynamically links and executes a dynamic link library function by using a function (a) for dynamically loading a dynamic link library, a process of replacing the name of a dynamic link library which is requested to be loaded with another name is added to the function (a) by using the method shown in FIG. 1 or FIG.  2 . Accordingly, the load-requested dynamic link library is replaced with another dynamic link library without altering the dynamic link library or without altering the program module. 
     Further, by adding a process of replacing the name of a function whose execution address is requested with another name by using the method shown in FIG. 2 or FIG. 3 to a function (b) of for acquiring the execution address of a function included in the dynamic link library that has dynamically been loaded by the program module, the function whose execution address has been requested is replaced with another function without altering the dynamic link library or without altering the program module. 
     As apparent from the foregoing description, this invention is characterized in that in a program module, which implements dynamic linkage to an external function and executes the external function, a function name described in a header portion of a program module and a function name to be referred to by the program module are replaced with other names, and functions having the same names as the replaced function names are linked and executed instead of the original function, and a preprocess or a postprocess is executed, thereby allowing a function to be expanded without altering the original function. With this feature, at the time of generating a dynamic link library including a dynamic link library function (Y), the amount of codes to be newly added or generated for other portions of the dynamic link library function (Y) than those which are to be changed can be suppressed as much as possible. This can ensure more efficient alteration of a dynamic link library function and can reduce the developing cost. 
     It should be apparent to those skilled in the art that other advantages and other modifications of the present invention may be obtained easily. Therefore, this invention is not limited to the details, the specific apparatuses, and the present examples all given herein. Therefore, the invention may be modified in many other specific forms within the scope of the appended claims and without departing from the spirit and scope of the invention defined by the scope equivalent to that of the appended claims.