1 | /* $Id: RTLocaleQueryUserCountryCode-win.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTLocaleQueryUserCountryCode, ring-3, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/win/windows.h>
|
---|
42 |
|
---|
43 | #include <iprt/locale.h>
|
---|
44 | #include "internal/iprt.h"
|
---|
45 |
|
---|
46 | #include <iprt/ctype.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 |
|
---|
50 | #include "internal-r3-win.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Structures and Typedefs *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | typedef GEOID (WINAPI *PFNGETUSERGEOID)(GEOCLASS);
|
---|
57 | typedef INT (WINAPI *PFNGETGEOINFOW)(GEOID,GEOTYPE,LPWSTR,INT,LANGID);
|
---|
58 |
|
---|
59 |
|
---|
60 | /*********************************************************************************************************************************
|
---|
61 | * Global Variables *
|
---|
62 | *********************************************************************************************************************************/
|
---|
63 | /** Pointer to GetUserGeoID. */
|
---|
64 | static PFNGETUSERGEOID g_pfnGetUserGeoID = NULL;
|
---|
65 | /** Pointer to GetGeoInfoW. */
|
---|
66 | static PFNGETGEOINFOW g_pfnGetGeoInfoW = NULL;
|
---|
67 | /** Set if we've tried to resolve the APIs. */
|
---|
68 | static bool volatile g_fResolvedApis = false;
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(int) RTLocaleQueryUserCountryCode(char pszCountryCode[3])
|
---|
72 | {
|
---|
73 | /*
|
---|
74 | * Get API pointers.
|
---|
75 | */
|
---|
76 | PFNGETUSERGEOID pfnGetUserGeoID;
|
---|
77 | PFNGETGEOINFOW pfnGetGeoInfoW;
|
---|
78 | if (g_fResolvedApis)
|
---|
79 | {
|
---|
80 | pfnGetUserGeoID = g_pfnGetUserGeoID;
|
---|
81 | pfnGetGeoInfoW = g_pfnGetGeoInfoW;
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | pfnGetUserGeoID = (PFNGETUSERGEOID)GetProcAddress(g_hModKernel32, "GetUserGeoID");
|
---|
86 | pfnGetGeoInfoW = (PFNGETGEOINFOW)GetProcAddress(g_hModKernel32, "GetGeoInfoW");
|
---|
87 | g_pfnGetUserGeoID = pfnGetUserGeoID;
|
---|
88 | g_pfnGetGeoInfoW = pfnGetGeoInfoW;
|
---|
89 | g_fResolvedApis = true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | int rc;
|
---|
93 | if ( pfnGetGeoInfoW
|
---|
94 | && pfnGetUserGeoID)
|
---|
95 | {
|
---|
96 | /*
|
---|
97 | * Call the API and retrieve the two letter ISO country code.
|
---|
98 | */
|
---|
99 | GEOID idGeo = pfnGetUserGeoID(GEOCLASS_NATION);
|
---|
100 | if (idGeo != GEOID_NOT_AVAILABLE)
|
---|
101 | {
|
---|
102 | RTUTF16 wszName[16];
|
---|
103 | RT_ZERO(wszName);
|
---|
104 | DWORD cwcReturned = pfnGetGeoInfoW(idGeo, GEO_ISO2, wszName, RT_ELEMENTS(wszName), LOCALE_NEUTRAL);
|
---|
105 | if ( cwcReturned >= 2
|
---|
106 | && cwcReturned <= 3
|
---|
107 | && wszName[2] == '\0'
|
---|
108 | && wszName[1] != '\0'
|
---|
109 | && RT_C_IS_ALPHA(wszName[1])
|
---|
110 | && wszName[0] != '\0'
|
---|
111 | && RT_C_IS_ALPHA(wszName[0]) )
|
---|
112 | {
|
---|
113 | pszCountryCode[0] = RT_C_TO_UPPER(wszName[0]);
|
---|
114 | pszCountryCode[1] = RT_C_TO_UPPER(wszName[1]);
|
---|
115 | pszCountryCode[2] = '\0';
|
---|
116 | return VINF_SUCCESS;
|
---|
117 | }
|
---|
118 | AssertMsgFailed(("cwcReturned=%d err=%u wszName='%.16ls'\n", cwcReturned, GetLastError(), wszName));
|
---|
119 | }
|
---|
120 | rc = VERR_NOT_AVAILABLE;
|
---|
121 | }
|
---|
122 | else
|
---|
123 | rc = VERR_NOT_SUPPORTED;
|
---|
124 | pszCountryCode[0] = 'Z';
|
---|
125 | pszCountryCode[1] = 'Z';
|
---|
126 | pszCountryCode[2] = '\0';
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|