Recent

Author Topic: Krakozyabry - fear  (Read 10800 times)

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Krakozyabry - fear
« Reply #15 on: April 19, 2017, 10:51:14 pm »
Don't use Pointer(Str)^. Use Str[1] instead.
Disagree
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$RANGECHECKS ON}
  4.  
  5. procedure Dummy(const Buffer);
  6. begin
  7. end;
  8.  
  9. var
  10.   S: string = '';
  11. begin
  12.   Dummy(Pointer(S)^); // Working fine
  13. {$IFNDEF Skip_Cyrax_Hint}
  14.   Dummy(S[1]); // Runtime error 201
  15. {$ENDIF}
  16. end.

Erhm, that should be @S[1]:-[

Фролов

  • New Member
  • *
  • Posts: 15
Re: Krakozyabry - fear
« Reply #16 on: April 19, 2017, 10:53:46 pm »
Each successive character is equal to the NULL character, alternating the desired one
Strange. This code is working fine:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses Windows, SysUtils;
  4.  
  5. function SaveToFile(const FileName: string; const Str: UnicodeString): Boolean;
  6. var
  7.   HFile: THandle;
  8.   Unused: DWORD;
  9. begin
  10.   Result := False;
  11.   HFile := FileCreate(FileName);
  12.   if HFile <> INVALID_HANDLE_VALUE then
  13.   try
  14.     Result := WriteFile(HFile, Pointer(Str)^, Length(Str) * SizeOf(WideChar), {%H-}Unused, nil);
  15.   finally
  16.     FileClose(HFile);
  17.   end;
  18. end;
  19.  
  20. var
  21.   S: string = 'Строка по русски';
  22. begin
  23.   SetMultiByteConversionCodePage(CP_UTF8);
  24.   SaveToFile('c:\temp\ИмяФайла.txt', S);
  25. end.

Does not work(!

Фролов

  • New Member
  • *
  • Posts: 15
Re: Krakozyabry - fear
« Reply #17 on: April 19, 2017, 11:06:33 pm »
All attempts had no effect, it's strange!  MultiByteToWideChar is powerless

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Krakozyabry - fear
« Reply #18 on: April 19, 2017, 11:10:17 pm »
Does not work(!
Work!
If you want to make the file understandable for text editors, it is necessary to add Byte Order Mark:
Code: Pascal  [Select][+][-]
  1. function SaveToFile(const FileName: string; const Str: UnicodeString): Boolean;
  2. const
  3.   CByteOrderMarkUTF16LittleEndian: array [1..2] of Byte = ($FF, $FE);
  4. var
  5.   HFile: THandle;
  6.   Unused: DWORD;
  7. begin
  8.   Result := False;
  9.   HFile := FileCreate(FileName);
  10.   if HFile <> INVALID_HANDLE_VALUE then
  11.   try
  12.     Result := WriteFile(HFile, CByteOrderMarkUTF16LittleEndian, Length(CByteOrderMarkUTF16LittleEndian), {%H-}Unused, nil) and
  13.       WriteFile(HFile, Pointer(Str)^, Length(Str) * SizeOf(WideChar), {%H-}Unused, nil);
  14.   finally
  15.     FileClose(HFile);
  16.   end;
  17. end;

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Krakozyabry - fear
« Reply #19 on: April 19, 2017, 11:25:13 pm »
Erhm, that should be @S[1]:-[
No, @ is not needed here, because untyped const or var want variable, not address.
The problem lies elsewhere. When using indexes like S[1] program tests the boundaries, which wrong for empty strings, but when casting to a pointer the check excluded.

Фролов

  • New Member
  • *
  • Posts: 15
Re: Krakozyabry - fear
« Reply #20 on: April 19, 2017, 11:35:55 pm »
Does not work(!
Work!
If you want to make the file understandable for text editors, it is necessary to add Byte Order Mark:
Code: Pascal  [Select][+][-]
  1. function SaveToFile(const FileName: string; const Str: UnicodeString): Boolean;
  2. const
  3.   CByteOrderMarkUTF16LittleEndian: array [1..2] of Byte = ($FF, $FE);
  4. var
  5.   HFile: THandle;
  6.   Unused: DWORD;
  7. begin
  8.   Result := False;
  9.   HFile := FileCreate(FileName);
  10.   if HFile <> INVALID_HANDLE_VALUE then
  11.   try
  12.     Result := WriteFile(HFile, CByteOrderMarkUTF16LittleEndian, Length(CByteOrderMarkUTF16LittleEndian), {%H-}Unused, nil) and
  13.       WriteFile(HFile, Pointer(Str)^, Length(Str) * SizeOf(WideChar), {%H-}Unused, nil);
  14.   finally
  15.     FileClose(HFile);
  16.   end;
  17. end;

Great, many thanks!  :) :) :)


Фролов

  • New Member
  • *
  • Posts: 15
Re: Krakozyabry - fear
« Reply #21 on: April 20, 2017, 12:17:17 am »
Хочу всем сказать спасибо!
I want to, say thank you all! You have all been most helpful in my. Special ASerge

 

TinyPortal © 2005-2018