Path: blob/master/test/jdk/java/util/Locale/data/deflocale.c
41152 views
/*1* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23*24*25* A simple tool to output all the installed locales on a Windows machine, and26* corresponding Java default locale/file.encoding using PrintDefaultLocale27*28* WARNING: This tool directly modifies the locale info in the Windows registry.29* It may not work with the Windows versions after Windows XP SP2. Also,30* if the test did not complete or was manually killed, you will need to reset31* the user default locale in the Control Panel manually. This executable has32* to be run with the "Administrator" privilege.33*34* Usage: "deflocale.exe <java launcher> PrintDefaultLocale35*36* How to compile: "cl -DUNICODE -D_UNICODE deflocale.c user32.lib advapi32.lib"37*/38#include <windows.h>39#include <stdio.h>40#include <memory.h>4142wchar_t* launcher;43wchar_t szBuffer[MAX_PATH];44LCID LCIDArray[1024];45int numLCIDs = 0;46BOOL isWin7orUp = FALSE;4748// for Windows 749BOOL (WINAPI * pfnEnumSystemLocalesEx)(LPVOID, DWORD, LPARAM, LPVOID);50BOOL (WINAPI * pfnEnumUILanguages)(LPVOID, DWORD, LPARAM);51LCID (WINAPI * pfnLocaleNameToLCID)(LPCWSTR, DWORD);52int (WINAPI * pfnLCIDToLocaleName)(LCID, LPWSTR, int, DWORD);53wchar_t* LocaleNamesArray[1024];54wchar_t* UILangNamesArray[1024];55int numLocaleNames = 0;56int numUILangNames = 0;5758void launchAndWait() {59STARTUPINFO si;60PROCESS_INFORMATION pi;6162ZeroMemory(&si, sizeof(si));63si.cb = sizeof(si);64ZeroMemory(&pi, sizeof(pi));65if (CreateProcess(NULL, launcher, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)==0) {66wprintf(L"CreateProcess failed with the error code: %x\n", GetLastError());67}6869WaitForSingleObject( pi.hProcess, INFINITE );70}7172void testLocale(int anLCID, wchar_t* pName) {73HKEY hk;7475if (pName != NULL && wcslen(pName) == 2) {76// ignore language only locale.77return;78}7980wprintf(L"\n");81wprintf(L"OS Locale (lcid: %x", anLCID);82if (pName != NULL) {83wprintf(L", name: %s", pName);84}85GetLocaleInfo(anLCID, LOCALE_SENGLANGUAGE, szBuffer, MAX_PATH);86wprintf(L"): %s (", szBuffer);87GetLocaleInfo(anLCID, LOCALE_SENGCOUNTRY, szBuffer, MAX_PATH);88wprintf(L"%s) - ", szBuffer);89GetLocaleInfo(anLCID, LOCALE_IDEFAULTANSICODEPAGE, szBuffer, MAX_PATH);90wprintf(L"%s\n", szBuffer);91fflush(0);9293if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Control Panel\\International", 0, KEY_READ | KEY_WRITE, &hk) == ERROR_SUCCESS) {94wchar_t originalLocale[16];95wchar_t testLocale[16];96wchar_t* pKeyName;97DWORD cb = sizeof(originalLocale);98DWORD cbTest;99100if (isWin7orUp) {101pKeyName = L"LocaleName";102wcscpy(testLocale, pName);103cbTest = wcslen(pName) * sizeof(wchar_t);104} else {105pKeyName = L"Locale";106swprintf(testLocale, L"%08x", anLCID);107cbTest = sizeof(wchar_t) * 8;108}109110RegQueryValueEx(hk, pKeyName, 0, 0, (LPBYTE)originalLocale, &cb);111RegSetValueEx(hk, pKeyName, 0, REG_SZ, (LPBYTE)testLocale, cbTest );112launchAndWait();113RegSetValueEx(hk, pKeyName, 0, REG_SZ, (LPBYTE)originalLocale, cb);114RegCloseKey(hk);115}116}117118void testUILang(wchar_t* pName) {119HKEY hk;120121wprintf(L"\n");122wprintf(L"OS UI Language (name: %s)\n", pName);123fflush(0);124125if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Control Panel\\Desktop", 0, KEY_READ | KEY_WRITE, &hk) == ERROR_SUCCESS) {126wchar_t originalUILang[16];127wchar_t testUILang[16];128wchar_t* pKeyName;129DWORD cb = sizeof(originalUILang);130DWORD cbTest = wcslen(pName) * sizeof(wchar_t);131132pKeyName = L"PreferredUILanguages";133wcscpy(testUILang, pName);134cbTest = wcslen(pName) * sizeof(wchar_t);135136RegQueryValueEx(hk, pKeyName, 0, 0, (LPBYTE)originalUILang, &cb);137RegSetValueEx(hk, pKeyName, 0, REG_SZ, (LPBYTE)testUILang, cbTest);138launchAndWait();139RegSetValueEx(hk, pKeyName, 0, REG_SZ, (LPBYTE)originalUILang, cb);140RegCloseKey(hk);141}142}143144BOOL CALLBACK EnumLocalesProc(LPWSTR lpLocaleStr) {145swscanf(lpLocaleStr, L"%08x", &LCIDArray[numLCIDs]);146numLCIDs ++;147148return TRUE;149}150151BOOL CALLBACK EnumLocalesProcEx(LPWSTR lpLocaleStr, DWORD flags, LPARAM lp) {152wchar_t* pName = malloc((wcslen(lpLocaleStr) + 1) * sizeof(wchar_t *));153wcscpy(pName, lpLocaleStr);154LocaleNamesArray[numLocaleNames] = pName;155numLocaleNames ++;156157return TRUE;158}159160BOOL CALLBACK EnumUILanguagesProc(LPWSTR lpUILangStr, LPARAM lp) {161wchar_t* pName = malloc((wcslen(lpUILangStr) + 1) * sizeof(wchar_t *));162wcscpy(pName, lpUILangStr);163UILangNamesArray[numUILangNames] = pName;164numUILangNames ++;165166return TRUE;167}168169int sortLCIDs(LCID * pLCID1, LCID * pLCID2) {170if (*pLCID1 < *pLCID2) return (-1);171if (*pLCID1 == *pLCID2) return 0;172return 1;173}174175int sortLocaleNames(wchar_t** ppName1, wchar_t** ppName2) {176LCID l1 = pfnLocaleNameToLCID(*ppName1, 0);177LCID l2 = pfnLocaleNameToLCID(*ppName2, 0);178return sortLCIDs(&l1, &l2);179}180181int main(int argc, char** argv) {182OSVERSIONINFO osvi;183LPWSTR commandline = GetCommandLine();184int i;185186osvi.dwOSVersionInfoSize = sizeof(osvi);187GetVersionEx(&osvi);188wprintf(L"# OSVersionInfo\n");189wprintf(L"# MajorVersion: %d\n", osvi.dwMajorVersion);190wprintf(L"# MinorVersion: %d\n", osvi.dwMinorVersion);191wprintf(L"# BuildNumber: %d\n", osvi.dwBuildNumber);192wprintf(L"# CSDVersion: %s\n", osvi.szCSDVersion);193wprintf(L"\n");194fflush(0);195196launcher = wcschr(commandline, L' ')+1;197while (*launcher == L' ') {198launcher++;199}200201isWin7orUp = (osvi.dwMajorVersion > 6) ||202(osvi.dwMajorVersion == 6 && osvi.dwMinorVersion >= 1);203204if (!isWin7orUp) {205// Enumerate locales206EnumSystemLocales(EnumLocalesProc, LCID_INSTALLED);207208// Sort LCIDs209qsort(LCIDArray, numLCIDs, sizeof(LCID), (void *)sortLCIDs);210} else {211// For Windows 7, use "LocaleName" registry key for the user locale212// as they seem to switch from "Locale".213HMODULE hmod = GetModuleHandle(L"kernel32");214*(FARPROC*)&pfnEnumSystemLocalesEx =215GetProcAddress(hmod, "EnumSystemLocalesEx");216*(FARPROC*)&pfnEnumUILanguages =217GetProcAddress(hmod, "EnumUILanguagesW");218*(FARPROC*)&pfnLocaleNameToLCID =219GetProcAddress(hmod, "LocaleNameToLCID");220*(FARPROC*)&pfnLCIDToLocaleName =221GetProcAddress(hmod, "LCIDToLocaleName");222if (pfnEnumSystemLocalesEx != NULL &&223pfnEnumUILanguages != NULL &&224pfnLocaleNameToLCID != NULL &&225pfnLCIDToLocaleName != NULL) {226// Enumerate locales227pfnEnumSystemLocalesEx(EnumLocalesProcEx,2281, // LOCALE_WINDOWS229(LPARAM)NULL, NULL);230// Enumerate UI Languages.231pfnEnumUILanguages(EnumUILanguagesProc,2320x8, // MUI_LANGUAGE_NAME233(LPARAM)NULL);234} else {235wprintf(L"Could not get needed entry points. quitting.\n");236exit(-1);237}238239// Sort LocaleNames240qsort(LocaleNamesArray, numLocaleNames,241sizeof(wchar_t*), (void *)sortLocaleNames);242qsort(UILangNamesArray, numUILangNames,243sizeof(wchar_t*), (void *)sortLocaleNames);244}245246// Execute enumeration of Java default locales247if (isWin7orUp) {248for (i = 0; i < numLocaleNames; i ++) {249testLocale(pfnLocaleNameToLCID(LocaleNamesArray[i], 0),250LocaleNamesArray[i]);251}252for (i = 0; i < numUILangNames; i ++) {253testUILang(UILangNamesArray[i]);254}255} else {256for (i = 0; i < numLCIDs; i ++) {257testLocale(LCIDArray[i], NULL);258}259}260}261262263