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
Solved

Delphi2010: Question about get Windows temporary directory.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

861 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