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
MemFree(IN PVOID lpMem)26 MemFree(IN PVOID lpMem)
27 {
28     /* Free memory back into the heap */
29     return HeapFree(g_hHeap, 0, lpMem);
30 }
31 
32 PVOID
MemAlloc(IN DWORD dwFlags,IN SIZE_T dwBytes)33 MemAlloc(IN DWORD dwFlags,
34          IN SIZE_T dwBytes)
35 {
36     /* Allocate memory from the heap */
37     return HeapAlloc(g_hHeap, dwFlags, dwBytes);
38 }
39 
40 LPWSTR
FormatDateTime(IN LPSYSTEMTIME pDateTime)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
FreeDateTime(IN LPWSTR lpszDateTime)84 FreeDateTime(IN LPWSTR lpszDateTime)
85 {
86     if (lpszDateTime)
87         MemFree(lpszDateTime);
88 }
89 
90 LPWSTR
LoadResourceStringEx(IN HINSTANCE hInstance,IN UINT uID,OUT size_t * pSize OPTIONAL)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
LoadConditionalResourceStringEx(IN HINSTANCE hInstance,IN BOOL bCondition,IN UINT uIDifTrue,IN UINT uIDifFalse,IN size_t * pSize OPTIONAL)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
RunCommand(IN LPCWSTR lpszCommand,IN LPCWSTR lpszParameters,IN INT nShowCmd)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_PTR)ShellExecuteW(NULL,
146                                      NULL /* and not L"open" !! */,
147                                      lpszExpandedCommand,
148                                      lpszParameters,
149                                      NULL,
150                                      nShowCmd);
151     MemFree(lpszExpandedCommand);
152 
153     return dwRes;
154 }
155 
156 
157 ////////////////////  The following comes from MSDN samples  ///////////////////
158 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162826(v=vs.85).aspx
159 //
160 
161 //
162 //  ClipOrCenterRectToMonitor
163 //
164 //  The most common problem apps have when running on a
165 //  multimonitor system is that they "clip" or "pin" windows
166 //  based on the SM_CXSCREEN and SM_CYSCREEN system metrics.
167 //  Because of app compatibility reasons these system metrics
168 //  return the size of the primary monitor.
169 //
170 //  This shows how you use the multi-monitor functions
171 //  to do the same thing.
172 //
ClipOrCenterRectToMonitor(LPRECT prc,UINT flags)173 VOID ClipOrCenterRectToMonitor(LPRECT prc, UINT flags)
174 {
175     HMONITOR    hMonitor;
176     MONITORINFO mi;
177     RECT        rc;
178     int         w = prc->right  - prc->left;
179     int         h = prc->bottom - prc->top;
180 
181     //
182     // Get the nearest monitor to the passed rect.
183     //
184     hMonitor = MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST);
185 
186     //
187     // Get the work area or entire monitor rect.
188     //
189     mi.cbSize = sizeof(mi);
190     GetMonitorInfo(hMonitor, &mi);
191 
192     if (flags & MONITOR_WORKAREA)
193         rc = mi.rcWork;
194     else
195         rc = mi.rcMonitor;
196 
197     //
198     // Center or clip the passed rect to the monitor rect.
199     //
200     if (flags & MONITOR_CENTER)
201     {
202         prc->left   = rc.left + (rc.right  - rc.left - w) / 2;
203         prc->top    = rc.top  + (rc.bottom - rc.top  - h) / 2;
204         prc->right  = prc->left + w;
205         prc->bottom = prc->top  + h;
206     }
207     else
208     {
209         prc->left   = max(rc.left, min(rc.right-w,  prc->left));
210         prc->top    = max(rc.top,  min(rc.bottom-h, prc->top));
211         prc->right  = prc->left + w;
212         prc->bottom = prc->top  + h;
213     }
214 }
215 
ClipOrCenterWindowToMonitor(HWND hWnd,UINT flags)216 VOID ClipOrCenterWindowToMonitor(HWND hWnd, UINT flags)
217 {
218     RECT rc;
219     GetWindowRect(hWnd, &rc);
220     ClipOrCenterRectToMonitor(&rc, flags);
221     SetWindowPos(hWnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
222 }
223 ////////////////////////////////////////////////////////////////////////////////
224 
225 
IsWindowsOS(VOID)226 BOOL IsWindowsOS(VOID)
227 {
228     BOOL bIsWindowsOS = FALSE;
229 
230     OSVERSIONINFOW osvi = {0};
231     osvi.dwOSVersionInfoSize = sizeof(osvi);
232 
233     if (!GetVersionExW(&osvi))
234         return FALSE;
235 
236     if (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT)
237         return FALSE;
238 
239     /* ReactOS reports as Windows NT 5.2 */
240 
241     if ( (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion >= 2) ||
242          (osvi.dwMajorVersion  > 5) )
243     {
244         HKEY hKey = NULL;
245 
246         if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
247                           L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
248                           0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
249         {
250             LONG ret;
251             DWORD dwType = 0, dwBufSize = 0;
252 
253             ret = RegQueryValueExW(hKey, L"ProductName", NULL, &dwType, NULL, &dwBufSize);
254             if (ret == ERROR_SUCCESS && dwType == REG_SZ)
255             {
256                 LPTSTR lpszProductName = (LPTSTR)MemAlloc(0, dwBufSize);
257                 RegQueryValueExW(hKey, L"ProductName", NULL, &dwType, (LPBYTE)lpszProductName, &dwBufSize);
258 
259                 bIsWindowsOS = (FindSubStrI(lpszProductName, _T("Windows")) != NULL);
260 
261                 MemFree(lpszProductName);
262             }
263 
264             RegCloseKey(hKey);
265         }
266     }
267     else
268     {
269         bIsWindowsOS = TRUE;
270     }
271 
272     return bIsWindowsOS;
273 }
274 
IsPreVistaOSVersion(VOID)275 BOOL IsPreVistaOSVersion(VOID)
276 {
277     OSVERSIONINFOW osvi = {0};
278     osvi.dwOSVersionInfoSize = sizeof(osvi);
279 
280     if (!GetVersionExW(&osvi))
281         return FALSE;
282 
283     /* Vista+-class OSes are NT >= 6 */
284     return ( (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) ? (osvi.dwMajorVersion < 6) : FALSE );
285 }
286 
287 LPWSTR
GetExecutableVendor(IN LPCWSTR lpszFilename)288 GetExecutableVendor(IN LPCWSTR lpszFilename)
289 {
290     LPWSTR lpszVendor = NULL;
291     DWORD dwHandle = 0;
292     DWORD dwLen;
293 
294     LPVOID lpData;
295 
296     LPVOID pvData = NULL;
297     UINT BufLen = 0;
298     WORD wCodePage = 0, wLangID = 0;
299     LPWSTR lpszStrFileInfo = NULL;
300 
301     LPWSTR lpszData = NULL;
302 
303     if (lpszFilename == NULL) return NULL;
304 
305     dwLen = GetFileVersionInfoSizeW(lpszFilename, &dwHandle);
306     if (dwLen == 0) return NULL;
307 
308     lpData = MemAlloc(0, dwLen);
309     if (!lpData) return NULL;
310 
311     GetFileVersionInfoW(lpszFilename, dwHandle, dwLen, lpData);
312 
313     if (VerQueryValueW(lpData, L"\\VarFileInfo\\Translation", &pvData, &BufLen))
314     {
315         wCodePage = LOWORD(*(DWORD*)pvData);
316         wLangID   = HIWORD(*(DWORD*)pvData);
317 
318         lpszStrFileInfo = FormatString(L"StringFileInfo\\%04X%04X\\CompanyName",
319                                        wCodePage,
320                                        wLangID);
321     }
322 
323     VerQueryValueW(lpData, lpszStrFileInfo, (LPVOID*)&lpszData, &BufLen);
324     if (lpszData)
325     {
326         lpszVendor = (LPWSTR)MemAlloc(0, BufLen * sizeof(WCHAR));
327         if (lpszVendor)
328             wcscpy(lpszVendor, lpszData);
329     }
330     else
331     {
332         lpszVendor = NULL;
333     }
334 
335     MemFree(lpszStrFileInfo);
336     MemFree(lpData);
337 
338     return lpszVendor;
339 }
340