I have a method that reads the data in the cells of a row of a TStringGrid, and copies it to the clipboard. And I have a corresponding method to paste the data from the clipboard into an empty row in the TStringGrid.
These methods were written for D7, but are broken after migration to XE2.
procedure TfrmBaseRamEditor.CopyLine(Sender: TObject; StrGridTemp: TStringGrid;
Row, Column: Integer);
var
Stream: TMemoryStream;
MemHandle: THandle;
MemBlock: Pointer;
i, Len: Integer;
RowStr: String;
begin
Stream := nil;
try
Stream := TMemoryStream.Create;
// The intermediate format to write to the stream.
// Separate each item by horizontal tab character.
RowStr := '';
for i := 0 to (StrGridTemp.ColCount - 1) do
RowStr := RowStr + StrGridTemp.Cells[i, Row] + #9;
// Write all elements in a string.
Len := Length(RowStr);
Stream.Write(Len, SizeOf(Len));
Stream.Write(PChar(RowStr)^, Length(RowStr));
// Request Memory for the clipboard.
MemHandle := GlobalAlloc(GMEM_DDESHARE, Stream.SIZE);
MemBlock := GlobalLock(MemHandle);
try
// Copy the contents of the stream into memory.
Stream.Seek(0, soFromBeginning);
Stream.Read(MemBlock^, Stream.SIZE);
finally
GlobalUnlock(MemHandle);
end;
// Pass the memory to the clipboard in the correct format.
Clipboard.Open;
Clipboard.SetAsHandle(TClipboardFormat, MemHandle);
Clipboard.Close;
finally
Stream.Free;
end;
end;
procedure TfrmBaseRamEditor.PasteLine(Sender: TObject; StrGridTemp: TStringGrid;
Row, Column: Integer);
var
Stream: TMemoryStream;
MemHandle: THandle;
MemBlock: Pointer;
ASize, Len, i: Integer;
TempStr: String;
begin
Clipboard.Open;
try
// If something is in the clipboard in the correct format.
if Clipboard.HasFormat(TClipboardFormat) then
begin
MemHandle := Clipboard.GetAsHandle(TClipboardFormat);
if MemHandle <> 0 then
begin
// Detect size (number of bytes).
ASize := GlobalSize(MemHandle);
Stream := nil;
try
Stream := TMemoryStream.Create;
// Lock the contents of the clipboard.
MemBlock := GlobalLock(MemHandle);
try
// Copy the data into the stream.
Stream.Write(MemBlock^, ASize);
finally
GlobalUnlock(MemHandle);
end;
Stream.Seek(0, soFromBeginning);
Stream.Read(Len, SizeOf(Len));
SetLength(TempStr, Len);
Stream.Read(PChar(TempStr)^, Stream.SIZE);
for i := 0 to StrGridTemp.RowCount do
StrGridTemp.Cells[i, Row] := NextStr(TempStr, #9);
finally
Stream.Free;
end;
end;
end;
finally
Clipboard.Close;
end;
end;
The problem manifests when I copy a row with some values, then paste it into an empty row. The first cell is pasted correctly, but the second cell contains garbage characters (and nothing is pasted in the 3rd column onwards). I know why nothing is pasted in 3rd column onwards: because the "horizontal tab" character which separates the columns is corrupted along with the cell contents.
I've looked through "Delphi and Unicode" by Marco Cantu, but haven't been able to figure out where it's all going wrong.
Char is an alias for WideChar. So in CopyLine
Stream.Write(PChar(RowStr)^, Length(RowStr));
only writes half the string. It should be
Stream.Write(PChar(RowStr)^, Length(RowStr)*SizeOf(Char));
In PasteLine I find this line odd:
Stream.Read(PChar(TempStr)^, Stream.SIZE);
Since you've already consumed some of the string you are attempting to read past the end. I'd write it like this:
Stream.Read(PChar(TempStr)^, Len*SizeOf(Char));
Note that if you use the same custom clipboard format identifier as your ANSI program then you'll have encoding mismatches if you copy from one and paste into the other. You might be wise to register under a different clipboard format for your new Unicode format.
Some other comments:
Stream := nil;
try
Stream := TMemoryStream.Create;
...
finally
Stream.Free;
end;
should be written as:
Stream := TMemoryStream.Create;
try
...
finally
Stream.Free;
end;
If the constructor raises an exception, the try block will not be entered.
You don't really need to write out the string length. You can rely on the stream size when reading to know how long the string is.
In CopyLine, the clipboard Open and Close calls should be protected by a try/finally block.
Related
I am using Delphi XE3.
In my code, I will need to create a file stream, and then write some my own data, as well as the contents of several TStringList into it. The file is in UTF-16LE format.
Therefore, my code:
FileStream := TFileStream.Create('D:\MyFile.dat', fmCreate or fmOpenWrite or fmShareExclusive);
try
// Write some data to FileStream
// Write contents of StringList1 into FileStream
StringList1.SaveToStream(FileStream, TEncoding.Unicode);
// Write some more data to FileStream
// Write contents of StringList2 into FileSteram
StringList2.SaveToStream(FileStream, TEncoding.Unicode);
finally
FileStream.Free;
end;
After executing the codes, I find a problem, each time I invoke StringList1.SaveToStream(FileStream, TEncoding.Unicode); it will write BOM (0xFFFE) then followed by the actual strings in the string list.
Therefore, I get a Unicode file like this:
0xFFFE(The first one is written by myself)
(some data)
0xFFFE (StringList1 contents)
(some data)
0xFFFE (StringList2 contents)
But this is not I expect since there should be only one 0xFFFE at the beginning of the file. Therefore, I just wonder how to prevent StringList1.SaveToStream to write the 0xFFFE BOM before writing the actual string lists?
I find another solution for my question.
TStrings has a WriteBOM property, which will control whether to write out the BOM when using SaveToStream or SaveToFile.
Therefore, using the following codes will disable the BOM:
StringList1.WriteBOM := False;
StringList1.SaveToStream(FileStream, TEncoding.Unicode);
You can use SaveToStream() to save to a TMemoryStream first, then set that stream's Position to skip past the BOM in it and save the rest of its data to the TFileStream.
procedure WriteUnicodeStrings(AStream: TStream; AStrings: TStrings);
var
MS: TMemoryStream;
begin
MS := TMemoryStream.Create;
try
AStrings.SaveToStream(MS, TEncoding.Unicode);
if MS.Size > 2 then
begin
MS.Position := 2;
AStream.CopyFrom(MS, MS.Size-2);
end;
finally
MS.Free;
end;
end;
...
FileStream := TFileStream.Create('D:\MyFile.dat', fmCreate or fmOpenWrite or fmShareExclusive);
try
// Write some data to FileStream
WriteUnicodeStrings(FileStream, StringList1);
// Write some more data to FileStream
WriteUnicodeStrings(FileStream, StringList2);
finally
FileStream.Free;
end;
Or, you can simply derive a class from SysUtils.TUnicodeEncoding and override its GetPreamble() method to not return any BOM, then use that class instead of using TEncoding.Unicode.
type
TMyUnicodeEncoding = class(TUnicodeEncoding)
public
function GetPreamble: TBytes; override;
end;
function TMyUnicodeEncoding.GetPreamble: TBytes;
begin
Result := nil;
end;
procedure WriteUnicodeStrings(AStream: TStream; AStrings: TStrings);
var
Enc: TMyUnicodeEncoding;
begin
Enc := TMyUnicodeEncoding.Create;
try
AStrings.SaveToStream(AStream, Enc);
finally
Enc.Free;
end;
end;
...
FileStream := TFileStream.Create('D:\MyFile.dat', fmCreate or fmOpenWrite or fmShareExclusive);
try
// Write some data to FileStream
WriteUnicodeStrings(FileStream, StringList1);
// Write some more data to FileStream
WriteUnicodeStrings(FileStream, StringList2);
finally
FileStream.Free;
end;
I wanna over delphi change hex adress 15 character,
I follow like this a way but I didnt get success,
BlockRead(F,arrChar,1); //read all to the buf
CloseFile(F); //close file
IMEI:=Form1.Edit1.Text; //get the number
Form1.Memo1.Lines.Add('new IMEI is'+IMEI); //output
for i:=524288 to 524288+15 do /
arrChar[i]:=IMEI[i-524287];
Do this with a file stream.
var
Stream: TFileStream;
....
Stream := TFileStream.Create(FileName, fmOpenWrite);
try
Stream.Position := $080000;
Stream.WriteBuffer(IMEI, SizeOf(IMEI));
finally
Stream.Free;
end;
I'm assuming that IMEI is an fixed length array of bytes of length 15 but your code attempts to write 16 bytes so it would appear that you are suffering from a degree of confusion.
In your code, your variable IMEI is a string. Which is not an array of bytes. Please don't make that classic mistake of regarding a string as an array of bytes.
You might declare an IMEI type like this:
type
TIMEI = array [0..14] of Byte;
Then you might write a function to populate such a variable from text:
function TextToIMEI(const Text: string): TIMEI;
var
ResultIndex, TextIndex: Integer;
C: Char;
begin
if Length(Text) <> Length(Result) then
raise SomeExceptionClass.Create(...);
TextIndex := low(Text);
for ResultIndex := low(Result) to high(Result) do
begin
C := Result[TextIndex];
if (C < '0') or (C > '9') then
raise SomeExceptionClass.Create(...);
Result[ResultIndex] := ord(C);
inc(TextIndex);
end;
end;
You might then combine this code with that above:
procedure WriteIMEItoFile(const FileName: string; FileOffset: Int64; const IMEI: TIMEI);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName, fmOpenWrite);
try
Stream.Position := FileOffset;
Stream.WriteBuffer(IMEI, SizeOf(IMEI));
finally
Stream.Free;
end;
end;
Call it like this:
WriteIMEItoFile(FileName, $080000, TextToIMEI(Form1.Edit1.Text));
Although it looks a bit odd that you are explicitly using the Form1 global variable. If that code executes in a method of TForm1 then you should use the implicit Self variable.
We have a library function that goes like this:
class function TFileUtils.ReadTextStream(const AStream: TStream): string;
var
StringStream: TStringStream;
begin
StringStream := TStringStream.Create('', TEncoding.Unicode);
try
// This is WRONG since CopyFrom might rewind the stream (see Remys comment)
StringStream.CopyFrom(AStream, AStream.Size - AStream.Position);
Result := StringStream.DataString;
finally
StringStream.Free;
end;
end;
When I check the string that is returned by the function the first Char is the (little-endian) BOM.
Why doesn't TStringStream ignore the BOM?
Is there a better way to do this? I don't need backwards compatibility with older Delphi versions, a working solution for XE2 would be fine.
The BOM has to be coming from the source TStream, as TStringStream does not write a BOM. If you want to ignore the BOM if it is present in the source, you have to do it manually before then copying the data, eg:
class function TFileUtils.ReadTextStream(const AStream: TStream): string;
var
StreamPos, StreamSize: Int64;
Buf: TBytes;
NumBytes: Integer;
Encoding: TEncoding;
begin
Result := '';
StreamPos := AStream.Position;
StreamSize := AStream.Size - StreamPos;
// Anything available to read?
if StreamSize < 1 then Exit;
// Read the first few bytes from the stream...
SetLength(Buf, 4);
NumBytes := AStream.Read(Buf[0], Length(Buf));
if NumBytes < 1 then Exit;
Inc(StreamPos, NumBytes);
Dec(StreamSize, NumBytes);
// Detect the BOM. If you know for a fact what the TStream data is encoded as,
// you can assign the Encoding variable to the appropriate TEncoding object and
// GetBufferEncoding() will check for that encoding's BOM only...
SetLength(Buf, NumBytes);
Encoding := nil;
Dec(NumBytes, TEncoding.GetBufferEncoding(Buf, Encoding));
// If any non-BOM bytes were read than rewind the stream back to that position...
if NumBytes > 0 then
begin
AStream.Seek(-NumBytes, soCurrent);
Dec(StreamPos, NumBytes);
Inc(StreamSize, NumBytes);
end else
begin
// Anything left to read after the BOM?
if StreamSize < 1 then Exit;
end;
// Now read and decode whatever is left in the stream...
StringStream := TStringStream.Create('', Encoding);
try
StringStream.CopyFrom(AStream, StreamSize);
Result := StringStream.DataString;
finally
StringStream.Free;
end;
end;
Apparently TStreamReader doesn't suffer from the same problem:
var
StreamReader: TStreamReader;
begin
StreamReader := TStreamReader.Create(AStream);
try
Result := StreamReader.ReadToEnd;
finally
StreamReader.Free;
end;
end;
TStringList also works (thanks whosrdaddy):
var
Strings: TStringList;
begin
Strings := TStringList.Create;
try
Strings.LoadFromStream(AStream);
Result := Strings.Text;
finally
Strings.Free;
end;
end;
I also measured both methods and TStreamReader seems to be about twice as fast.
for write something in a file i use for example this code:
procedure MyProc (... );
const
BufSize = 65535;
var
FileSrc, FileDst: TFileStream;
StreamRead: Cardinal;
InBuf, OutBuf: Array [0..bufsize] of byte;
begin
.....
FileSrc := TFileStream.Create (uFileSrc, fmOpenRead Or fmShareDenyWrite);
try
FileDst := TFileStream.Create (uFileTmp, fmCreate);
try
StreamRead := 0;
while ((iCounter < iFileSize) or (StreamRead = Cardinal(BufSize)))
begin
StreamRead := FileSrc.Read (InBuf, BufSize);
Inc (iCounter, StreamRead);
end;
finally
FileDst.Free;
end;
finally
FileSrc.Free;
end;
end;
And for I/O file i use a array of byte, and so is all ok, but when i use a string, for example declaring:
InBuf, OutBuf: string // in delphi xe2 = unicode string
then not work. In sense that file not write nothing. I have understood why, or just think to have understood it.
I think that problem maybe is why string contain just a pointer to memory and not static structure; correct?
In this case, there is some solution for solve it? In sense, is possible to do something for i can to write a file using string and not vector? Or i need necessary use a vector?
If possible, can i can to do ?
Thanks very much.
There are two issues with using strings. First of all you want to use RawByteString so that you ensure the use of byte sized character elements – a Unicode string has elements that are two bytes wide. And secondly you need to dereference the string which is really just a pointer.
But I wonder why you would prefer strings to the stack allocated byte array.
procedure MyProc (... );
const
BufSize = 65536;
var
FileSrc, FileDst: TFileStream;
StreamRead: Cardinal;
InBuf: RawByteString;
begin
.....
FileSrc := TFileStream.Create (uFileSrc, fmOpenRead Or fmShareDenyWrite);
try
FileDst := TFileStream.Create (uFileTmp, fmCreate);
try
SetLength(InBuf, BufSize);
StreamRead := 0;
while ((iCounter < iFileSize) or (StreamRead = Cardinal(BufSize)))
begin
StreamRead := FileSrc.Read (InBuf[1], BufSize);
Inc (iCounter, StreamRead);
end;
finally
FileDst.Free;
end;
finally
FileSrc.Free;
end;
end;
Note: Your previous code declared a buffer of 65536 bytes, but you only ever used 65535 of them. Probably not what you intended.
To use a string as a buffer (which I would not recommend), you'll have to use SetLength to allocate the internal buffer, and you'll have to pass InBuf[1] and OutBuf[1] as the data to read or write.
var
InBuf, OutBuf: AnsiString; // or TBytes
begin
SetLength(InBuf, BufSize);
SetLength(OutBuf, BufSize);
...
StreamRead := FileSrc.Read(InBuf[1], BufSize); // if TBytes, use InBuf[0]
// etc...
You can also use a TBytes, instead of an AnsiString. The usage remains the same.
But I actually see no advantage in dynamically allocating TBytes, AnsiStrings or RawByteStrings here. I'd rather do what you already do: use a stack based buffer. I would perhaps make it a little smaller in a multi-threaded environment.
Yes, you can save / load strings to / from stream, see the following example
var Len: Integer;
buf: string;
FData: TStream;
// save string to stream
// save the length of the string
Len := Length(buf);
FData.Write(Len, SizeOf(Len));
// save string itself
if(Len > 0)then FData.Write(buf[1], Len * sizeof(buf[1]));
// read string from stream
// read the length of the string
FData.Read(Len, SizeOf(Len));
if(Len > 0)then begin
// get memory for the string
SetLength(buf, Len);
// read string content
FData.Read(buf[1], Len * sizeof(buf[1]));
end else buf := '';
On a related note, to copy the contents from one TStream to another TStream, you could just use the TStream.CopyFrom() method instead:
procedure MyProc (... );
var
FileSrc, FileDst: TFileStream;
begin
...
FileSrc := TFileStream.Create (uFileSrc, fmOpenRead Or fmShareDenyWrite);
try
FileDst := TFileStream.Create (uFileTmp, fmCreate);
try
FileDst.CopyFrom(FileSrc, 0); // or FileDst.CopyFrom(FileSrc, iFileSize)
finally
FileDst.Free;
end;
finally
FileSrc.Free;
end;
...
end;
Which can be simplified by calling CopyFile() instead:
procedure MyProc (... );
begin
...
CopyFile(PChar(uFileSrc), PChar(uFileTmp), False);
...
end;
Either way, you don't have to worry about read/writing the file data manually at all!
I am trying to remotely read a binary (REG_BINARY) registry value, but I get nothing but junk back. Any ideas what is wrong with this code? I'm using Delphi 2010:
function GetBinaryRegistryData(ARootKey: HKEY; AKey, AValue, sMachine: string; var sResult: string): boolean;
var
MyReg: TRegistry;
RegDataType: TRegDataType;
DataSize, Len: integer;
sBinData: string;
bResult: Boolean;
begin
bResult := False;
MyReg := TRegistry.Create(KEY_QUERY_VALUE);
try
MyReg.RootKey := ARootKey;
if MyReg.RegistryConnect('\\' + sMachine) then
begin
if MyReg.KeyExists(AKey) then
begin
if MyReg.OpenKeyReadOnly(AKey) then
begin
try
RegDataType := MyReg.GetDataType(AValue);
if RegDataType = rdBinary then
begin
DataSize := MyReg.GetDataSize(AValue);
if DataSize > 0 then
begin
SetLength(sBinData, DataSize);
Len := MyReg.ReadBinaryData(AValue, PChar(sBinData)^, DataSize);
if Len <> DataSize then
raise Exception.Create(SysErrorMessage(ERROR_CANTREAD))
else
begin
sResult := sBinData;
bResult := True;
end;
end;
end;
except
MyReg.CloseKey;
end;
MyReg.CloseKey;
end;
end;
end;
finally
MyReg.Free;
end;
Result := bResult;
end;
And I call it like this:
GetBinaryRegistryData(
HKEY_LOCAL_MACHINE,
'\SOFTWARE\Microsoft\Windows NT\CurrentVersion',
'DigitalProductId', '192.168.100.105',
sProductId
);
WriteLn(sProductId);
The result I receive from the WriteLn on the console is:
ñ ♥ ???????????6Z ????1 ???????☺ ???♦ ??3 ? ??? ?
??
Assuming that you are already connected remotely, try using the GetDataAsString function
to read binary data from the registry.
sResult := MyReg.GetDataAsString(AValue);
You're using Delphi 2010, so all your characters are two bytes wide. When you set the length of your result string, you're allocating twice the amount of space you need. Then you call ReadBinaryData, and it fills half your buffer. There are two bytes of data in each character. Look at each byte separately, and you'll probably find that your data looks less garbage-like.
Don't use strings for storing arbitrary data. Use strings for storing text. To store arbitrary blobs of data, use TBytes, which is an array of bytes.