Microsoft KB Archive/40774

{|
 * width="100%"|

How to Pass a Far Pointer from Parent to Spawned Child

 * }

Q40774

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50 MS-DOS | OS/2 | WINDOWS kbprg -- The information in this article applies to: - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax - Microsoft C for OS/2, versions 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 ======= The best way to pass a pointer from a parent process to a child is to convert the pointer to an ASCII string using sprintf, spawn the child with the string on the command line, and convert the string back to a pointer with sscanf. Below is a sample program that does this. Note that the program and the child process must be compiled separately. Be sure to pass a far (segment and offset) pointer rather than a near (offset only) pointer. Attempting to pass a near pointer will not work. MORE INFORMATION ================ It is also possible to leave the pointer in an agreed-upon memory area. Sometimes unused interrupt vectors are used for this purpose under DOS. Microsoft does not recommend this method because it could cause conflicts with other software using the same memory. The method described above is much safer. Sample Code --- Compile the following two programs separately. Be sure to name the child process MAIN2.EXE, or change the name in the spawnlp call in the parent process to match your name. Parent Process -- /* Compile options needed: /AL */ #include #include #include #include #include char buffer[80]; /* buffer to be passed to child */ char ascptr[20]; /* holds ASCII version of ptr. */ void main(void) { int ret; /* initial string */ strcpy(buffer, &quot;Main1&quot;); printf(&quot;Beginning main1...\nString is: %s\n&quot;, buffer); /* convert address to ASCII */ sprintf(ascptr, &quot;%p&quot;, buffer); printf(&quot;ASCII version of pointer is: %p\nSpawning main2...\n\n&quot;, ascptr); /* spawn, passing ASCII address on command line */ ret = spawnlp(P_WAIT, &quot;main2.exe&quot;, &quot;main2&quot;, ascptr, NULL); printf(&quot;\nBack to main1--spawnlp returned %d\n&quot;, ret); /* Show that string is changed */ printf(&quot;String is now: %s\nAll done&quot;, buffer); exit(0); } /* end of program number one*/ Child Process - /* Compile options needed: /AL be sure to name this MAIN2.EXE (main2.c) */ #include #include #include #include #include #include char far *ptrstuff; void main(int argc, char **argv) { int ch; printf(&quot;In main2: argv[1] is %s\n&quot;, argv[1]); if (argc != 2) { /* error in argument list */ printf(&quot;Error in argument list to main2\n&quot;); exit(1); } /* retrieve pointer value from string */ sscanf(argv[1], &quot;%p&quot;, &ptrstuff); printf(&quot;\nPointer is actually set to: %p\n&quot;, ptrstuff); /* check to see that it's right */ printf(&quot;Pointer points to: %s\n&quot;, ptrstuff); /* change string (CAREFULLY!!!!!) */ printf(&quot;Warning: modifying the string incorrectly could hang &quot; &quot;your system...\n&quot;); printf(&quot;\007Should I modify this string? &quot;); ch = getchar; if (toupper(ch) == 'Y') { strcpy(ptrstuff, &quot;2nd main&quot;); printf(&quot;Changed string to: %s\n&quot;, ptrstuff); } else printf(&quot;String not changed....\n&quot;); printf(&quot;Exiting main2...\n&quot;); exit(0); } /* end of child process */ 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 kbPTProdChange kbvc150 kbvc100 kbCCompSearch kbZNotKeyword3 kbCComp600DOS kbCComp600aDOS kbCComp600axDOS kbCComp600OS2 kbCComp600aOS2 kbCVC700DOS