Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
File Services
File Systems
File Management
 GetTempPath Function

  Switch on low bandwidth view
GetTempPath Function

Retrieves the path of the directory designated for temporary files.

Syntax

C++
DWORD WINAPI GetTempPath(
  __in   DWORD nBufferLength,
  __out  LPTSTR lpBuffer
);

Parameters

nBufferLength [in]

The size of the string buffer identified by lpBuffer, in TCHARs.

lpBuffer [out]

A pointer to a string buffer that receives the null-terminated string specifying the temporary file path. The returned string ends with a backslash, for example, C:\TEMP\.

Return Value

If the function succeeds, the return value is the length, in TCHARs, of the string copied to lpBuffer, not including the terminating null character. If the return value is greater than nBufferLength, the return value is the length, in TCHARs, of the buffer required to hold the path.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. The Windows directory.

Note that the function does not verify that the path exists.

Symbolic link behavior—If the path points to a symbolic link, the temp path name maintains any symbolic links.

Examples

For an example, see Creating and Using a Temporary File.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinBase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode and ANSI namesGetTempPathW (Unicode) and GetTempPathA (ANSI)

See Also

File Management Functions
GetTempFileName
Symbolic Links

Send comments about this topic to Microsoft

Build date: 7/9/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
C++ Wrapper for GetTempPath()      Cygon4 ... veparla   |   Edit   |   Show History
Here's a small C++ wrapper for GetTempPath(). edited to support ANSI and _UNICODE. Never call TCHAR methods with anything other than mutable TCHAR type. If you want to use the W or A function, call them directly.
/// <summary>Returns the path to the system's temporary directory</summary>
/// <returns>The full path to the system's temporary directory</returns>
#ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif
tstring GetTempPath()
{
DWORD result = ::GetTempPath(0, _T(""));
if(result == 0)
throw std::runtime_error("Could not get system temp path");

std::vector<TCHAR> tempPath(result + 1);
result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
if((result == 0) || (result >= tempPath.size()))
throw std::runtime_error("Could not get system temp path");

return tstring(
tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result)
);
}

Flag as ContentBug
Limit of Buffer Size for GetTempPath      6XGate ... Thomas Lee   |   Edit   |   Show History
[Note - this behavior does not occur with the latest versions of the OS as of Vista SP1/Windows Server 2008. If anyone has more information about when this condition occurs, please update this content.]

[Note - this post has been edited based on, and extended by, information in the following post]

Apparently due to the method used by GetTempPathA to translate ANSI strings to UNICODE, this function itself cannot be told that the buffer is greater than 32766 in narrow convention. Attempting to pass a larger value in nBufferLength will result in a failed RtlHeapFree call in ntdll.dll and subsequently cause your application to call DbgBreakPoint in debug compiles and simple close without warning in release compiles.

Example:
// Allocate a 32Ki character buffer, enough to hold even native NT paths.
LPTSTR tempPath = new TCHAR[32767];
::GetTempPath(32767, tempPath); // Will crash in RtlHeapFree
Flag as ContentBug
Extend of the GetTempPath bug. (probably only GetTempPathA)      6XGate   |   Edit   |   Show History
I have experienced this bug while developing software on Windows Vista gold (first release). It would seem that updating the SDK would not fix this since the call is made to a system library, kernel32.dll in this case, that is only updated during services packs and hotfixes.

I have only tested this when compiling an application in MultiByte/Ansi mode, thus it was calling GetTempPathA. This issue may not manifest in Unicode builds since no translation occurs. For the sake of portability, I currently treat even the Unicode version the same way (not specifying sizes greater than 32766) incase I need it to compile and run on 9x/Me systems.

If this has been fixed since Vista SP1 or Server 2008, a note probably should be made about this bug on versions of NT before these. The following link is to posts made by people who have experienced this bug and have made some document of it:

http://www.codeguru.com/forum/archive/index.php/t-401055.html

Hope this helps.
Flag as ContentBug
Requirements      Mark Twain   |   Edit   |   Show History
The requirements list Windows 2000 or better but the A and W versions work ok for me on Windows 95.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker