52d5c2dd468f4f18f8d8ad6b6ca80fc00cdbb117
[reactos.git] / base / applications / msconfig_new / utils.c
1 /*
2 * PROJECT: ReactOS Applications
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/msconfig_new/utils.c
5 * PURPOSE: Memory Management, Resources, ... Utility Functions
6 * COPYRIGHT: Copyright 2011-2012 Hermes BELUSCA - MAITO <hermes.belusca@sfr.fr>
7 */
8
9 #include "precomp.h"
10 #include "utils.h"
11 #include "stringutils.h"
12
13 // HANDLE g_hHeap = GetProcessHeap();
14 #define g_hHeap GetProcessHeap()
15
16 #if 0
17 VOID
18 MemInit(IN HANDLE Heap)
19 {
20 /* Save the heap handle */
21 g_hHeap = Heap;
22 }
23 #endif
24
25 BOOL
26 MemFree(IN PVOID lpMem)
27 {
28 /* Free memory back into the heap */
29 return HeapFree(g_hHeap, 0, lpMem);
30 }
31
32 PVOID
33 MemAlloc(IN DWORD dwFlags,
34 IN DWORD dwBytes)
35 {
36 /* Allocate memory from the heap */
37 return HeapAlloc(g_hHeap, dwFlags, dwBytes);
38 }
39
40 LPWSTR
41 FormatDateTime(IN LPSYSTEMTIME pDateTime)
42 {
43 LPWSTR lpszDateTime = NULL;
44 int iDateBufSize, iTimeBufSize;
45
46 if (pDateTime == NULL) return NULL;
47
48 iDateBufSize = GetDateFormatW(LOCALE_USER_DEFAULT,
49 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE,
50 pDateTime,
51 NULL,
52 NULL,
53 0);
54 iTimeBufSize = GetTimeFormatW(LOCALE_USER_DEFAULT,
55 0,
56 pDateTime,
57 NULL,
58 NULL,
59 0);
60
61 if ( (iDateBufSize > 0) && (iTimeBufSize > 0) )
62 {
63 lpszDateTime = (LPWSTR)MemAlloc(0, (iDateBufSize + iTimeBufSize) * sizeof(WCHAR));
64
65 GetDateFormatW(LOCALE_USER_DEFAULT,
66 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE,
67 pDateTime,
68 NULL,
69 lpszDateTime,
70 iDateBufSize);
71 if (iDateBufSize > 0) lpszDateTime[iDateBufSize-1] = L' ';
72 GetTimeFormatW(LOCALE_USER_DEFAULT,
73 0,
74 pDateTime,
75 NULL,
76 lpszDateTime + iDateBufSize,
77 iTimeBufSize);
78 }
79
80 return lpszDateTime;
81 }
82
83 VOID
84 FreeDateTime(IN LPWSTR lpszDateTime)
85 {
86 if (lpszDateTime)
87 MemFree(lpszDateTime);
88 }
89
90 LPWSTR
91 LoadResourceStringEx(IN HINSTANCE hInstance,
92 IN UINT uID,
93 OUT size_t* pSize OPTIONAL)
94 {
95 LPWSTR lpszDestBuf = NULL, lpszResourceString = NULL;
96 size_t iStrSize = 0;
97
98 // When passing a zero-length buffer size, LoadString(...) returns a
99 // read-only pointer buffer to the program's resource string.
100 iStrSize = LoadStringW(hInstance, uID, (LPWSTR)&lpszResourceString, 0);
101
102 if ( lpszResourceString && ( (lpszDestBuf = (LPWSTR)MemAlloc(0, (iStrSize + 1) * sizeof(WCHAR))) != NULL ) )
103 {
104 wcsncpy(lpszDestBuf, lpszResourceString, iStrSize);
105 lpszDestBuf[iStrSize] = L'\0'; // NULL-terminate the string
106
107 if (pSize)
108 *pSize = iStrSize + 1;
109 }
110 else
111 {
112 if (pSize)
113 *pSize = 0;
114 }
115
116 return lpszDestBuf;
117 }
118
119 LPWSTR
120 LoadConditionalResourceStringEx(IN HINSTANCE hInstance,
121 IN BOOL bCondition,
122 IN UINT uIDifTrue,
123 IN UINT uIDifFalse,
124 IN size_t* pSize OPTIONAL)
125 {
126 return LoadResourceStringEx(hInstance,
127 (bCondition ? uIDifTrue : uIDifFalse),
128 pSize);
129 }
130
131 DWORD
132 RunCommand(IN LPCWSTR lpszCommand,
133 IN LPCWSTR lpszParameters,
134 IN INT nShowCmd)
135 {
136 DWORD dwRes;
137
138 DWORD dwNumOfChars;
139 LPWSTR lpszExpandedCommand;
140
141 dwNumOfChars = ExpandEnvironmentStringsW(lpszCommand, NULL, 0);
142 lpszExpandedCommand = (LPWSTR)MemAlloc(0, dwNumOfChars * sizeof(WCHAR));
143 ExpandEnvironmentStringsW(lpszCommand, lpszExpandedCommand, dwNumOfChars);
144
145 dwRes = (DWORD)ShellExecuteW(NULL, NULL /* and not L"open" !! */,
146 lpszExpandedCommand,
147 lpszParameters,
148 NULL, nShowCmd);
149 MemFree(lpszExpandedCommand);
150
151 return dwRes;
152 }
153
154
155 //////////////////// The following comes from MSDN samples ///////////////////
156 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162826(v=vs.85).aspx
157 //
158
159 //
160 // ClipOrCenterRectToMonitor
161 //
162 // The most common problem apps have when running on a
163 // multimonitor system is that they "clip" or "pin" windows
164 // based on the SM_CXSCREEN and SM_CYSCREEN system metrics.
165 // Because of app compatibility reasons these system metrics
166 // return the size of the primary monitor.
167 //
168 // This shows how you use the multi-monitor functions
169 // to do the same thing.
170 //
171 VOID ClipOrCenterRectToMonitor(LPRECT prc, UINT flags)
172 {
173 HMONITOR hMonitor;
174 MONITORINFO mi;
175 RECT rc;
176 int w = prc->right - prc->left;
177 int h = prc->bottom - prc->top;
178
179 //
180 // Get the nearest monitor to the passed rect.
181 //
182 hMonitor = MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST);
183
184 //
185 // Get the work area or entire monitor rect.
186 //
187 mi.cbSize = sizeof(mi);
188 GetMonitorInfo(hMonitor, &mi);
189
190 if (flags & MONITOR_WORKAREA)
191 rc = mi.rcWork;
192 else
193 rc = mi.rcMonitor;
194
195 //
196 // Center or clip the passed rect to the monitor rect.
197 //
198 if (flags & MONITOR_CENTER)
199 {
200 prc->left = rc.left + (rc.right - rc.left - w) / 2;
201 prc->top = rc.top + (rc.bottom - rc.top - h) / 2;
202 prc->right = prc->left + w;
203 prc->bottom = prc->top + h;
204 }
205 else
206 {
207 prc->left = max(rc.left, min(rc.right-w, prc->left));
208 prc->top = max(rc.top, min(rc.bottom-h, prc->top));
209 prc->right = prc->left + w;
210 prc->bottom = prc->top + h;
211 }
212 }
213
214 VOID ClipOrCenterWindowToMonitor(HWND hWnd, UINT flags)
215 {
216 RECT rc;
217 GetWindowRect(hWnd, &rc);
218 ClipOrCenterRectToMonitor(&rc, flags);
219 SetWindowPos(hWnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
220 }
221 ////////////////////////////////////////////////////////////////////////////////
222
223
224 BOOL IsWindowsOS(VOID)
225 {
226 BOOL bIsWindowsOS = FALSE;
227
228 OSVERSIONINFOW osvi = {0};
229 osvi.dwOSVersionInfoSize = sizeof(osvi);
230
231 if (!GetVersionExW(&osvi))
232 return FALSE;
233
234 if (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT)
235 return FALSE;
236
237 /* ReactOS reports as Windows NT 5.2 */
238
239 if ( (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion >= 2) ||
240 (osvi.dwMajorVersion > 5) )
241 {
242 HKEY hKey = NULL;
243
244 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
245 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
246 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
247 {
248 LONG ret;
249 DWORD dwType = 0, dwBufSize = 0;
250
251 ret = RegQueryValueExW(hKey, L"ProductName", NULL, &dwType, NULL, &dwBufSize);
252 if (ret == ERROR_SUCCESS && dwType == REG_SZ)
253 {
254 LPTSTR lpszProductName = (LPTSTR)MemAlloc(0, dwBufSize);
255 RegQueryValueExW(hKey, L"ProductName", NULL, &dwType, (LPBYTE)lpszProductName, &dwBufSize);
256
257 bIsWindowsOS = (FindSubStrI(lpszProductName, _T("Windows")) != NULL);
258
259 MemFree(lpszProductName);
260 }
261
262 RegCloseKey(hKey);
263 }
264 }
265 else
266 {
267 bIsWindowsOS = TRUE;
268 }
269
270 return bIsWindowsOS;
271 }
272
273 BOOL IsPreVistaOSVersion(VOID)
274 {
275 OSVERSIONINFOW osvi = {0};
276 osvi.dwOSVersionInfoSize = sizeof(osvi);
277
278 if (!GetVersionExW(&osvi))
279 return FALSE;
280
281 /* Vista+-class OSes are NT >= 6 */
282 return ( (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) ? (osvi.dwMajorVersion < 6) : FALSE );
283 }
284
285 LPWSTR
286 GetExecutableVendor(IN LPCWSTR lpszFilename)
287 {
288 LPWSTR lpszVendor = NULL;
289 DWORD dwHandle = 0;
290 DWORD dwLen;
291
292 LPVOID lpData;
293
294 LPVOID pvData = NULL;
295 UINT BufLen = 0;
296 WORD wCodePage = 0, wLangID = 0;
297 LPWSTR lpszStrFileInfo = NULL;
298
299 LPWSTR lpszData = NULL;
300
301 if (lpszFilename == NULL) return NULL;
302
303 dwLen = GetFileVersionInfoSizeW(lpszFilename, &dwHandle);
304 if (dwLen == 0) return NULL;
305
306 lpData = MemAlloc(0, dwLen);
307 if (!lpData) return NULL;
308
309 GetFileVersionInfoW(lpszFilename, dwHandle, dwLen, lpData);
310
311 if (VerQueryValueW(lpData, L"\\VarFileInfo\\Translation", &pvData, &BufLen))
312 {
313 wCodePage = LOWORD(*(DWORD*)pvData);
314 wLangID = HIWORD(*(DWORD*)pvData);
315
316 lpszStrFileInfo = FormatString(L"StringFileInfo\\%04X%04X\\CompanyName",
317 wCodePage,
318 wLangID);
319 }
320
321 VerQueryValueW(lpData, lpszStrFileInfo, (LPVOID*)&lpszData, &BufLen);
322 if (lpszData)
323 {
324 lpszVendor = (LPWSTR)MemAlloc(0, BufLen * sizeof(WCHAR));
325 if (lpszVendor)
326 wcscpy(lpszVendor, lpszData);
327 }
328 else
329 {
330 lpszVendor = NULL;
331 }
332
333 MemFree(lpszStrFileInfo);
334 MemFree(lpData);
335
336 return lpszVendor;
337 }