Signed-off-by: João Diogo Craveiro Ferreira devilj@outlook.pt --- Supersedes: 172514 V5: Remove superfluous SetLastError(), amend documentation, simplify code a bit. --- dlls/kernel32/kernel32.spec | 1 + dlls/kernel32/locale.c | 115 ++++++++++++++++++++++++++++++++++++ include/winnls.h | 1 + 3 files changed, 117 insertions(+)
diff --git a/dlls/kernel32/kernel32.spec b/dlls/kernel32/kernel32.spec index 9898bdec6e..c2dd727135 100644 --- a/dlls/kernel32/kernel32.spec +++ b/dlls/kernel32/kernel32.spec @@ -869,6 +869,7 @@ # @ stub GetUILanguageInfo @ stdcall -arch=x86_64 GetUmsCompletionListEvent(ptr ptr) # @ stub -arch=x86_64 GetUmsSystemThreadInformation +@ stdcall GetUserDefaultGeoName(ptr long) @ stdcall -import GetUserDefaultLCID() @ stdcall -import GetUserDefaultLangID() @ stdcall -import GetUserDefaultLocaleName(ptr long) diff --git a/dlls/kernel32/locale.c b/dlls/kernel32/locale.c index 5731f42cdf..31c20f365b 100644 --- a/dlls/kernel32/locale.c +++ b/dlls/kernel32/locale.c @@ -4141,6 +4141,121 @@ GEOID WINAPI GetUserGeoID(GEOCLASS geoclass) return ret; }
+/****************************************************************************** + * GetUserDefaultGeoName (KERNEL32.@) + * + * Retrieves the name of the user's geographic location. + * + * This name is a two-letter ISO 3166 country code + * or a three-digit UN M.49 code for anything other than countries (e.g. continents). + * + * If buffer_len is 0, this function will return the minimum length + * required for a buffer to hold the name. + * + * PARAMS + * buffer [O] Pointer to a buffer to hold the name. + * buffer_len [I] Length of the buffer, including null terminator, measured in WCHARs. + * + * RETURNS + * SUCCESS: Number of WCHARs written to the buffer -OR- minimum buffer length required. + * FAILURE: Zero. Call GetLastError() to determine the cause. + * + * NOTES + * On failure, GetLastError() will return one of the following values: + * - ERROR_INVALID_PARAMETER: one of the parameters was invalid. + * - ERROR_INSUFFICIENT_BUFFER: the specified buffer was too small to hold the name. + */ +int WINAPI GetUserDefaultGeoName(WCHAR *buffer, int buffer_len) +{ + int ret = 0, name_len = 0; + WCHAR fallback[] = {'0','0','1',0}; /* World */ + int fallback_len = ARRAY_SIZE(fallback); + + static const WCHAR geokeyname[] = {'G','e','o',0}; + HANDLE hkey, hsubkey; + KEY_VALUE_PARTIAL_INFORMATION kvi; + WCHAR valuename[] = {'N','a','m','e',0}; + UNICODE_STRING uvaluename; + DWORD reg_len; + + TRACE("(%p, %d)\n", buffer, buffer_len); + + if (buffer_len < 0 || (buffer_len && !buffer)) + { + SetLastError(ERROR_INVALID_PARAMETER); + return 0; + } + + RtlInitUnicodeString(&uvaluename, valuename); + + if ((hkey = create_registry_key()) && + (hsubkey = NLS_RegOpenKey(hkey, geokeyname)) && + NtQueryValueKey(hsubkey, &uvaluename, KeyValuePartialInformation, + &kvi, sizeof(kvi), ®_len) == STATUS_BUFFER_OVERFLOW) + /* If we don't get an overflow, it's the wrong value. */ + name_len = kvi.DataLength / sizeof(WCHAR); + + if (name_len) + { + if (!buffer_len) + ret = name_len; + else if (buffer_len < name_len) + { + SetLastError(ERROR_INSUFFICIENT_BUFFER); + ret = 0; + } + else if (buffer_len >= name_len) + { + KEY_VALUE_PARTIAL_INFORMATION *kviptr; + DWORD kvi_size = sizeof(*kviptr) + (name_len * sizeof(WCHAR)); + + if ((kviptr = HeapAlloc(GetProcessHeap(), 0, kvi_size))) + { + NTSTATUS status = NtQueryValueKey(hsubkey, &uvaluename, KeyValuePartialInformation, + kviptr, kvi_size, ®_len); + if (status == STATUS_SUCCESS) + { + memcpy(buffer, kviptr->Data, name_len * sizeof(WCHAR)); + ret = name_len; + } + else + { + ERR("Failure: status=0x%x, alloc'd kvi size = %d (needed %d), DataLength=%u\n", + status, kvi_size, reg_len, kviptr->DataLength); + ret = 0; + } + HeapFree(GetProcessHeap(), 0, kviptr); + } + else + { + ERR("We couldn't allocate enough memory.\n"); + ret = 0; + } + } + } + else + { + TRACE("We've fallen back to our hardcoded value.\n"); + + if (!buffer_len) + ret = fallback_len; + else if (buffer_len < fallback_len) + { + SetLastError(ERROR_INSUFFICIENT_BUFFER); + ret = 0; + } + else if (buffer_len >= fallback_len) + { + strcpyW(buffer, fallback); + ret = fallback_len; + } + } + + if (hsubkey) NtClose(hsubkey); + if (hkey) NtClose(hkey); + return ret; +} + /****************************************************************************** * set_geo_reg * diff --git a/include/winnls.h b/include/winnls.h index 4f84c64f81..6e60ceaa1d 100644 --- a/include/winnls.h +++ b/include/winnls.h @@ -935,6 +935,7 @@ WINBASEAPI INT WINAPI GetTimeFormatA(LCID,DWORD,const SYSTEMTIME*,LPCSTR WINBASEAPI INT WINAPI GetTimeFormatEx(LPCWSTR,DWORD,const SYSTEMTIME*,LPCWSTR,LPWSTR,INT); WINBASEAPI INT WINAPI GetTimeFormatW(LCID,DWORD,const SYSTEMTIME*,LPCWSTR,LPWSTR,INT); #define GetTimeFormat WINELIB_NAME_AW(GetTimeFormat) +WINBASEAPI int WINAPI GetUserDefaultGeoName(WCHAR*,int); WINBASEAPI LANGID WINAPI GetUserDefaultLangID(void); WINBASEAPI LCID WINAPI GetUserDefaultLCID(void); WINBASEAPI INT WINAPI GetUserDefaultLocaleName(LPWSTR,int);