🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.delphi.components.misc
Article #27

TMemoryStream.Write overwrites source data

#27
From: David Block
Date: Fri, 27 Mar 1998 00:00
64 lines
2530 bytes
I'm trying to use a TMemoryStream to read the clipboard data. I need
to do this to read some special formats into a component that supports
LoadFromStream.

The problem is that when I do a Stream.Write to put the clipboard data
in a TMemoryStream for later access, the clipboard data is overwritten
by the TMemoryStream.SetCapacity() call.

I've reduced it to two similar functions. One writes a string to a
stream (button1click), the other TRIES to do the same thing with the
clipboard data. The former works, the later doesn't.

I hesitate to call it a bug in Delphi 3.02, but I can't figure out
what I'm doing wrong, and would appreciate any input.

This Works:
procedure TForm1.Button1Click(Sender: TObject);
var
   MemPtr: PChar;
   Stream : TMemoryStream;
   s : String;
Begin
   s := 'Hello, World' + char($0a) + 'Line 2';
   MemPtr := Pchar(s);
   Stream := TMemoryStream.Create;
   Stream.Write(MemPtr^, Length(s) + 1);
   Stream.Position := 0;
   Stream.SaveToFile('c:\windows\temp\Test.Txt');
   Memo1.Lines.LoadFromStream(Stream);
end;

This Fails:
{ For testing, supports only CF_TEXT. MAKE SURE text is in clipboard}
procedure TForm1.Button2Click(Sender: TObject);
var
   Handle:THandle;
   Stream : TMemoryStream;
   MemPtr: PChar;
Begin
   Stream := TMemoryStream.Create;
   if Clipboard.HasFormat(CF_TEXT) then
   Begin
      Handle := Clipboard.GetAsHandle(CF_TEXT);
      MemPtr := GlobalLock(Handle);
      try
         Stream.Write(MemPtr^, GlobalSize(Handle));
      finally
         GlobalUnlock(Handle);
      end;
      Stream.Position := 0;
      Stream.SaveToFile('c:\windows\temp\Test.Txt');
      Memo1.Lines.LoadFromStream(Stream);
   End;
end;


David Block
CoStar Corporation

Check out our label printers at www.costar.com


Email:  dblockREMOVE@costar.com
Technical Details
Message-ID: <351c0ee5.22341419@news.nothingbutnet.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f973b,59105672f7fbab34,start
X-Google-Attributes: gidf973b,public
X-Google-Thread: fb0ea,59105672f7fbab34,start
X-Google-Attributes: gidfb0ea,public
X-Google-Thread: 109d99,59105672f7fbab34,start
X-Google-Attributes: gid109d99,public
From: dblock@vdn.com (David Block)
Subject: TMemoryStream.Write overwrites source data
Date: 1998/03/27
Message-ID: <351c0ee5.22341419@news.nothingbutnet.net>
X-Deja-AN: 338371781
Organization: All USENET -- http://www.Supernews.com
Newsgroups: comp.lang.pascal.delphi.components.misc,comp.lang.pascal.delphi.components.usage,borland.public.delphi.vcl.components.using