Solved

Delphi2010: Question about get Windows temporary directory.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

735 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