ReactOS 0.4.16-dev-852-gcfcc8d8
get_environment_from_os.cpp File Reference
#include <corecrt_internal.h>
#include <corecrt_internal_traits.h>
#include <stdlib.h>
Include dependency graph for get_environment_from_os.cpp:

Go to the source code of this file.

Classes

struct  anonymous_namespace{get_environment_from_os.cpp}::environment_strings_traits
 

Namespaces

namespace  anonymous_namespace{get_environment_from_os.cpp}
 

Typedefs

typedef __crt_unique_handle_t< environment_strings_traits > anonymous_namespace{get_environment_from_os.cpp}::environment_strings_handle
 

Functions

static wchar_t const *__cdecl find_end_of_double_null_terminated_sequence (wchar_t const *const first) throw ()
 
wchar_t *__cdecl __dcrt_get_wide_environment_from_os () throw ()
 
char *__cdecl __dcrt_get_narrow_environment_from_os () throw ()
 

Function Documentation

◆ __dcrt_get_narrow_environment_from_os()

char *__cdecl __dcrt_get_narrow_environment_from_os ( void  )
throw (
)

Definition at line 75 of file get_environment_from_os.cpp.

76{
77 // Note that we call GetEnvironmentStringsW and convert to multibyte. The
78 // GetEnvironmentStringsA function returns the environment in the OEM code
79 // page, but we need the strings in ANSI.
81 if (!environment.is_valid())
82 return nullptr;
83
84 wchar_t const* const first = environment.get();
86
87 size_t const required_wide_count = last - first;
88#pragma warning(suppress:__WARNING_W2A_BEST_FIT) // 38021 Prefast recommends WC_NO_BEST_FIT_CHARS.
89 size_t const required_narrow_count = static_cast<size_t>(__acrt_WideCharToMultiByte(
90 CP_ACP,
91 0,
92 environment.get(),
93 static_cast<int>(required_wide_count),
94 nullptr,
95 0,
96 nullptr,
97 nullptr));
98
99 if (required_narrow_count == 0)
100 return nullptr;
101
102 __crt_unique_heap_ptr<char> buffer(_malloc_crt_t(char, required_narrow_count));
103 if (!buffer)
104 return nullptr;
105
106#pragma warning(suppress:__WARNING_W2A_BEST_FIT) // 38021 Prefast recommends WC_NO_BEST_FIT_CHARS.
107 int const conversion_result = __acrt_WideCharToMultiByte(
108 CP_ACP,
109 0,
110 environment.get(),
111 static_cast<int>(required_wide_count),
112 buffer.get(),
113 static_cast<int>(required_narrow_count),
114 nullptr,
115 nullptr);
116
117 if (conversion_result == 0)
118 return nullptr;
119
120 return buffer.detach();
121}
return __acrt_WideCharToMultiByte(code_page, 0, buffer.get(), -1, result_size !=0 ? result :nullptr, result_size, nullptr, nullptr)
#define CP_ACP
Definition: compat.h:109
static wchar_t const *__cdecl find_end_of_double_null_terminated_sequence(wchar_t const *const first)
GLuint buffer
Definition: glext.h:5915
const GLint * first
Definition: glext.h:5794
static UINT UINT last
Definition: font.c:45
__crt_unique_handle_t< environment_strings_traits > environment_strings_handle
LPWSTR WINAPI GetEnvironmentStringsW(void)
Definition: environ.c:344

◆ __dcrt_get_wide_environment_from_os()

wchar_t *__cdecl __dcrt_get_wide_environment_from_os ( void  )
throw (
)

Definition at line 52 of file get_environment_from_os.cpp.

53{
55 if (!environment)
56 return nullptr;
57
58 // Find the
59 wchar_t const* const first = environment.get();
61
62 size_t const required_count = last - first;
63
64 __crt_unique_heap_ptr<wchar_t> buffer(_malloc_crt_t(wchar_t, required_count));
65 if (!buffer)
66 return nullptr;
67
68 // Note that the multiplication here cannot overflow:
69 memcpy(buffer.get(), environment.get(), required_count * sizeof(wchar_t));
70 return buffer.detach();
71}
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878

◆ find_end_of_double_null_terminated_sequence()

static wchar_t const *__cdecl find_end_of_double_null_terminated_sequence ( wchar_t const *const  first)
throw (
)
static

Definition at line 40 of file get_environment_from_os.cpp.

41{
42 wchar_t const* last = first;
43 for (; *last != '\0'; last += wcslen(last) + 1)
44 {
45 }
46
47 return last + 1; // Return the pointer one-past-the-end of the double null terminator
48}
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)

Referenced by __dcrt_get_narrow_environment_from_os(), and __dcrt_get_wide_environment_from_os().