Groups | Search | Server Info | Login | Register


Groups > comp.lang.pascal.delphi.misc > #742

Re: Strings and PChar (D4 Pro)

From "P E Schoen" <paul@pstech-inc.com>
Newsgroups comp.lang.pascal.delphi.misc
Subject Re: Strings and PChar (D4 Pro)
Date 2016-08-27 18:59 -0400
Organization A noiseless patient Spider
Message-ID <npt613$geb$1@dont-email.me> (permalink)
References <nprn44$mvb$1@dont-email.me> <1rco3b1rvcu6.1k5cho7qohwwc$.dlg@40tude.net> <nprp91$trt$1@dont-email.me> <e2dbqcFkv7fU2@mid.individual.net>

Show all headers | View raw


"Hans-Peter Diettrich"  wrote in message 
news:e2dbqcFkv7fU2@mid.individual.net...

> P E Schoen schrieb:

>>  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.

Yes, as described here:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

> 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.

I found another Delphi function that works with strings. I made the 
following test project:

procedure TfmTestMain.Button1Click(Sender: TObject);
var       curDir, curDir2, curDir3: String;
          PcurDir3: PChar;
begin
  curDir := GetCurrentDir;
  setLength(curDir2, MAX_PATH); //fsDirectory);
  GetCurrentDirectory(length(curDir2), PChar(curDir2) );
  curDir3 := StrPas(PcurDir3);
  setLength(curDir3, MAX_PATH);
  PcurDir3 := @curDir3[1];
  GetCurrentDirectory(length(curDir3), PcurDir3 );
  Label1.Caption := curDir;
  Label2.Caption := curDir2;
  Label3.Caption := curDir3;
end;

This works, but I get a warning that PcurDir3 might not be initialized. I 
think I understand that, since the curDir3 string has not been initialized. 
The StrPas function is not needed. I think I am beginning to understand the 
string and PChar concepts better.

Thanks,

Paul 

Back to comp.lang.pascal.delphi.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 05:39 -0400
  Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 06:03 -0400
    Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 06:28 -0400
    Re: Strings and PChar (D4 Pro) JJ <jj4public@vfemail.net> - 2016-08-27 18:00 +0700
      Re: Strings and PChar (D4 Pro) Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2016-08-27 13:41 +0200
  Re: Strings and PChar (D4 Pro) JJ <jj4public@vfemail.net> - 2016-08-27 17:05 +0700
    Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 06:15 -0400
      Re: Strings and PChar (D4 Pro) JJ <jj4public@vfemail.net> - 2016-08-27 18:09 +0700
      Re: Strings and PChar (D4 Pro) Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2016-08-27 13:12 +0200
        Re: Strings and PChar (D4 Pro) "P E Schoen" <paul@pstech-inc.com> - 2016-08-27 18:59 -0400
  Re: Strings and PChar (D4 Pro) Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2016-08-27 12:50 +0200

csiph-web