From: JIMPRYOR@pucc.Princeton.EDU (Jim Pryor)
Subject: Launching one APPL from another
Originator: news@nimaster
Organization: Princeton University
Date: Sun, 3 Jan 1993 02:34:17 GMT

Could someone who understands the mechanics of launching an application
from another application take a look at the following code, and tell
me what I'm doing wrong? The launching works fine, but several
programs initialize strangely when launched this way. ResEdit says that
it's corrupted (but launches fine directly); various other programs are
unable to find their preferences files (especially programs that store
their preferences in the directory the application resides in, rather
than in the System Folder).
I imagine the trouble comes from the HSetVol call, but I'm not
sure why; and I don't know how to get around it (PBHSetVol would
probably yield the same results, but I haven't tried it). How else
would the assembly launch routines be able to locate the application,
since they're only provided with the name?
BTW, the assembly code is derived from a DA skeleton for launching programs
that I found on one of the archives (sumex-aim or wuarchive). Sorry for
the lack of attribution, but I've lost the original file.
 
Please respond by e-mail: jimpryor@pucc.princeton.edu
I'll summarize replies if anything turns up.
 
--- Code follows ---
 
program alias;
  type
    plaunchrec = launchrec;
    launchrec = record
        pfname: str255;
        param: integer;
      end;
    aka_h = aka_p;
    aka_p = aka_rec;
    aka_rec = record
        dirID: longint;
        fname: str255;
      end;
 
  procedure execlaunch;
  inline
    $a9f2;
  procedure initlaunch (plaunch: plaunchrec);
  inline
    $205f;  {move.l (a7)+, a0}
 
  var
    aka: aka_h;
    fname: str255;
    fvol: integer;
    res: oserr;
    dir: longint;
    info: finfo;
    launch: launchrec;
    x: integer;
    v, f: str255;
    s: signedbyte;
    hfp: hparamblockrec;
 
begin
{read 'AKA:' resource #1, which stores "hard" Directory ID}
{in a long integer, followed by a pascal string padded to 63 bytes}
  aka := aka_h(getresource('AKA:', 1));
  if aka = nil then
    debugstr('''AKA:'' resource #1 not found.');
{see if file stored in 'AKA:' resource exists}
  f := aka.fname;
  hfp.iodirid := aka.dirid;
  hfp.ionameptr := @f;
  res := pbhgetfinfo(@hfp, false);
  if res <> noerr then
    begin
{ask user to locate file}
{if sfgetfile not cancelled, update 'AKA:' resource}
      s := hgetstate(handle(aka));
      hnopurge(handle(aka));
      f := aka.fname;
      paramtext(concat('Please find ''', f, ''''), '', '', '');
{custom getfile routine, in separate unit; displays paramtext 0}
{returns true if and only if reply.good}
      if not getsfile(fname, fvol, 'APPL', 1) then
        exittoshell;
      res := setvol(nil, fvol);
      res := hgetvol(@v, x, dir);
      aka.dirID := dir;
      aka.fname := fname;
      changedresource(handle(aka));
      if reserror = noerr then
        writeresource(handle(aka))
      else
        sysbeep(10);
      hsetstate(handle(aka), s);
    end;
{I susupect this (unrecommended) use of hgetfile is the culprit:}
{maybe the launched programs expect the default volume to be the}
{root directory...}
  res := hsetvol(nil, 0, aka.dirID);
  launch.pfname := @aka.fname;
  launch.param := 0;
  initlaunch(@launch);
  execlaunch;
end.
Newsgroups: comp.sys.mac.programmer