Solved

Delphi2010: Question about get Windows temporary directory.

Posted on 2010-11-09
2
621 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
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now