Delphi - Store WideStrings inside a program
In the past I used INI-Files to store unicode text, but now I need to store unicode text in the executable. How can I achieve this?
I want to store these letters:
If you definitely need to use Delphi 7 there are some variants:
Store strings in resources linked to executable file.
Store strings in big memo or same thing, located on global data module or any other visual or non-visual component and access it by index. It's possible because strings in Delphi resources stored in XML-encoded form. E.g. your symbols example
āčēūīšķļņžwill be stored as
Store XML-encoded or Base64-encoded strings in string constants inside your code.
For string conversion you can use EncdDecd.pas , xdom.pas or some functions of System.pas like UTF8Encode/UTF8Decode.
To display and edit Unicode strings in Delphi forms you can use special set of Unicode controls like TNT Unicode Controls or subclass original Delphi controls and do some other workarounds by yourself, like described in this excerpt from comments in TntControls.pas (part of TNT Unicode Controls):
Windows NT provides support for native Unicode windows. To add Unicode support to a TWinControl descendant, override CreateWindowHandle() and call CreateUnicodeHandle().
One major reason this works is because the VCL only uses the ANSI version of SendMessage() -- SendMessageA(). If you call SendMessageA() on a UNICODE window, Windows deals with the ANSI/UNICODE conversion automatically. So for example, if the VCL sends WM_SETTEXT to a window using SendMessageA, Windows actually expects a PAnsiChar even if the target window is a UNICODE window. So caling SendMessageA with PChars causes no problems.
A problem in the VCL has to do with the TControl.Perform() method. Perform() calls the window procedure directly and assumes an ANSI window. This is a problem if, for example, the VCL calls Perform(WM_SETTEXT, ...) passing in a PAnsiChar which eventually gets passed downto DefWindowProcW() which expects a PWideChar.
This is the reason for SubClassUnicodeControl(). This procedure will subclass the Windows WndProc, and the TWinControl.WindowProc pointer. It will determine if the message came from Windows or if the WindowProc was called directly. It will then call SendMessageA() for Windows to perform proper conversion on certain text messages.
Another problem has to do with TWinControl.DoKeyPress(). It is called from the WM_CHAR message. It casts the WideChar to an AnsiChar, and sends the resulting character to DefWindowProc. In order to avoid this, the DefWindowProc is subclassed as well. WindowProc will make a WM_CHAR message safe for ANSI handling code by converting the char code to #FF before passing it on. It stores the original WideChar in the .Unused field of TWMChar. The code #FF is converted back to the WideChar before passing onto DefWindowProc.
Delphi - Store WideStrings inside a program - delphi - android, In the past I used INI-Files to store Unicode text, but now I need to store unicode text in the same program. How can I achieve this result? Want to store these I'm using Delphi7 (non-unicode VCL), I need to store lots of WideStrings inside a TFileStream. I can't use TStringStream as the (wide)strings are mixed with binary data, the format is projected to
If you want to save the Unicode INI files then you might try the following code. The files are saved in UTF8 encoding.
Also you might take a look at this Unicode library where you can find a lot of helper functions.
uses IniFiles; function WideStringToUTF8(const Value: WideString): AnsiString; var BufferLen: Integer; begin Result := ''; if Value <> '' then begin BufferLen := WideCharToMultiByte(CP_UTF8, 0, PWideChar(Value), -1, nil, 0, nil, nil); SetLength(Result, BufferLen - 1); if BufferLen > 1 then WideCharToMultiByte(CP_UTF8, 0, PWideChar(Value), -1, PAnsiChar(Result), BufferLen - 1, nil, nil); end; end; function UTF8ToWideString(const Value: AnsiString): WideString; var BufferLen: integer; begin Result := ''; if Value <> '' then begin BufferLen := MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(Value), -1, nil, 0); SetLength(Result, BufferLen - 1); if BufferLen > 1 then MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(Value), -1, PWideChar(Result), BufferLen - 1); end; end; procedure TForm1.Button1Click(Sender: TObject); var IniFile: TIniFile; const UnicodeValue = WideString(#$0101#$010D#$0113#$016B#$012B#$0161); begin IniFile := TIniFile.Create('C:\test.ini'); try IniFile.WriteString('Section', 'Key', WideStringToUTF8(UnicodeValue)); IniFile.UpdateFile; finally IniFile.Free; end; end; procedure TForm1.Button2Click(Sender: TObject); var IniFile: TIniFile; UnicodeValue: WideString; begin IniFile := TIniFile.Create('C:\test.ini'); try UnicodeValue := UTF8ToWideString(IniFile.ReadString('Section', 'Key', 'Default')); MessageBoxW(Handle, PWideChar(UnicodeValue), 'Caption', 0); finally IniFile.Free; end; end;
with Delphi 2007 on 64-bit Windows 7 Enterprise SP 1
Delphi in a Nutshell, VWideChar; vtWideString: S := WideString(Args[I]. Short strings are not used much in Delphi programs, but if you know a string will have fewer The string does not store a size, but uses a zero-valued character to mark the end of the string. This is an example of Delphi managing storage on your behalf. WideString s can be assigned from other strings, from functions that return a string, and with concatenations as in the sample code. When assigning to a 'narrow' string, such as an AnsiString , the double to single byte conversion can have unpredictable results.
const MyString = WideString('Teksts latvie'#$0161'u valod'#$0101);
String Types, This topic describes the string data types available in the Delphi language. ANSI strings (AnsiString); Unicode strings (UnicodeString and WideString) using older 16-bit Delphi code or Turbo Pascal code with your current programs. its memory is a statically allocated 256 bytes; the first byte stores the length of the Delphi Basics - Free Delphi Source Code - Ultimate Programming Resource > Delphi Basics Snippets > How to store files inside an executable program posted 10 Dec 2009, 08:11 by Delphi Basics [ updated 10 Dec 2009, 08:12 ]
Simple, the idea is to find a non-visual component, which can store text and store your text there. Prefer that such component can also provide you an editor to edit the text in design time.
There is a component call
FormResource which can do this. I use
TUniScript. I believe there are other similar components. However, I did not find a usable component from the standard library.
Unicode in RAD Studio, A short string could contain UTF-8 data for a particular application, but this is not generally true. WideString is still appropriate for use in COM applications. For Delphi, Char and PChar types are now WideChar and PWideChar, respectively. TStrings: Store UnicodeStrings internally (remains declared as string). 0 Delphi - Store WideStrings inside a program Sep 24 '19-2 Delphi interface delegation implements cannot call directly Apr 4 '17. Badges (7)
WideString command, Length, Return the number of elements in an array or string Example code : Assign to two WideStrings and manipulate these. var string1 Show full unit code. Store more than just code in the application executable file and work with resources in Delphi. Embed a WAV, AVI, or MP3 file in a Delphi executable.
WideString Type - Delphi in a Nutshell [Book], Name WideString Type Syntax type WideString; Description WideString is similar to AnsiString, but instead of storing a string of AnsiChar characters, it stores a Delphi makes it easy to use this feature, called thread local storage, without worrying about the limitations imposed by Windows. Just declare a variable using threadvar instead of var. Ordinarily, Delphi creates a single instance of a unit-level variable and shares that instance among all threads.
1. Delphi Pascal - Delphi in a Nutshell [Book], To compile and link a Delphi program, you need a program source file and any If a unit has an associated form, the IDE also stores the form name in a comment. When you assign a wide string to a WideString variable, Delphi copies the The syntax to declare a pointer data type uses a caret (^). In the above code, iValue is an integer type variable and pIntValue is an integer type pointer. Since a pointer is nothing more than an address in memory, we must assign to it the location (address) of the value stored in the iValue integer variable.
- Working with Unicode characters in Delphi 7 is extremely boring. It would be a really, really good idea to upgrade to Delphi 2009+. Alternatively, you can switch to Free Pascal/Lazarus, which is free.
- I can't get Delphi 2009+ because Im 14 yrs old and dont have any money, C'mon people.
- @Robrok: The latter problem can probably be solved by telling Lazarus to save your source files as Unicode or UTF-8 files. Try the options dialog, or the editor right-click menu.
- @Robrok - ok, so you are back in Delphi now. Once again, the answer what Andreas provided is right. You are probably trying to display your Unicode constant in a certain non Unicode component or function. Note that Delphi 7 has no built-in component supporting Unicode and most of the functions are also ANSI (non Unicode). And that the source files are saved also as ANSI so you can't save the chars like
āčēūīšķļņždirectly in your source file, you need to use their ordinal values.
- Your question right now seems like a simple one. What difficulty are you having storing Unicode text in your program?
- Yes i am using TNT controls to display unicode text instead of MessageBoxW(Handle, PWideChar(MyString), 'Caption', 0);
- @Robrok - about how to store Unicode INI files. My answer was wrong. At least in D2007 I haven't found the overriden constructor with TEncoding. I'll try to update it.
TEncodingwas added in D2009
- @Robrok: Since I cannot ready your mind, I don't know what is wrong with it. (It does work in the sense that the string is stored as a wide string in memory...) Maybe you are trying to store Unicode characters in an ANSI source file? Try
WideString('Teksts latvie'#$0161'u valod'#$0101'). (You can obtain the character codes in many ways -- using
charmap, a decent text editor (like Rejbrand Text Editor), or -- easiest -- using Wolfram|Alpha.)
- I am using Delphi-7 and if i paste this: const MyString : WideString='āčēūīšķļņž' to Delphi unit it transforms to this: const MyString : WideString=('a-c(e-u-i-s(k,l,n,z(');
- @Robrok: Andreas is trying to help. It doesn't make that easy if you act like that. I don't remember if Delphi 7 supports it, but try to set the source file to Unicode or UTF8, if possible.
- How you are showing that string ? Have you tried e.g.
MessageBoxW(Handle, PWideChar(MyString), 'Caption', 0);? Don't forget you have no Unicode components in Delphi 7. And most of the functions like ShowMessage won't be Unicode too. You need to use only Unicode compatible things
- Yes i use this boring thing ^^^