<< Localizing
>> Pragmas

How to obtain a pointer to a console's window

The following function returns the window pointer of a CON window. It can be executed safely under all versions of the Amiga's OS.

  struct Window *getConWindowPtr(BPTR fh)
  {
    struct Window *w;
    struct FileHandle *cfh;
    struct StandardPacket *sp;
    struct InfoData *id;
    struct MsgPort *mp;

    w = NULL;

    if ((cfh = BADDR(fh))->fh_Type != NULL) {
      if (sp = AllocMem(sizeof (struct StandardPacket),
                        MEMF_PUBLIC | MEMF_CLEAR)) {
        if (id = AllocMem(sizeof (struct InfoData),
                          MEMF_PUBLIC | MEMF_CLEAR)) {
          if (mp = CreatePort(NULL, 0)) {
            sp->sp_Msg.mn_Node.ln_Name = (char *) &sp->sp_Pkt;
            sp->sp_Pkt.dp_Link         = &sp->sp_Msg;
            sp->sp_Pkt.dp_Port         = mp;
            sp->sp_Pkt.dp_Type         = ACTION_DISK_INFO;
            sp->sp_Pkt.dp_Arg1         = MKBADDR(id);

            PutMsg(cfh->fh_Type, &sp->sp_Msg);
            (void) WaitPort(mp);
            (void) GetMsg(mp);

            if (sp->sp_Pkt.dp_Res1)
              w = (struct Window *) id->id_VolumeNode;

            DeletePort(mp);
          }
          FreeMem(id, sizeof (struct InfoData));
        }
        FreeMem(sp, sizeof (struct StandardPacket));
      }
    }

    return w;
  }

Notes:

For more information, please refer to pages 273, 276, 435, 463, 485, and 629 in "The Amiga Guru Book" (see What documentation do I need as an Amiga programmer?).

Ralph Babel, rbabel@babylon.pfm-mainz.de



<< Localizing >> Pragmas