738985c015b13d002521eb9aa3c421bda30456e8
[reactos.git] / reactos / base / applications / msconfig_new / utils.c
1 #include "precomp.h"
2 #include "utils.h"
3 #include "stringutils.h"
4
5 // HANDLE g_hHeap = GetProcessHeap();
6 #define g_hHeap GetProcessHeap()
7
8 #if 0
9 VOID
10 MemInit(IN HANDLE Heap)
11 {
12 /* Save the heap handle */
13 g_hHeap = Heap;
14 }
15 #endif
16
17 BOOL
18 MemFree(IN PVOID lpMem)
19 {
20 /* Free memory back into the heap */
21 return HeapFree(g_hHeap, 0, lpMem);
22 }
23
24 PVOID
25 MemAlloc(IN DWORD dwFlags,
26 IN DWORD dwBytes)
27 {
28 /* Allocate memory from the heap */
29 return HeapAlloc(g_hHeap, dwFlags, dwBytes);
30 }
31
32 LPWSTR
33 FormatDateTime(IN LPSYSTEMTIME pDateTime)
34 {
35 LPWSTR lpszDateTime = NULL;
36
37 if (pDateTime)
38 {
39 int iDateBufSize = 0, iTimeBufSize = 0;
40
41 iDateBufSize = GetDateFormatW(LOCALE_USER_DEFAULT,
42 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE,
43 pDateTime,
44 NULL,
45 NULL,
46 0);
47 iTimeBufSize = GetTimeFormatW(LOCALE_USER_DEFAULT,
48 0,
49 pDateTime,
50 NULL,
51 NULL,
52 0);
53
54 if ( (iDateBufSize > 0) && (iTimeBufSize > 0) )
55 {
56 lpszDateTime = (LPWSTR)MemAlloc(0, (iDateBufSize + iTimeBufSize) * sizeof(TCHAR));
57
58 GetDateFormatW(LOCALE_USER_DEFAULT,
59 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE,
60 pDateTime,
61 NULL,
62 lpszDateTime,
63 iDateBufSize);
64 if (iDateBufSize > 0) lpszDateTime[iDateBufSize-1] = L' ';
65 GetTimeFormatW(LOCALE_USER_DEFAULT,
66 0,
67 pDateTime,
68 NULL,
69 lpszDateTime + iDateBufSize,
70 iTimeBufSize);
71 }
72 }
73
74 return lpszDateTime;
75 }
76
77 VOID
78 FreeDateTime(IN LPWSTR lpszDateTime)
79 {
80 if (lpszDateTime)
81 MemFree(lpszDateTime);
82 }
83
84 LPWSTR
85 LoadResourceStringEx(IN HINSTANCE hInstance,
86 IN UINT uID,
87 OUT size_t* pSize OPTIONAL)
88 {
89 LPWSTR lpszDestBuf = NULL, lpszResourceString = NULL;
90 size_t iStrSize = 0;
91
92 // When passing a zero-length buffer size, LoadString(...) returns a
93 // read-only pointer buffer to the program's resource string.
94 iStrSize = LoadString(hInstance, uID, (LPTSTR)&lpszResourceString, 0);
95
96 if ( lpszResourceString && ( (lpszDestBuf = (LPWSTR)MemAlloc(0, (iStrSize + 1) * sizeof(WCHAR))) != NULL ) )
97 {
98 _tcsncpy(lpszDestBuf, lpszResourceString, iStrSize);
99 lpszDestBuf[iStrSize] = L'\0'; // NULL-terminate the string
100
101 if (pSize)
102 *pSize = iStrSize + 1;
103 }
104 else
105 {
106 if (pSize)
107 *pSize = 0;
108 }
109
110 return lpszDestBuf;
111 }
112
113 LPWSTR
114 LoadConditionalResourceStringEx(IN HINSTANCE hInstance,
115 IN BOOL bCondition,
116 IN UINT uIDifTrue,
117 IN UINT uIDifFalse,
118 IN size_t* pSize OPTIONAL)
119 {
120 return LoadResourceStringEx(hInstance,
121 (bCondition ? uIDifTrue : uIDifFalse),
122 pSize);
123 }
124
125
126 //////////////////// The following comes from MSDN samples ///////////////////
127 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162826(v=vs.85).aspx
128 //
129
130 //
131 // ClipOrCenterRectToMonitor
132 //
133 // The most common problem apps have when running on a
134 // multimonitor system is that they "clip" or "pin" windows
135 // based on the SM_CXSCREEN and SM_CYSCREEN system metrics.
136 // Because of app compatibility reasons these system metrics
137 // return the size of the primary monitor.
138 //
139 // This shows how you use the multi-monitor functions
140 // to do the same thing.
141 //
142 VOID ClipOrCenterRectToMonitor(LPRECT prc, UINT flags)
143 {
144 HMONITOR hMonitor;
145 MONITORINFO mi;
146 RECT rc;
147 int w = prc->right - prc->left;
148 int h = prc->bottom - prc->top;
149
150 //
151 // Get the nearest monitor to the passed rect.
152 //
153 hMonitor = MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST);
154
155 //
156 // Get the work area or entire monitor rect.
157 //
158 mi.cbSize = sizeof(mi);
159 GetMonitorInfo(hMonitor, &mi);
160
161 if (flags & MONITOR_WORKAREA)
162 rc = mi.rcWork;
163 else
164 rc = mi.rcMonitor;
165
166 //
167 // Center or clip the passed rect to the monitor rect.
168 //
169 if (flags & MONITOR_CENTER)
170 {
171 prc->left = rc.left + (rc.right - rc.left - w) / 2;
172 prc->top = rc.top + (rc.bottom - rc.top - h) / 2;
173 prc->right = prc->left + w;
174 prc->bottom = prc->top + h;
175 }
176 else
177 {
178 prc->left = max(rc.left, min(rc.right-w, prc->left));
179 prc->top = max(rc.top, min(rc.bottom-h, prc->top));
180 prc->right = prc->left + w;
181 prc->bottom = prc->top + h;
182 }
183 }
184
185 VOID ClipOrCenterWindowToMonitor(HWND hWnd, UINT flags)
186 {
187 RECT rc;
188 GetWindowRect(hWnd, &rc);
189 ClipOrCenterRectToMonitor(&rc, flags);
190 SetWindowPos(hWnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
191 }
192 ////////////////////////////////////////////////////////////////////////////////
193
194
195 LPWSTR
196 GetExecutableVendor(IN LPCWSTR lpszFilename)
197 {
198 LPWSTR lpszVendor = NULL;
199 DWORD dwHandle = 0;
200 DWORD dwLen;
201
202 LPVOID lpData;
203
204 LPVOID pvData = NULL;
205 UINT BufLen = 0;
206 WORD wCodePage = 0, wLangID = 0;
207 LPTSTR lpszStrFileInfo = NULL;
208
209 LPWSTR lpszData = NULL;
210
211 if (lpszFilename == NULL) return NULL;
212
213 dwLen = GetFileVersionInfoSizeW(lpszFilename, &dwHandle);
214 if (dwLen == 0) return NULL;
215
216 lpData = MemAlloc(0, dwLen);
217 if (!lpData) return NULL;
218
219 GetFileVersionInfoW(lpszFilename, dwHandle, dwLen, lpData);
220
221 if (VerQueryValueW(lpData, L"\\VarFileInfo\\Translation", &pvData, &BufLen))
222 {
223 wCodePage = LOWORD(*(DWORD*)pvData);
224 wLangID = HIWORD(*(DWORD*)pvData);
225
226 lpszStrFileInfo = FormatString(L"StringFileInfo\\%04X%04X\\CompanyName",
227 wCodePage,
228 wLangID);
229 }
230
231 VerQueryValueW(lpData, lpszStrFileInfo, (LPVOID*)&lpszData, &BufLen);
232 if (lpszData)
233 {
234 lpszVendor = (LPWSTR)MemAlloc(0, BufLen * sizeof(WCHAR));
235 if (lpszVendor)
236 wcscpy(lpszVendor, lpszData);
237 }
238 else
239 {
240 lpszVendor = NULL;
241 }
242
243 MemFree(lpszStrFileInfo);
244 MemFree(lpData);
245
246 return lpszVendor;
247 }