Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Hans-Peter Diettrich Newsgroups: comp.lang.pascal.delphi.misc Subject: Re: Strings and PChar (D4 Pro) Date: Sat, 27 Aug 2016 12:50:09 +0200 Lines: 25 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 3I9lkZ0H4p7bfyElIHBZKwhhRobXqeD4Pgmc4Nt4DD4e6/rSu+ Cancel-Lock: sha1:GhLESJeJviusNlwotRhED9yvGcg= User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) In-Reply-To: Xref: csiph.com comp.lang.pascal.delphi.misc:739 P E Schoen schrieb: > This is probably a very basic question, but I may have been improperly > casting a string to PChar in some cases. For instance, I got the > following code to work as shown: > > ==================================================== > procedure TfmOrtDatabase.btBackupClick(Sender: TObject); > var udfn: OrtData.TUserDataFile; > S, dbFileName, saveFileName, dirName: String; > // currDir: String; > currDir: array[0..50] of Char; > begin > if Application.MessageBox( 'Backup Database Files?', > 'Backup', MB_YESNO ) = ID_YES then begin //1 > // currDir := '01234567890123456789012345678901234567890123456789'; > // SetLength(currDir,50); > GetCurrentDirectory(sizeof(currDir)+1, @currDir); Unlike a fixed size ShortString, a dynamic string variable is a pointer to a dynamically allocated char array. I'd guess that sizeof(currDir) will return 4, the size of a pointer. Try Length instead, which returns the current (allocated) size of the string. Also use PCHAR(currDir) to get a pointer to the payload, not to the pointer variable. DoDi