1*04e0dc4aSTimo Kreuzer //
2*04e0dc4aSTimo Kreuzer // gettemppath.cpp
3*04e0dc4aSTimo Kreuzer //
4*04e0dc4aSTimo Kreuzer // Copyright (c) Microsoft Corporation. All rights reserved.
5*04e0dc4aSTimo Kreuzer //
6*04e0dc4aSTimo Kreuzer // The __acrt_GetTempPath2A() function, which calls GetTempPathW and converts the string to multibyte.
7*04e0dc4aSTimo Kreuzer //
8*04e0dc4aSTimo Kreuzer #include <corecrt_internal.h>
9*04e0dc4aSTimo Kreuzer #include <io.h>
10*04e0dc4aSTimo Kreuzer
11*04e0dc4aSTimo Kreuzer
12*04e0dc4aSTimo Kreuzer
13*04e0dc4aSTimo Kreuzer // This function simply calls __acrt_GetTempPath2W() and converts the wide string to multibyte.
14*04e0dc4aSTimo Kreuzer // Note that GetTempPathA is not UTF-8 aware. This is because APIs using temporary paths
15*04e0dc4aSTimo Kreuzer // must not depend on the current locale setting and must use the ACP or OEMCP since
16*04e0dc4aSTimo Kreuzer // the returned data must be a static buffer and this behavior is guaranteed by MSDN documentation.
__acrt_GetTempPath2A(DWORD nBufferLength,LPSTR lpBuffer)17*04e0dc4aSTimo Kreuzer extern "C" DWORD __cdecl __acrt_GetTempPath2A(DWORD nBufferLength, LPSTR lpBuffer)
18*04e0dc4aSTimo Kreuzer {
19*04e0dc4aSTimo Kreuzer wchar_t wide_buffer[_MAX_PATH + 1] = {};
20*04e0dc4aSTimo Kreuzer
21*04e0dc4aSTimo Kreuzer DWORD const get_temp_path_result = __acrt_GetTempPath2W(_countof(wide_buffer), wide_buffer);
22*04e0dc4aSTimo Kreuzer if (get_temp_path_result == 0)
23*04e0dc4aSTimo Kreuzer {
24*04e0dc4aSTimo Kreuzer return 0;
25*04e0dc4aSTimo Kreuzer }
26*04e0dc4aSTimo Kreuzer
27*04e0dc4aSTimo Kreuzer bool const use_oem_code_page = !__acrt_AreFileApisANSI();
28*04e0dc4aSTimo Kreuzer int const code_page = use_oem_code_page ? CP_OEMCP : CP_ACP;
29*04e0dc4aSTimo Kreuzer #pragma warning(suppress:__WARNING_W2A_BEST_FIT) // 38021 Prefast recommends WC_NO_BEST_FIT_CHARS.
30*04e0dc4aSTimo Kreuzer int const wctmb_result = __acrt_WideCharToMultiByte(code_page, 0, wide_buffer, -1, lpBuffer, nBufferLength, nullptr, nullptr);
31*04e0dc4aSTimo Kreuzer if (wctmb_result == 0)
32*04e0dc4aSTimo Kreuzer {
33*04e0dc4aSTimo Kreuzer return 0;
34*04e0dc4aSTimo Kreuzer }
35*04e0dc4aSTimo Kreuzer
36*04e0dc4aSTimo Kreuzer // The return value of WideCharToMultiByte includes the null terminator; the
37*04e0dc4aSTimo Kreuzer // return value of GetTempPathA does not.
38*04e0dc4aSTimo Kreuzer return wctmb_result - 1;
39*04e0dc4aSTimo Kreuzer }
40