ReactOS 0.4.15-dev-7788-g1ad9096
util.cpp
Go to the documentation of this file.
1#include "precomp.h"
2#include <winver.h>
3
4typedef struct _LANGCODEPAGE
5{
9
12{
13 HRESULT hRet;
14
15 hRet = punk1->QueryInterface(IID_PPV_ARG(IUnknown, &punk1));
16 if (!SUCCEEDED(hRet))
17 return hRet;
18
19 hRet = punk2->QueryInterface(IID_PPV_ARG(IUnknown, &punk2));
20
21 punk1->Release();
22
23 if (!SUCCEEDED(hRet))
24 return hRet;
25
26 punk2->Release();
27
28 /* We're dealing with the same object if the IUnknown pointers are equal */
29 return (punk1 == punk2) ? S_OK : S_FALSE;
30}
31
34 IN LPCWSTR lpMenuName)
35{
36 HMENU hMenu, hSubMenu = NULL;
37
38 hMenu = LoadMenuW(hInstance, lpMenuName);
39 if (hMenu != NULL)
40 {
41 hSubMenu = GetSubMenu(hMenu, 0);
42 if ((hSubMenu != NULL) &&
43 !RemoveMenu(hMenu, 0, MF_BYPOSITION))
44 {
45 hSubMenu = NULL;
46 }
47
48 DestroyMenu(hMenu);
49 }
50
51 return hSubMenu;
52}
53
56 IN UINT uItem,
57 IN BOOL fByPosition)
58{
59 MENUITEMINFOW mii;
60
61 mii.cbSize = sizeof(mii);
62 mii.fMask = MIIM_SUBMENU;
63
64 if (GetMenuItemInfoW(hMenu, uItem, fByPosition, &mii))
65 {
66 return mii.hSubMenu;
67 }
68
69 return NULL;
70}
71
72BOOL
74 IN DWORD dwBufferSize)
75{
76 DWORD dwType;
78
79 /* Query the user name from the registry */
80 dwSize = (dwBufferSize * sizeof(WCHAR)) - 1;
82 L"Logon User Name",
83 0,
84 &dwType,
85 (LPBYTE)szBuffer,
86 &dwSize) == ERROR_SUCCESS &&
87 (dwSize / sizeof(WCHAR)) > 1 &&
88 szBuffer[0] != L'\0')
89 {
90 szBuffer[dwSize / sizeof(WCHAR)] = L'\0';
91 return TRUE;
92 }
93
94 /* Fall back to GetUserName() */
95 dwSize = dwBufferSize;
96 if (!GetUserNameW(szBuffer, &dwSize))
97 {
98 szBuffer[0] = L'\0';
99 return FALSE;
100 }
101
102 return TRUE;
103}
104
105BOOL
107 IN UINT uPosition,
108 IN UINT uFlags,
109 ...)
110{
111 va_list vl;
112 MENUITEMINFOW mii;
113 WCHAR szBuf[128];
114 WCHAR szBufFmt[128];
115
116 /* Find the menu item and read the formatting string */
117 mii.cbSize = sizeof(mii);
118 mii.fMask = MIIM_STRING;
119 mii.dwTypeData = szBufFmt;
120 mii.cch = _countof(szBufFmt);
121 if (GetMenuItemInfoW(hMenu, uPosition, uFlags, &mii))
122 {
123 /* Format the string */
124 va_start(vl, uFlags);
125 _vsntprintf(szBuf,
126 _countof(szBuf) - 1,
127 szBufFmt,
128 vl);
129 va_end(vl);
130 szBuf[_countof(szBuf) - 1] = L'\0';
131
132 /* Update the menu item */
133 mii.dwTypeData = szBuf;
134 if (SetMenuItemInfo(hMenu, uPosition, uFlags, &mii))
135 {
136 return TRUE;
137 }
138 }
139
140 return FALSE;
141}
142
143BOOL GetRegBool(IN LPCWSTR pszSubKey, IN LPCWSTR pszValueName, IN BOOL bDefaultValue)
144{
145 return SHRegGetBoolUSValueW(pszSubKey, pszValueName, FALSE, bDefaultValue);
146}
147
148BOOL SetRegDword(IN LPCWSTR pszSubKey, IN LPCWSTR pszValueName, IN DWORD dwValue)
149{
150 return (SHRegSetUSValueW(pszSubKey, pszValueName, REG_DWORD, &dwValue,
151 sizeof(dwValue), SHREGSET_FORCE_HKCU) == ERROR_SUCCESS);
152}
153
154#define REGKEY_ADVANCED L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced"
155
156BOOL GetAdvancedBool(IN LPCWSTR pszValueName, IN BOOL bDefaultValue)
157{
158 return GetRegBool(REGKEY_ADVANCED, pszValueName, bDefaultValue);
159}
160
161BOOL SetAdvancedDword(IN LPCWSTR pszValueName, IN DWORD dwValue)
162{
163 return SetRegDword(REGKEY_ADVANCED, pszValueName, dwValue);
164}
165
166BOOL
168 IN LPCWSTR szVersionInfo,
169 OUT LPWSTR szBuffer,
170 IN UINT cbBufLen)
171{
172 LPVOID lpData = NULL;
173 WCHAR szSubBlock[128];
174 WCHAR *lpszLocalBuf = NULL;
175 LANGID UserLangId;
176 PLANGCODEPAGE lpTranslate = NULL;
177 DWORD dwLen;
178 DWORD dwHandle;
179 UINT cbTranslate;
180 UINT cbLen;
181 BOOL bRet = FALSE;
182 UINT i;
183
184 dwLen = GetFileVersionInfoSizeW(szFileName, &dwHandle);
185
186 if (dwLen > 0)
187 {
188 lpData = HeapAlloc(hProcessHeap, 0, dwLen);
189
190 if (lpData != NULL)
191 {
192 if (GetFileVersionInfoW(szFileName,
193 0,
194 dwLen,
195 lpData) != 0)
196 {
197 UserLangId = GetUserDefaultLangID();
198
199 VerQueryValueW(lpData,
200 L"\\VarFileInfo\\Translation",
201 (LPVOID*)&lpTranslate,
202 &cbTranslate);
203
204 for (i = 0; i < cbTranslate / sizeof(LANGCODEPAGE); i++)
205 {
206 /* If the bottom eight bits of the language id's
207 match, use this version information (since this
208 means that the version information and the users
209 default language are the same). */
210 if (LOBYTE(lpTranslate[i].wLanguage) == LOBYTE(UserLangId))
211 {
212 wnsprintf(szSubBlock,
213 _countof(szSubBlock),
214 L"\\StringFileInfo\\%04X%04X\\%s",
215 lpTranslate[i].wLanguage,
216 lpTranslate[i].wCodePage,
217 szVersionInfo);
218
219 if (VerQueryValueW(lpData,
220 szSubBlock,
221 (LPVOID*)&lpszLocalBuf,
222 &cbLen) != 0)
223 {
224 wcsncpy(szBuffer, lpszLocalBuf, cbBufLen / sizeof(*szBuffer));
225
226 bRet = TRUE;
227 break;
228 }
229 }
230 }
231 }
232
233 HeapFree(hProcessHeap, 0, lpData);
234 lpData = NULL;
235 }
236 }
237
238 return bRet;
239}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
HKEY hkExplorer
Definition: explorer.cpp:26
HINSTANCE hInstance
Definition: charmap.c:19
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4132
BOOL WINAPI GetUserNameW(LPWSTR lpszName, LPDWORD lpSize)
Definition: misc.c:291
UINT uFlags
Definition: api.c:59
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
LONG WINAPI SHRegSetUSValueW(LPCWSTR pszSubKey, LPCWSTR pszValue, DWORD dwType, LPVOID pvData, DWORD cbData, DWORD dwFlags)
Definition: reg.c:673
BOOL WINAPI SHRegGetBoolUSValueW(LPCWSTR pszSubKey, LPCWSTR pszValue, BOOL fIgnoreHKCU, BOOL fDefault)
Definition: reg.c:770
BOOL WINAPI GetFileVersionInfoW(LPCWSTR filename, DWORD handle, DWORD datasize, LPVOID data)
Definition: version.c:845
BOOL WINAPI VerQueryValueW(LPCVOID pBlock, LPCWSTR lpSubBlock, LPVOID *lplpBuffer, PUINT puLen)
Definition: version.c:1049
DWORD WINAPI GetFileVersionInfoSizeW(LPCWSTR filename, LPDWORD handle)
Definition: version.c:611
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
ULONG Release()
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define LOBYTE(W)
Definition: jmemdos.c:487
HANDLE hProcessHeap
Definition: kbswitch.c:37
LANGID WINAPI GetUserDefaultLangID(void)
Definition: lang.c:744
USHORT LANGID
Definition: mui.h:9
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:63
unsigned int UINT
Definition: ndis.h:50
#define L(x)
Definition: ntvdm.h:50
#define REG_DWORD
Definition: sdbapi.c:596
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
#define SHREGSET_FORCE_HKCU
Definition: shlwapi.h:312
#define wnsprintf
Definition: shlwapi.h:1701
#define _countof(array)
Definition: sndvol32.h:68
WORD wCodePage
Definition: util.cpp:7
WORD wLanguage
Definition: util.cpp:6
LPWSTR dwTypeData
Definition: winuser.h:3269
unsigned char * LPBYTE
Definition: typedefs.h:53
#define IN
Definition: typedefs.h:39
#define OUT
Definition: typedefs.h:40
HMENU FindSubMenu(IN HMENU hMenu, IN UINT uItem, IN BOOL fByPosition)
Definition: util.cpp:55
BOOL FormatMenuString(IN HMENU hMenu, IN UINT uPosition, IN UINT uFlags,...)
Definition: util.cpp:106
struct _LANGCODEPAGE * PLANGCODEPAGE
BOOL GetVersionInfoString(IN LPCWSTR szFileName, IN LPCWSTR szVersionInfo, OUT LPWSTR szBuffer, IN UINT cbBufLen)
Definition: util.cpp:167
BOOL SetAdvancedDword(IN LPCWSTR pszValueName, IN DWORD dwValue)
Definition: util.cpp:161
BOOL GetAdvancedBool(IN LPCWSTR pszValueName, IN BOOL bDefaultValue)
Definition: util.cpp:156
HMENU LoadPopupMenu(IN HINSTANCE hInstance, IN LPCWSTR lpMenuName)
Definition: util.cpp:33
#define REGKEY_ADVANCED
Definition: util.cpp:154
BOOL GetCurrentLoggedOnUserName(OUT LPWSTR szBuffer, IN DWORD dwBufferSize)
Definition: util.cpp:73
BOOL GetRegBool(IN LPCWSTR pszSubKey, IN LPCWSTR pszValueName, IN BOOL bDefaultValue)
Definition: util.cpp:143
HRESULT IsSameObject(IN IUnknown *punk1, IN IUnknown *punk2)
Definition: util.cpp:11
BOOL SetRegDword(IN LPCWSTR pszSubKey, IN LPCWSTR pszValueName, IN DWORD dwValue)
Definition: util.cpp:148
struct _LANGCODEPAGE LANGCODEPAGE
#define S_FALSE
Definition: winerror.h:2357
#define MIIM_STRING
Definition: winuser.h:727
#define SetMenuItemInfo
Definition: winuser.h:5850
HMENU WINAPI GetSubMenu(_In_ HMENU, _In_ int)
#define MIIM_SUBMENU
Definition: winuser.h:723
#define MF_BYPOSITION
Definition: winuser.h:203
BOOL WINAPI RemoveMenu(_In_ HMENU, _In_ UINT, _In_ UINT)
BOOL WINAPI DestroyMenu(_In_ HMENU)
BOOL WINAPI GetMenuItemInfoW(_In_ HMENU, _In_ UINT, _In_ BOOL, _Inout_ LPMENUITEMINFOW)
HMENU WINAPI LoadMenuW(_In_opt_ HINSTANCE, _In_ LPCWSTR)
#define IID_PPV_ARG(Itype, ppType)
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define _vsntprintf
Definition: xmlstorage.h:203
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185