Skip to content

Commit

Permalink
Get thread locale in pj strerror (#4018)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonidGoltsblat authored Jul 31, 2024
1 parent 9b79aef commit 1a82f90
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
16 changes: 15 additions & 1 deletion pjlib/include/pj/errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ PJ_BEGIN_DECL
/**
* Guidelines on error message length.
*/
#define PJ_ERR_MSG_SIZE 80
#ifndef PJ_ERR_MSG_SIZE
# define PJ_ERR_MSG_SIZE 80
#endif

/**
* Buffer for title string of #PJ_PERROR().
Expand All @@ -90,6 +92,18 @@ PJ_BEGIN_DECL
# define PJ_PERROR_TITLE_BUF_SIZE 120
#endif

/**
* On Windows XP and later, force the use of GetThreadLocale() in pj_strerror()
* to obtain the required locale and corresponding language for the platform
* error message string.
* Default value is set to "no" for compatibility reason, which means use
* "User default language".
*
* Default: 0 (no)
*/
#ifndef PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE
# define PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE 0
#endif

/**
* Get the last platform error/status, folded into pj_status_t.
Expand Down
13 changes: 11 additions & 2 deletions pjlib/src/pj/os_error_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,21 @@ int platform_strerror( pj_os_err_type os_errcode,


if (!len) {
DWORD dwLanguageId;
#if defined(PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE) && \
(PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE==1) && (WINVER >= 0x0500)
/* current thread language */
dwLanguageId = LANGIDFROMLCID(GetThreadLocale());
#else
/* LANG_USER_DEFAULT - User default language */
dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
#endif
#if PJ_NATIVE_STRING_IS_UNICODE
len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
os_errcode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
dwLanguageId,
wbuf,
PJ_ARRAY_SIZE(wbuf),
NULL);
Expand All @@ -190,7 +199,7 @@ int platform_strerror( pj_os_err_type os_errcode,
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
os_errcode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
dwLanguageId,
buf,
(int)bufsize,
NULL);
Expand Down
10 changes: 10 additions & 0 deletions pjlib/src/pjlib-test/errno.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,17 @@ int errno_test(void)
pj_set_os_error(rc);

/* Whole */
#if defined(PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE) && (PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE==1) && (WINVER >= 0x0500)
LCID lcid = GetThreadLocale();
/* set en_US thread locale to obtain "invalid" in errbuff */
BOOL res = SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
PJ_UNUSED_ARG(res);
#endif
pj_strerror(rc, errbuf, sizeof(errbuf));
#if defined(PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE) && (PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE==1) && (WINVER >= 0x0500)
/* restore thread locale now */
SetThreadLocale(lcid);
#endif
trim_newlines(errbuf);
PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA: '%s'", errbuf));
if (my_stristr(errbuf, "invalid") == NULL) {
Expand Down

0 comments on commit 1a82f90

Please sign in to comment.