Programs sometime need to get a pointer to their CLI window. A method for obtaining this pointer was illustrated in the \IAmiga Mail\i article entitled "CLIColors.asm - Using ConsolePackets in 68000 Assembler" (page II-65, July/August 1989 \IAmiga Mail\i). However, many C programmers would rather avoid using assembly language. This article shows how to use C to get a pointer the CLI window.
The basic method is the same whether you use C or Assembler. You send an \ff<Courier>\fs<10>ACTION_DISK_INFO\ff<Times>\fs<12> packet to the console handler process which includes a pointer to an InfoData struture. When the packet is returned by AmigaDOS, the id_VolumeNode field of the InfoData structure will be filled in with a pointer to the CLI window that the console handler is using.
The program below, WindowPtr.c\I,\i follows the code of CLIColors.asm closely. Like the latter program, the strategy behind WindowPtr.c is to:
\s\ff<zapfdingbats>\lr<160>o\ff<Times> Make sure the program was launched from a CLI.
\s\ff<zapfdingbats>o\ff<Times> Send an \ff<Courier>\fs<10>ACTION_DISK_INFO\ff<Times>\fs<12> packet.
\s\ff<zapfdingbats>\lr<120>o\ff<Times> Extract the window pointer from the information
\s\sreturned in the packet.
The program starts by examining certain structures to determine if it was launched from a CLI. If it passes that test, the program checks its Process structure to make sure that it has a valid pointer to a console task. Next, the program allocates memory for FindWindow, a custom structure composed of a struct StandardPacket and a struct InfoData.
The program then initializes the StandardPacket fields and sends an \ff<Courier>\fs<10>ACTION_DISK_INFO\ff<Times>\fs<12> packet to the DOS. When the packet returns, the program checks for an error. An error could possibly be caused by the CLI not having an associated window. A CLI that uses the AUX: handler rather than the CON: handler could cause such an error.
\lr<115>If the packet returns without an error, the id_VolumeNode field of the InfoData structure points to the CLI window. This is an ordinary C pointer, not a BPTR. The program shows that the pointer obtained does indeed point to the current CLI window by printing out a few of the window's parameters.
WindowPtr.c is listed below. It was compiled under Lattice 5.05 using the command:
LC -b1 -cfist -L -v -w WindowPtr.c. Link with lc.lib and amiga.lib.\fs<9>
\ff<Courier>\lr<120>/*
* File name: windowptr.c
* Purpose: To illustrate how to get the pointer
*\s\s\s\sto the current CLI window.
* Requires: Must be started from a CLI
#include <libraries/dosextens.h>\s\s/* Defines the StandardPacket
\s\s\s\s\s\s\s\s\s\s\s * structure, the packet types,
\s\s\s\s\s\s\s\s\s\s\s * and the Process structure
\s\s\s\s\s\s\s\s\s\s\s */
#include <libraries/dos.h>\s\s\s/* Defines the InfoData structure */