?
Solved

Delphi2010: Question about get Windows temporary directory.

Posted on 2010-11-09
2
Medium Priority
?
629 Views
Last Modified: 2013-11-23
hi all,

i have some codes to get windows temporary directory, it works fine under delphi2010. But there are some lines i don't understand how do they work.

 i write some questions in comments there, It would be nice if someone could answer it.

thanks,

wantime






procedure TForm1.Button1Click(Sender: TObject);
var 
    mTemPath: ARRAY [0 .. MAX_PATH] OF WideChar;
    mTemFile: String;
begin
    GetTempPath(Length(mTemPath), mTemPath);
    mTemFile := format('%sTestFile.txt', [mTemPath]);
    ShowMessage(mTemFile);
end;

Comments:

  - mTemPath is an array of WideChar with size 261
  - mTemFile is a string to store windows temporary 
    direcotry             
  - Method 'GetTempPath' is from windows.pas.
  function GetTempPath(nBufferLength: DWORD; lpBuffer: PWideChar): DWORD; stdcall;
  Question1: in GetTempPath there are two Parameters, the second one should has tpye PWideChar, but in my code it is mTemPath which has type Array of WideChar. what does here happened?
  - Method 'format' is from SysUtils.pas.
function Format(const Format: string; const Args: array of TVarRec): string; overload;
Question2: as mTemPath is an array, does it mean the the Parameter '[mTemPath]' in format is a 2d array? what Format method need is an array of TVarRec, but [mTemPath] seems like an array of PWideChar array, what is going on here?

Open in new window

procedure TForm1.Button1Click(Sender: TObject);
var 
    mTemPath: ARRAY [0 .. MAX_PATH] OF WideChar;
    mTemFile: String;
begin
    GetTempPath(Length(mTemPath), mTemPath);
    mTemFile := format('%sTestFile.txt', [mTemPath]);
    ShowMessage(mTemFile);
end;

Comments:

  - mTemPath is an array of WideChar with size 261
  - mTemFile is a string to store windows temporary 
    direcotry             
  - Method 'GetTempPath' is from windows.pas.
  function GetTempPath(nBufferLength: DWORD; lpBuffer: PWideChar): DWORD; stdcall;
  Question1: in GetTempPath there are two Parameters, the second one should has tpye PWideChar, but in my code it is mTemPath which has type Array of WideChar. what does here happened?
  - Method 'format' is from SysUtils.pas.
function Format(const Format: string; const Args: array of TVarRec): string; overload;
Question2: as mTemPath is an array, does it mean the the Parameter '[mTemPath]' in format is a 2d array? what Format method need is an array of TVarRec, but [mTemPath] seems like an array of PWideChar array, what is going on here?

Open in new window

0
Comment
Question by:wantime
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 total points
ID: 34092266
 Question1: in GetTempPath there are two Parameters, the second one should has tpye PWideChar, but in my code it is mTemPath which has type Array of WideChar. what does here happened?

An array is also a pointer to WideChars.  The array variable points to the first WideChar, and is compatible with PWideChar
Using an array conveniently also blocks out and allocates the required memory, and arrays in functions are cleared from the stack when function exits.

Question2:   http://www.delphibasics.co.uk/RTL.asp?Name=Format
System.TVarRec is used where prior versions of delphi uses const.
TVarRec represents a constant value that can be used in the format specifiers.
It is array of TVarRec because you can format multiple things inside a string.
TVarRec can take a lot of types, including string, int, float etc, including PWideChar / array of WideChar
0
 

Author Comment

by:wantime
ID: 34092311
thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question