Gettin HTML source from clipboard
lazarus / 31-LCL
----------------------------------------------------------------------
Maris · 2015-01-15 11:10
Hello, everybody!
I guess I will need some help and advice on how to work correctly with
clipboard.
I used example from wiki about how to do that. Here is my code:
Code: [Select]procedure TForm1.PasteButtonClick(Sender: TObject);
var
Fid: TClipboardFormat;
Str: WideString;
Stream: TMemoryStream;
Source: String;
begin
Fid := Clipboard.FindFormatID('text/html');
if Fid <> 0 then
try
Stream := TMemoryStream.Create;
if Clipboard.GetFormat(Fid, Stream) then
begin
Stream.Position := 0;
Str := PWideChar(Stream.Memory);
Str[(Stream.Size div 2) + 1] := #0; {Is this string really
necessary?}
Memo1.Append('Stream size: ' + IntToStr(Stream.Size));
Memo1.Append('Symbols: ' + IntToStr(Stream.Size div 2));
Memo1.Append('Str length: ' + IntToStr(Length(Str)));
Source := UTF8Encode(Str); {Source string will be used for HTML
parsing.}
Memo1.Append('Source length: ' + IntToStr(Length(Source)));
end;
finally
Stream.Free;
end
end;
Lines added to Memo is for debugging purpose.
There is a comment about line:
Code: [Select]Str[(Stream.Size div 2) + 1] := #0;
If this line is necessary? It looks logical of course and necessary,
but while this line is in code, I got very strange behavior of
resulting program. It could rise exception, it could close, and some
antiviruses even blocking program activity and it just hangs (on
windows server 2008 R2, Nod32 antivirus). I think this is because
program is trying access memory which is not reserved or something
like this.
Debugging is complicated, as it happens randomly, the same web page
(so the same source code) can work fine and then suddenly the same
content makes program to crash. One more strange thing is that it
never ever happened on development computer (Windows 7 Pro 64 bit,
Lazarus/FPC 32 bit), though program crashes on similar windows 7 pro
system with no lazarus installed. Also tested on several windows xp
(normal PC and virtual), several windows7 pro and windows server 2008,
everywhere got the same results, program crashes randomly.
So far solved problem by removing line where string terminating symbol
is added. It works now, and as this program is for temporal internal
use, it can stay like this, but I'm just curious, what can rise such
behavior of program, and if it's ok to remove that line? Maybe now
there can be some memory leaks, which can't be captured by Heaptrc
unit, or something like this?
And one more question. Is there any recommended tools and approach for
debugging such issues?
Sorry for long writing, just wanted to explain problem in details.
Thanks in advance!
----------------------------------------------------------------------
marcov · 2015-01-15 11:18
Assume
1. the heap manager allocates blocks in 16 bytes increments.
2. the block returned by the heapmanager is at the end of a page.
(4096 bytes).
-> this means that the byte after the current one might not be
allocated.
So assume the stream has a memory buffer of 32 bytes allocated like
above.
str[stream.size div 2+1] is equal to str[17] which is in the next
block -> GPF.
Note that this only happens when the size is a multiple of the
allocation size (+/- a value for heap administration).
So best is to write two 0 bytes to the memory stream (before the
.position=0), or use an utf8encode version that takes a pointer and
number of bytes. (don't know if it exists)
If that exists, in general that is better anyway, since most Pascal
strings can contain zero characters.
----------------------------------------------------------------------
Maris · 2015-01-16 07:27
Marcov, thanks for your reply. Changed code to this:
Code: [Select]if Clipboard.GetFormat(Fid, Stream) then
begin
Stream.Write(#0#0, Length(#0#0));
Stream.Position := 0;
Str := PWideChar(Stream.Memory);
Source := UTF8Encode(Str); {Source string will be used for HTML
parsing.}
end;
Looks like it is working.
Would that mean there is a mistake in Free Pascal wiki, about how to
work with clipboard? I mean here:
http://wiki.freepascal.org/Clipboard
----------------------------------------------------------------------
marcov · 2015-01-16 10:40
Quote from: Maris on January 16, 2015, 08:27:19 am
Looks like it is working.
Would that mean there is a mistake in Free Pascal wiki, about how to
work with clipboard? I mean here:
It's wiki, anybody can edit. Anyway I corrected it.
----------------------------------------------------------------------
Maris · 2015-01-23 12:06
Already tested program for a few days and it is stable now. Thank's
Marcov for your help.
Not sure if there is a version of function you mentioned in first
reply. I found something similar - UnicodeToUtf8, but didn't check it
yet, so don't know if it would be better solution.