-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalization.c
executable file
·122 lines (100 loc) · 3.58 KB
/
localization.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2009 Heiko Hund <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <windows.h>
#include <tchar.h>
static const LANGID defaultLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL);
static HRSRC
FindLangResource(HINSTANCE instance, PTSTR resType, PTSTR resId, LANGID langId)
{
HRSRC res;
/* try to find the resource in requested language */
res = FindResourceEx(instance, resType, resId, langId);
if (res)
return res;
/* try to find the resource in the default language */
res = FindResourceEx(instance, resType, resId, defaultLangId);
if (res)
return res;
/* try to find the resource in any language */
return FindResource(instance, resId, resType);
}
int
LoadLangString(HINSTANCE instance, UINT stringId, LANGID langId, PTSTR buffer, int bufferSize)
{
PWCH entry;
PTSTR resBlockId = MAKEINTRESOURCE(stringId / 16 + 1);
int resIndex = stringId & 15;
/* find resource block for string */
HRSRC res = FindLangResource(instance, RT_STRING, resBlockId, langId);
if (res == NULL)
return 0;
/* get pointer to first entry in resource block */
entry = (PWCH) LoadResource(instance, res);
if (entry == NULL)
return 0;
/* search for string in block */
for (int i = 0; i < 16; i++)
{
/* string found, copy it */
if (i == resIndex && *entry)
{
int maxLen = (*entry < bufferSize ? *entry : bufferSize);
#ifdef _UNICODE
wcsncpy(buffer, entry + 1, maxLen);
#else
WideCharToMultiByte(CP_ACP, 0, entry + 1, *entry, buffer, maxLen, "?", NULL);
#endif
buffer[bufferSize - 1] = 0;
return _tcslen(buffer);
}
/* string does not exist */
if (i == resIndex)
break;
/* skip over this entry */
entry += ((*entry) + 1);
}
return 0;
}
HICON
LoadLangIcon(HINSTANCE instance, PTSTR iconId, LANGID langId)
{
/* find group icon resource */
HRSRC res = FindLangResource(instance, RT_GROUP_ICON, iconId, langId);
if (res == NULL)
return NULL;
HGLOBAL resInfo = LoadResource(instance, res);
if (resInfo == NULL)
return NULL;
int id = LookupIconIdFromDirectory(resInfo, TRUE);
if (id == 0)
return NULL;
/* find the actual icon */
res = FindLangResource(instance, RT_ICON, MAKEINTRESOURCE(id), langId);
if (res == NULL)
return NULL;
resInfo = LoadResource(instance, res);
if (resInfo == NULL)
return NULL;
DWORD resSize = SizeofResource(instance, res);
if (resSize == 0)
return NULL;
return CreateIconFromResource(resInfo, resSize, TRUE, 0x30000);
}