Forum > Databases

SetString / AppendChunk behaviour

(1/2) > >>

RayoGlauco:
Hello,

Some time ago, I had written some code to write data to MS Access blob fields. It was working fine, but recently I have recompiled the program (Lazarus 2.2.2 on Windows 10) and now something is wrong.

I guess something in the internal handling of the string has changed, and now the bytes are not transferred correctly (it's very likely that the string contains the character #0 multiple times).

Does someone have a clue how to fix it?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  fieldname: WideString;  buffer: packed array [0..MaxBuffer - 1] of byte;  s: rawbytestring;  nbytes:integer;  st: TMemoryStream  ADOconn, ADOrset: Variant;begin  ...  ADOconn := CreateOleObject('ADODB.Connection');  ...  ADOrset := CreateOleObject('ADODB.Recordset');  ...  st.ReadBuffer(buffer, nbytes);  SetString(s, @buffer[0], nbytes);    ADOrset.Fields[fieldname].AppendChunk(s);     ...end;

Thaddy:
The buffer is probably way to smal  buffer: packed array [0..MaxBuffer - 1] of byte;
l, it should be  buffer: packed array [0..MaxBuffer*4 - 1] of byte; because unicodestring or widestring can take upto 4 bytes per unicodechar.
You can of course do that multiplication by 4 already in the declaration of MaxBuffer! But I suspect you haven't done that.
So:
Check if the buffer is large enough
maybe change widestring to UTF8String or UnicodeString, which work both like Pascal string types. Widestring doesn't.

RayoGlauco:
Thank you Taddy for your reply, but the code worked in previous versions of Lazarus.

I have verified that the string passed to AppendChunck is always converted to Widestring, in the 2.2.2 version of Lazarus.

And that's the problem: the content of the string is altered by Lazarus just before being passed to AppendChunck.

I want to use the latest version of Lazarus, so I'll find another way to get the job done.

RayoGlauco:
I have found a solution that works: put the data directly into a WideString.

The only thing is that the number of bytes that can be written will always be a multiple of 2.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  ...  cptr: ^char;  s: WideString;begin  ...  SetLength(s, nbytes div 2 + nbytes mod 2);  cptr:=@s[1];  for i:=0 to nbytes-1 do    cptr[i]:= char(buffer[i]);  ADOrset.Fields[fieldname].AppendChunk(s);  ...end; 

KodeZwerg:
My advise would be.... when you using "WideString" you also should use "WideChar" and multiply with SizeOf(WideChar) to have correct length.

Navigation

[0] Message Index

[#] Next page

Go to full version