GetTempFileName

  UINT GetTempFileName(lpszPath, lpszPrefix, uUnique, lpszTempFile)    
  LPCTSTR lpszPath; /* name of dir in which to create temp file */
  LPCTSTR lpszPrefix; /* prefix of temp filename */
  UINT uUnique; /* number used to create temp filename */
  LPTSTR lpszTempFile; /* buffer that will receive temp filename */

The GetTempFileName function creates a temporary filename of the following form:

path\preuuuu.tmp

The following list describes the filename syntax:

Filename Component Description

path The path specified by the lpszPath parameter
pre The first three letters of the lpszPrefix string.
uuuu The hexadecimal value of uUnique.

Parameters

lpszPath

Points to a null-terminated pathname where the function will create the temporary filename. This string must consist of characters in the OEM-defined character set.

lpszPrefix

Points to a null-terminated string to be used as the temporary filename prefix. This string must consist of characters in the OEM-defined character set.

uUnique

If this parameter is nonzero, it will be appended to the temporary filename. If the parameter is zero, Windows uses the current system time to create a number to append to the filename.

lpszTempFile

Points to the buffer that will receive the temporary filename. This string consists of characters in the OEM-defined character set. This buffer should be at least MAX_PATH bytes in length to allow sufficient room for the path.

Return Value

The return value specifies a unique numeric value used in the temporary filename. If the uUnique parameter is nonzero, the return value specifies this same number.

If the function fails, the return value is zero. Use the GetLastError function to obtain extended error information.

Comments

Temporary files created with this function are not automatically deleted when Windows shuts down.

To avoid problems resulting from converting an OEM-character string to a Windows string, an application should call the CreateFile function to create the temporary file.

If the uUnique parameter is zero, GetTempFileName attempts to form a unique number based on the current system time. If a file with the resulting filename exists, the number is increased by one and the test for existence is repeated. This continues until a unique filename is found; GetTempFileName then creates a file by that name and closes it. No attempt is made to create and open the file when uUnique is nonzero.

The GetTempFileName function may be used as either a wide-character function (where text arguments must use Unicode) or an ANSI function (where text arguments must use characters from the Windows 3.x character set installed).

See Also

CreateFile, GetTempPath