Thread View: comp.lang.pascal.delphi.components.misc
1 messages
1 total messages
Started by David Block
Fri, 27 Mar 1998 00:00
TMemoryStream.Write overwrites source data
Author: David Block
Date: Fri, 27 Mar 1998 00:00
Date: Fri, 27 Mar 1998 00:00
64 lines
2530 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads