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>
11 #include "stringutils.h"
13 // HANDLE g_hHeap = GetProcessHeap();
14 #define g_hHeap GetProcessHeap()
18 MemInit(IN HANDLE Heap
)
20 /* Save the heap handle */
26 MemFree(IN PVOID lpMem
)
28 /* Free memory back into the heap */
29 return HeapFree(g_hHeap
, 0, lpMem
);
33 MemAlloc(IN DWORD dwFlags
,
36 /* Allocate memory from the heap */
37 return HeapAlloc(g_hHeap
, dwFlags
, dwBytes
);
41 FormatDateTime(IN LPSYSTEMTIME pDateTime
)
43 LPWSTR lpszDateTime
= NULL
;
44 int iDateBufSize
, iTimeBufSize
;
46 if (pDateTime
== NULL
) return NULL
;
48 iDateBufSize
= GetDateFormatW(LOCALE_USER_DEFAULT
,
49 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE
,
54 iTimeBufSize
= GetTimeFormatW(LOCALE_USER_DEFAULT
,
61 if ( (iDateBufSize
> 0) && (iTimeBufSize
> 0) )
63 lpszDateTime
= (LPWSTR
)MemAlloc(0, (iDateBufSize
+ iTimeBufSize
) * sizeof(WCHAR
));
65 GetDateFormatW(LOCALE_USER_DEFAULT
,
66 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE
,
71 if (iDateBufSize
> 0) lpszDateTime
[iDateBufSize
-1] = L
' ';
72 GetTimeFormatW(LOCALE_USER_DEFAULT
,
76 lpszDateTime
+ iDateBufSize
,
84 FreeDateTime(IN LPWSTR lpszDateTime
)
87 MemFree(lpszDateTime
);
91 LoadResourceStringEx(IN HINSTANCE hInstance
,
93 OUT
size_t* pSize OPTIONAL
)
95 LPWSTR lpszDestBuf
= NULL
, lpszResourceString
= NULL
;
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);
102 if ( lpszResourceString
&& ( (lpszDestBuf
= (LPWSTR
)MemAlloc(0, (iStrSize
+ 1) * sizeof(WCHAR
))) != NULL
) )
104 wcsncpy(lpszDestBuf
, lpszResourceString
, iStrSize
);
105 lpszDestBuf
[iStrSize
] = L
'\0'; // NULL-terminate the string
108 *pSize
= iStrSize
+ 1;
120 LoadConditionalResourceStringEx(IN HINSTANCE hInstance
,
124 IN
size_t* pSize OPTIONAL
)
126 return LoadResourceStringEx(hInstance
,
127 (bCondition
? uIDifTrue
: uIDifFalse
),
132 RunCommand(IN LPCWSTR lpszCommand
,
133 IN LPCWSTR lpszParameters
,
139 LPWSTR lpszExpandedCommand
;
141 dwNumOfChars
= ExpandEnvironmentStringsW(lpszCommand
, NULL
, 0);
142 lpszExpandedCommand
= (LPWSTR
)MemAlloc(0, dwNumOfChars
* sizeof(WCHAR
));
143 ExpandEnvironmentStringsW(lpszCommand
, lpszExpandedCommand
, dwNumOfChars
);
145 dwRes
= (DWORD
)ShellExecuteW(NULL
, NULL
/* and not L"open" !! */,
149 MemFree(lpszExpandedCommand
);
155 //////////////////// The following comes from MSDN samples ///////////////////
156 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162826(v=vs.85).aspx
160 // ClipOrCenterRectToMonitor
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.
168 // This shows how you use the multi-monitor functions
169 // to do the same thing.
171 VOID
ClipOrCenterRectToMonitor(LPRECT prc
, UINT flags
)
176 int w
= prc
->right
- prc
->left
;
177 int h
= prc
->bottom
- prc
->top
;
180 // Get the nearest monitor to the passed rect.
182 hMonitor
= MonitorFromRect(prc
, MONITOR_DEFAULTTONEAREST
);
185 // Get the work area or entire monitor rect.
187 mi
.cbSize
= sizeof(mi
);
188 GetMonitorInfo(hMonitor
, &mi
);
190 if (flags
& MONITOR_WORKAREA
)
196 // Center or clip the passed rect to the monitor rect.
198 if (flags
& MONITOR_CENTER
)
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
;
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
;
214 VOID
ClipOrCenterWindowToMonitor(HWND hWnd
, UINT flags
)
217 GetWindowRect(hWnd
, &rc
);
218 ClipOrCenterRectToMonitor(&rc
, flags
);
219 SetWindowPos(hWnd
, NULL
, rc
.left
, rc
.top
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
221 ////////////////////////////////////////////////////////////////////////////////
224 BOOL
IsWindowsOS(VOID
)
226 BOOL bIsWindowsOS
= FALSE
;
228 OSVERSIONINFOW osvi
= {0};
229 osvi
.dwOSVersionInfoSize
= sizeof(osvi
);
231 if (!GetVersionExW(&osvi
))
234 if (osvi
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
)
237 /* ReactOS reports as Windows NT 5.2 */
239 if ( (osvi
.dwMajorVersion
== 5 && osvi
.dwMinorVersion
>= 2) ||
240 (osvi
.dwMajorVersion
> 5) )
244 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
245 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
246 0, KEY_QUERY_VALUE
, &hKey
) == ERROR_SUCCESS
)
249 DWORD dwType
= 0, dwBufSize
= 0;
251 ret
= RegQueryValueExW(hKey
, L
"ProductName", NULL
, &dwType
, NULL
, &dwBufSize
);
252 if (ret
== ERROR_SUCCESS
&& dwType
== REG_SZ
)
254 LPTSTR lpszProductName
= (LPTSTR
)MemAlloc(0, dwBufSize
);
255 RegQueryValueExW(hKey
, L
"ProductName", NULL
, &dwType
, (LPBYTE
)lpszProductName
, &dwBufSize
);
257 bIsWindowsOS
= (FindSubStrI(lpszProductName
, _T("Windows")) != NULL
);
259 MemFree(lpszProductName
);
273 BOOL
IsPreVistaOSVersion(VOID
)
275 OSVERSIONINFOW osvi
= {0};
276 osvi
.dwOSVersionInfoSize
= sizeof(osvi
);
278 if (!GetVersionExW(&osvi
))
281 /* Vista+-class OSes are NT >= 6 */
282 return ( (osvi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) ? (osvi
.dwMajorVersion
< 6) : FALSE
);
286 GetExecutableVendor(IN LPCWSTR lpszFilename
)
288 LPWSTR lpszVendor
= NULL
;
294 LPVOID pvData
= NULL
;
296 WORD wCodePage
= 0, wLangID
= 0;
297 LPWSTR lpszStrFileInfo
= NULL
;
299 LPWSTR lpszData
= NULL
;
301 if (lpszFilename
== NULL
) return NULL
;
303 dwLen
= GetFileVersionInfoSizeW(lpszFilename
, &dwHandle
);
304 if (dwLen
== 0) return NULL
;
306 lpData
= MemAlloc(0, dwLen
);
307 if (!lpData
) return NULL
;
309 GetFileVersionInfoW(lpszFilename
, dwHandle
, dwLen
, lpData
);
311 if (VerQueryValueW(lpData
, L
"\\VarFileInfo\\Translation", &pvData
, &BufLen
))
313 wCodePage
= LOWORD(*(DWORD
*)pvData
);
314 wLangID
= HIWORD(*(DWORD
*)pvData
);
316 lpszStrFileInfo
= FormatString(L
"StringFileInfo\\%04X%04X\\CompanyName",
321 VerQueryValueW(lpData
, lpszStrFileInfo
, (LPVOID
*)&lpszData
, &BufLen
);
324 lpszVendor
= (LPWSTR
)MemAlloc(0, BufLen
* sizeof(WCHAR
));
326 wcscpy(lpszVendor
, lpszData
);
333 MemFree(lpszStrFileInfo
);