(* * * * * * * * * * * * * * * * * * *
 *
 * prog4-4.pas
 *
 *      Demonstrates
 *          - Get all handle names
 *
 * * * * * * * * * * * * * * * * * * *)

program prog4_4;

uses    _globals,
        _ems;

var
    ems:                EMS_Ems;

    handle1:            EMS_EmBlk;
    handle2:            EMS_EmBlk;
    handle3:            EMS_EmBlk;

    numHandles:         Word;

    handleNameArray:   ^EMS_HandleNameInfoArr;

    nameBuff:           EMS_HandleName;

    i:                  Word;

begin

    (*
     *  First check for presence of EMS
     *)
    if ems.init then begin
        writeln('EMS is not present');
        Halt;
        end;

    (*
     *  Allocate some handles and name them:
     *)
    if handle1.allocEM(1) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;
    if handle2.allocEM(1) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;
    if handle3.allocEM(1) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;
    nameBuff:= 'Larry   ';
    if handle1.setHandleName40(nameBuff) then begin
        ems_demoError('EMS_EmBlk.setHandleName40');
        end;
    nameBuff:= 'Moe     ';
    if handle2.setHandleName40(nameBuff) then begin
        ems_demoError('EMS_EmBlk.setHandleName40');
        end;
    nameBuff:= 'Curly   ';
    if handle3.setHandleName40(nameBuff) then begin
        ems_demoError('EMS_EmBlk.setHandleName40');
        end;

    (*
     *  Allocate space for the names.
     *)
    if ems.getNumActiveHandles(numHandles) then begin
        ems_demoError('EMS_Ems.getNumActiveHandles');
        end;

    GetMem(handleNameArray, sizeof(EMS_HandleNameInfo) * numHandles);

    (*
     *  Get the names for the handles.
     *)
    if ems.getAllHandleNames40(handleNameArray^) then begin
        ems_demoError('EMS_Ems.getAllHandleNames40');
        end;

    (*
     *  Now print the info out:
     *)
    writeln('ÉÍÍHandleÍÍÑÍÍÍÍNameÍÍÍÍ»');

    for i:= 0 to numHandles-1 do begin
        writeln('º   ',
                handleNameArray^[i].handle:4,
                '   ³  ',
                handleNameArray^[i].name,
                '  º');

        if i < numHandles-1 then begin
            writeln('ÇÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄ¶');
            end;
        end;
    writeln('ÈÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍ¼');  

    if handle1.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
    if handle2.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
    if handle3.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;

end.

