Solved

Delphi2010: Question about get Windows temporary directory.

Posted on 2010-11-09
2
628 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

729 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