The Set and GetApwString functions

Now we will add functions that get and set text variables. Make sure that apw.pas file is showing in the code window. Add these function declarations after the SetApwNumber declaration.

function GetApwString( variable : Pchar ; value : Pchar ; size : word ; wnd : hWnd ) : Boolean;
function SetApwString( variable : Pchar ; value : Pchar ; wnd : hWnd ) : Boolean;

Now add the functions after the SetApwNumber function:

function GetApwString( variable : Pchar ; value : Pchar ; size : word ; wnd : hWnd ) : Boolean;
var
   varPB : APWC_VARPB;
   err: LongInt;
   hString : THandle;
   lpString : Pchar;
begin
     result := false;
     varPB.var_Name := variable;
     varPB.at_icon_name := nil;
     varPB.at_icon_id := 0;
     varPB.flags := 0;
     varPB.eval.ev_type := APW_STR_TYPE;
     err := SendMessage( wnd , APWC_GETVAR , 0 , LongInt( @varpb ) );
     if err = EXPR_OK then
        begin
        hString := varPB.eval.string_value;
        lpString := GlobalLock( hString );
        strlcopy( value , lpString , size - 1 );
        GlobalUnlock( hString );
        GlobalFree( hString );
        result := true;
        end;
end;

function SetApwString( variable : Pchar ; value : Pchar ; wnd : hWnd ) : Boolean;
var
   varpb : APWC_VARPB;
   err: LongInt;
   hString : THandle;
   lpString : Pchar;
begin
     result := false;
     varPB.var_Name := variable;
     varPB.at_icon_name := nil;
     varPB.at_icon_id := 0;
     varPB.flags := SV_DONTCREATE;
     varPB.eval.ev_type := APW_STR_TYPE;
     varPB.eval.string_ptr := value;
     err := SendMessage( wnd , APWC_SETVAR , 0 , LongInt( @varpb ) );
     if err = EXPR_OK then result := true;
end;

These functions are similar in concept to the number functions. The GetApwString function takes a pointer to string buffer and the size of that buffer. The result is copied into the buffer. The problem with this approach is that you need to specify a maximum size of the retrieved string. This may not work in all scenarios, but will suffice for this example.

On to: The UCase function

Back to: Setting a variable value      Start