Reactos
at master 39 lines 1.5 kB view raw
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. 17extern "C" DWORD __cdecl __acrt_GetTempPath2A(DWORD nBufferLength, LPSTR lpBuffer) 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}