Microsoft KB Archive/43318

{|
 * width="100%"|

Spawned Program Accessing Parent's Functions

 * }

Q43318

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a | 1.00 1.50 MS-DOS | OS/2 | WINDOWS kbprg -- The information in this article applies to: - The C Run-time (CRT) included with: - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax - Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0 and 1.5 -- SUMMARY ======= A program spawned with the P_WAIT modeflag can successfully call functions within its parent. The functions must be set up in such a way, however, that CPU registers are preserved and the data segment register is loaded as necessary. It is also very important that all necessary startup code and C run time library routines are present whenever the parent functions are called. Warning: This procedure is neither recommended nor supported. This article is for informational purposes only. MORE INFORMATION ================ The programs below, PARENT.C and CHILD.C, demonstrate this technique. This method of sharing functions may be useful as a primitive form of overlaying when there is a need for a common set of functions to be accessed by a sequence of spawned child processes. The method of communication from the parent to the child is through command-line arguments. In the parent, a far pointer to the function that will be called is converted to a string and passed as a parameter through a spawn. In the child, it is then converted back into a far pointer. There are several considerations to be made when writing code of this nature: - For any variables in the parent to be accessed, the routines to be called must use the _loadds keyword. Not loading DS for the called function results in the child's DS being used rather than the DS associated with the function in the parent. - Even if _loadds is used, however, DS will not be equal to SS, because the child's stack is the one that is used and there is no mechanism in C for changing stacks. It is necessary to ensure that the functions called by the child do not blow out the child's stack. - Many of the run-time library routines rely on SS equaling DS; therefore, one must avoid those routines. - Preservation of the child's state can be accomplished by using the _saveregs keyword. This is not necessary when calling C from C; however, it may be vital if C is being called from MASM. - All calls must be far because the parent and child are loaded separately. Different memory models may be used for parent and child. - This process obviously produces a general-protection fault (GP fault) in OS/2. Use dynamic-link libraries to duplicate this functionality with greater ease, portability, and safety. Sample Code: The following is the parent program: /* * PARENT.C */ #include #include #include int far _saveregs _loadds myfunc(void); void main(void); int k = 0, l = 0; /* Globals to be accessed inside myfunc */ int far _saveregs _loadds myfunc(void) { int i, /* Return value */ j; /* Indexing */ for (j = 1; j < 10; j++) { k = k + j; l = k * j; } i = k + l; return (i); } void main { int (far _saveregs _loadds * myfuncaddr) (void); /* myfunc pointer */ char address[16]; /* address to pass */ printf(&quot;Now inside parent main.\n&quot;); myfuncaddr = (int (far _saveregs _loadds *) (void)) myfunc; ultoa((unsigned long) myfuncaddr, address, 10); printf(&quot;Address of myfunc: %s\n&quot;, address); spawnlp(P_WAIT, &quot;child.exe&quot;, &quot;child.exe&quot;, address, NULL); printf(&quot;Back inside parent main.\n&quot;); } The following is the child program: /* * CHILD.C */ #include #include void main(int argc, char **argv) { int (far _loadds _saveregs * myfuncaddr)(void); /* Pointer to parent's function */ int i; /* Function return value */ printf(&quot; Now in child.\n&quot;); myfuncaddr = (int (far _loadds _saveregs *)(void))atol(argv[1]); printf(&quot; Received: %ld\n&quot;, myfuncaddr); printf(&quot; Calling myfunc.\n&quot;); i = myfuncaddr; printf(&quot; Result of myfunc: %d\n&quot;, i); printf(&quot; Leaving child.\n&quot;); } Additional reference words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50 KBCategory: kbprg KBSubcategory: CRTIss

Keywords : kb16bitonly

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCRT