Okay... Here is a similair routine, but now to write a string.

Writes MaxLength characters of a Null-Terminated String to a MemoryStream. The routine doesn't write the NullChar. If a NullChar is found in the string, Only the precending Chars are written. Returns the number of bytes written.
[pascal]
function WriteStr(Buffer: PChar; MaxLength: Integer): Integer;
type
TCharArray = array [0..MaxInt-1] of Char;
PCharArray = ^TCharArray;
var
I: Integer;
Chars: PCharArray;
begin
Chars := Pointer(Buffer);
for I:=0 to MaxLength-1 do
if Chars[I] = #0 then Break;

Result := Stream.Write( Buffer^, I );
end;
[/pascal]