Patrick,

> What is the practical difference in the parameter, i.e. the difference
> between passing the dynamic variable directly and casting it to
> a string first?

Ought to be the same, according to the docs, but I have always used
StrPas().

However, there is an easier way to call GetTempPath (or any WinAPI call that
expects a PChar):

var
  sTmpPath, sTmpName: string;
begin
  // Let Delphi take care of memory allocation for you.
  SetLength (sTmpPath, MAX_PATH);
  SetLength (sTmpName, MAX_PATH);
  // PChar cast of string returns a pointer to the string's buffer (Null
terminated)
  if GetTempPath (MAX_PATH, PChar (sTmpPath)) <> 0 then
    if GetTempFileName, PChar(sTempPath), 'AD_', 0, PChar(sTmpName)) <> 0
then begin
      // [...]
      Compressor.CompressFile(sTmpName, CurrentFile, coLZH5);
    end;
end;


Regards,
Dennis.

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of Patrick Dunford
> Sent: Monday, 6 December 1999 02:01
> To: Multiple recipients of list delphi
> Subject: [DUG]: PChars and String Casts
>
>
> I have some code in an application that calls GetTempFileName to get a
> filename from Windows. To do this it uses the following
>
> var
>     TmpPath,TmpName:PChar;
> begin
>      GetMem(TmpPath,MAX_PATH+1);
>      if GetTempPath(MAX_PATH,TmpPath)<>0 then
>      begin
>           GetMem(TmpName,MAX_PATH+1);
>           if GetTempFileName(TmpPath,PChar('AD_'),0,TmpName)<>0 then
>           begin
> [...]
>
> Compressor.CompressFile(TmpName^),CurrentFile,coLZH5);
>           end;
>           FreeMem(TmpPath);
>      end;
>      FreeMem(TmpName);
>      end;
> end;
>
> But what happens is that the file gets created but comes out as a zero
> length file (never written).
>
> This works though:
>
> Compressor.CompressFile(String(TmpName),CurrentFile,coLZH5);
>
> What is the practical difference in the parameter, i.e. the difference
> between passing the dynamic variable directly and casting it to
> a string first?
>
> ============================================
> Patrick Dunford, Christchurch, NZ
> http://patrick.dunford.com/
> "These are they who have come out of the great tribulation;
> they have washed their robes and made them white in the
> blood of the Lamb" - Revelation 7:14
>
> --------------------------------------------------------------
> -------------
>     New Zealand Delphi Users group - Delphi List -
> [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
>

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to