[comp.windows.ms] Spawning in Windows

mikek@ziebmef.uucp (Mike King) (05/19/89)

Recently I posted an article asking for help spawning other apps from 
within a Windows app.  I received a number of mail responses and I tried
out all of the suggestions.  Since I've seen others asking the same question
as I did I thought I would present my findings.

1 - spawn? from the standard library will work --- most of the time!
    Sometimes it seems to crash in a nasty way.

2 - using DOS function 4B works --- most of the time!
    Sometimes it will crash as well, usually with ENOMEM.

3 - the winning suggestion was received from Microsoft, and confirmed by
    someone from hDC (sorry I forget your name).

Here goes:
     
        /* reliable Windows spawn code */
        GlobalCompact((DWORD)-1);    /* compact the global heap */
        LockData(0);                 /* lock down your data segment */
        spawnl(P_NOWAIT, "newapp.exe", "newapp", NULL);
        UnlockData(0);               /* unlock your data segment */

    The interesting bits are the GlobalCompact - to give yourself some room,
    and the LockData - stop data pointers from becoming orphans.

jerryd@hpgrla.HP.COM (Jerry Donovan) (05/31/89)

> Recently I posted an article asking for help spawning other apps from 
> within a Windows app.  I received a number of mail responses and I tried
> out all of the suggestions.  Since I've seen others asking the same question
> as I did I thought I would present my findings.
...
> 3 - the winning suggestion was received from Microsoft, and confirmed by
>    someone from hDC (sorry I forget your name).

>Here goes:
>        /* reliable Windows spawn code */
>        GlobalCompact((DWORD)-1);    /* compact the global heap */
>        LockData(0);                 /* lock down your data segment */
>        spawnl(P_NOWAIT, "newapp.exe", "newapp", NULL);
>        UnlockData(0);               /* unlock your data segment */
>
>    The interesting bits are the GlobalCompact - to give yourself some room,
>    and the LockData - stop data pointers from becoming orphans.

Does this work for libraries? (where DS!=SS) if so, then what exactly does
LockData do?  if not, then what would work there?

Thanks,

bturner@hpcvlx.HP.COM (Bill Turner) (05/31/89)

>>    The interesting bits are the GlobalCompact - to give yourself some room,
>>    and the LockData - stop data pointers from becoming orphans.
>
> Does this work for libraries? (where DS!=SS) if so, then what exactly does
> LockData do?  if not, then what would work there?

I think it will.  When your program is executing, DS is automatically locked
for you.  The problem is that the spawn call unlocks DS to do its work.  So,
if you LockData again, then DS is locked twice (the locks are counted), so 
spawn unlocking it once still leaves it locked, and you're safe.

Now, if you're in the library code, the stack segment is already locked (because
for the program, DS == SS).  But you will need to lock the library's DS, other-
wise the same problem can occur (of DS moving underneath the spawn call).

[This raises an entirely different problem about locking the library's DS,
which I'll put into another note...]

--Bill Turner (bturner@hp-pcd.hp.com)
HP Corvallis Information Systems