SetString / AppendChunk behaviour lazarus / 16-Databases ---------------------------------------------------------------------- RayoGlauco · 2022-08-26 08:47 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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2022-08-26 10:58 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 · 2022-08-26 15:45 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 · 2022-08-26 16:39 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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2022-08-26 16:49 My advise would be.... when you using "WideString" you also should use "WideChar" and multiply with SizeOf(WideChar) to have correct length. ---------------------------------------------------------------------- RayoGlauco · 2022-08-26 17:13 Thanks KodeZwerg, but my problem is that I want to write a byte stream to a MSaccess blob field. It needs to be done by using an AnsiString, but Lazarus kindly converts my AnsiString to a WideString, altering the original content. That's why I copy the bytes directly into a WideString. This is my last solution: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  ...  s: WideString;begin  ...  // nbytes must be even  if nbytes mod 2 = 1 then  begin    buffer[nbytes]:= 0;  // set extra byte to 0    inc(nbytes);         // make nbytes even  end;  SetLength(s, nbytes div 2);  move(buffer[0], s[1], nbytes);  ADOrset.Fields[fieldname].AppendChunk(s);  ...end;