ReactOS 0.4.16-dev-853-g88d9285
gettemppath.cpp
Go to the documentation of this file.
1//
2// gettemppath.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// The __acrt_GetTempPath2A() function, which calls GetTempPathW and converts the string to multibyte.
7//
8#include <corecrt_internal.h>
9#include <io.h>
10
11
12
13// This function simply calls __acrt_GetTempPath2W() and converts the wide string to multibyte.
14// Note that GetTempPathA is not UTF-8 aware. This is because APIs using temporary paths
15// must not depend on the current locale setting and must use the ACP or OEMCP since
16// the returned data must be a static buffer and this behavior is guaranteed by MSDN documentation.
18{
19 wchar_t wide_buffer[_MAX_PATH + 1] = {};
20
21 DWORD const get_temp_path_result = __acrt_GetTempPath2W(_countof(wide_buffer), wide_buffer);
22 if (get_temp_path_result == 0)
23 {
24 return 0;
25 }
26
27 bool const use_oem_code_page = !__acrt_AreFileApisANSI();
28 int const code_page = use_oem_code_page ? CP_OEMCP : CP_ACP;
29#pragma warning(suppress:__WARNING_W2A_BEST_FIT) // 38021 Prefast recommends WC_NO_BEST_FIT_CHARS.
30 int const wctmb_result = __acrt_WideCharToMultiByte(code_page, 0, wide_buffer, -1, lpBuffer, nBufferLength, nullptr, nullptr);
31 if (wctmb_result == 0)
32 {
33 return 0;
34 }
35
36 // The return value of WideCharToMultiByte includes the null terminator; the
37 // return value of GetTempPathA does not.
38 return wctmb_result - 1;
39}
return __acrt_WideCharToMultiByte(code_page, 0, buffer.get(), -1, result_size !=0 ? result :nullptr, result_size, nullptr, nullptr)
#define __cdecl
Definition: accygwin.h:79
int WINAPI __acrt_GetTempPath2W(_In_ DWORD nBufferLength, _Out_writes_to_opt_(BufferLength, return+1) LPWSTR lpBuffer)
_Out_opt_ UINT * code_page
BOOL WINAPI __acrt_AreFileApisANSI(void)
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
#define CP_ACP
Definition: compat.h:109
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD __cdecl __acrt_GetTempPath2A(DWORD nBufferLength, LPSTR lpBuffer)
Definition: gettemppath.cpp:17
#define _MAX_PATH
Definition: utility.h:77
#define _countof(array)
Definition: sndvol32.h:70
_In_ LPCSTR _In_opt_ LPCSTR _In_ DWORD nBufferLength
Definition: winbase.h:3098
#define CP_OEMCP
Definition: winnls.h:233
char * LPSTR
Definition: xmlstorage.h:182