Skip to content

Commit 8297ca3

Browse files
committed
Use bcp47mrm IsWellFormedTag
1 parent 2e28dbb commit 8297ca3

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/ApplicationLanguages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace winrt::Microsoft::Windows::Globalization::implementation
4242

4343
void ApplicationLanguages::PrimaryLanguageOverride(hstring const& language)
4444
{
45-
bool isValidLanguageTag = IsWellFormedLanguageTag(language.c_str());
45+
bool isValidLanguageTag = _DefIsWellFormedTag(language.c_str());
4646

4747
THROW_HR_IF_MSG(E_INVALIDARG, !isValidLanguageTag, "The parameter is incorrect");
4848

dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/Helper.cpp

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,66 @@ HRESULT GetDefaultPriFile(winrt::hstring& filePath)
8787
return GetDefaultPriFileForCurentModule(isPackaged, filePath);
8888
}
8989

90-
bool IsWellFormedLanguageTag(const wchar_t* languageTag)
90+
// Taken from Platform.h/.cpp
91+
typedef bool(WINAPI* IsWellFormedTagFunc)(PCWSTR);
92+
HMODULE g_bcp47 = (HMODULE)(-1);
93+
94+
HMODULE LoadBcp47ModuleFrom(_In_ PCWSTR moduleName)
9195
{
92-
// Implementation taken from Platform.h, without accepting semi-colon, as it must be a single language tag
93-
if (languageTag == nullptr || *languageTag == L'\0')
96+
HMODULE module = LoadLibraryExW(moduleName, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
97+
if (module != nullptr)
9498
{
95-
return FALSE;
99+
// All BCP47 APIs we are interested are always exported from the same dll together. So we just pick anyone for probe.
100+
IsWellFormedTagFunc func = (IsWellFormedTagFunc)(GetProcAddress(module, "IsWellFormedTag"));
101+
if (func != nullptr)
102+
{
103+
return module;
104+
}
105+
FreeLibrary(module);
96106
}
97107

98-
BOOLEAN ok = TRUE;
99-
PCWSTR test = languageTag;
108+
return nullptr;
109+
}
100110

101-
while (ok && (*test != L'\0'))
111+
HMODULE LoadBcp47Module()
112+
{
113+
HMODULE module = LoadBcp47ModuleFrom(L"bcp47mrm.dll");
114+
if (module == nullptr)
102115
{
103-
// we accept letters, numbers and dash
104-
if (((*test >= L'0') && (*test <= L'9')) || ((*test >= L'a') && (*test <= L'z')) || ((*test >= L'A') && (*test <= L'Z')) ||
105-
(*test == L'-'))
106-
{
107-
test++;
108-
}
109-
else
110-
{
111-
ok = FALSE;
112-
}
116+
// In downlevel OS, the API is exposed by bcp47langs.dll.
117+
module = LoadBcp47ModuleFrom(L"bcp47langs.dll");
118+
}
119+
120+
return module;
121+
}
122+
123+
void InitializeBcp47Module()
124+
{
125+
HMODULE comparand = (HMODULE)(-1);
126+
if (InterlockedCompareExchangePointer(reinterpret_cast<PVOID*>(&g_bcp47), comparand, comparand) == comparand)
127+
{
128+
HMODULE module = LoadBcp47Module();
129+
InterlockedExchangePointer(reinterpret_cast<PVOID*>(&g_bcp47), module);
130+
}
131+
}
132+
133+
BOOLEAN _DefIsWellFormedTag(_In_ PCWSTR tag)
134+
{
135+
// Before new SDK is released, we need to use LoadLibrary/GetProcAddress
136+
InitializeBcp47Module();
137+
138+
if (g_bcp47 == nullptr)
139+
{
140+
// Didn't find an implementation. Just return TRUE.
141+
return TRUE;
142+
}
143+
144+
IsWellFormedTagFunc func = (IsWellFormedTagFunc)(GetProcAddress(g_bcp47, "IsWellFormedTag"));
145+
if (func != nullptr)
146+
{
147+
return func(tag);
113148
}
114149

115-
return ok && ((test - languageTag) >= 2);
150+
// Should not reach here.
151+
return TRUE;
116152
}

dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/Helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
#pragma once
55
bool IsResourceNotFound(HRESULT hr);
66
HRESULT GetDefaultPriFile(winrt::hstring& path);
7-
bool IsWellFormedLanguageTag(const wchar_t* languageTag);
7+
BOOLEAN _DefIsWellFormedTag(_In_ PCWSTR tag);

0 commit comments

Comments
 (0)