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 13:12:42 +0200 Lines: 35 Message-ID: References: <1rco3b1rvcu6.1k5cho7qohwwc$.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net ACeGdjYCpiVOMvesX9JgSA+V4LKi0G14N8wxM6cqcWeuC2Bpob Cancel-Lock: sha1:KoEzMTm2Gd6AMHiPuAUzqRo1qLQ= User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) In-Reply-To: Xref: csiph.com comp.lang.pascal.delphi.misc:740 P E Schoen schrieb: > "JJ" wrote in message news:1rco3b1rvcu6.1k5cho7qohwwc$.dlg@40tude.net... > > [snip] >> GetCurrentDirectory() arguments are: the buffer pointer, then the >> buffer length. Not the buffer length, then the buffer pointer. > > From the Delphi help: > > DWORD GetCurrentDirectory( > > DWORD nBufferLength, // size, in characters, of directory buffer > LPTSTR lpBuffer // address of buffer for current directory > ); According to MSDN I'd think that the length comes first. > Also, perhaps I could use the return value of the function with a buffer > size of zero. It would return the size needed, and then I could use > alloc() to create the memory space to use. I just don't know what is the > "best" way to handle functions like this, and I may have been using the > PChar() cast improperly. Most API functions, dealing with variable amount of data, return the required size when called with a Nil pointer. MSDN also claims that the *required* size is returned by GetCurrentDirectory, if the string doesn't fit into the buffer, but I'd not rely on that. At least the call succeeded if the returned (actual) size is lower than the allocated size. All non-empty dynamic Strings are automatically terminated by an invisible and not counted zero char. I.e. you can/should SetLength(str, charcount) to extend or shrink a dynamic String to the required length, with a zero byte at its end as expected by API (C) funtions. DoDi