ReactOS  0.4.14-dev-49-gfb4591c
misc.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Applications Manager
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * FILE: base/applications/rapps/misc.cpp
5  * PURPOSE: Misc functions
6  * COPYRIGHT: Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
7  * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
8  * Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
9  */
10 #include "rapps.h"
11 
12 #include "misc.h"
13 
14 static HANDLE hLog = NULL;
15 
17 {
18  RECT Rect;
19 
21  return (Rect.right - Rect.left);
22 }
23 
25 {
26  RECT Rect;
27 
29  return (Rect.bottom - Rect.top);
30 }
31 
33 {
34  RECT Rect;
35 
37  return (Rect.right - Rect.left);
38 }
39 
41 {
42  RECT Rect;
43 
45  return (Rect.bottom - Rect.top);
46 }
47 
49 {
50  if (!OpenClipboard(NULL))
51  {
52  return;
53  }
54 
55  HRESULT hr;
56  HGLOBAL ClipBuffer;
57  LPWSTR Buffer;
59 
61  cchBuffer = wcslen(lpszText) + 1;
62  ClipBuffer = GlobalAlloc(GMEM_DDESHARE, cchBuffer * sizeof(WCHAR));
63 
64  Buffer = (PWCHAR) GlobalLock(ClipBuffer);
65  hr = StringCchCopyW(Buffer, cchBuffer, lpszText);
66  GlobalUnlock(ClipBuffer);
67 
68  if (SUCCEEDED(hr))
69  SetClipboardData(CF_UNICODETEXT, ClipBuffer);
70 
72 }
73 
74 VOID ShowPopupMenu(HWND hwnd, UINT MenuID, UINT DefaultItem)
75 {
76  HMENU hMenu = NULL;
77  HMENU hPopupMenu;
78  MENUITEMINFO ItemInfo;
79  POINT pt;
80 
81  if (MenuID)
82  {
83  hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(MenuID));
84  hPopupMenu = GetSubMenu(hMenu, 0);
85  }
86  else
87  {
88  hPopupMenu = GetMenu(hwnd);
89  }
90 
91  ZeroMemory(&ItemInfo, sizeof(ItemInfo));
92  ItemInfo.cbSize = sizeof(ItemInfo);
93  ItemInfo.fMask = MIIM_STATE;
94 
95  GetMenuItemInfoW(hPopupMenu, DefaultItem, FALSE, &ItemInfo);
96 
97  if (!(ItemInfo.fState & MFS_GRAYED))
98  {
99  SetMenuDefaultItem(hPopupMenu, DefaultItem, FALSE);
100  }
101 
102  GetCursorPos(&pt);
103 
105  TrackPopupMenu(hPopupMenu, 0, pt.x, pt.y, 0, hMainWnd, NULL);
106 
107  if (hMenu)
108  {
109  DestroyMenu(hMenu);
110  }
111 }
112 
114 {
115  return StartProcess(const_cast<LPWSTR>(Path.GetString()), Wait);;
116 }
117 
119 {
121  STARTUPINFOW si;
122  DWORD dwRet;
123  MSG msg;
124 
125  ZeroMemory(&si, sizeof(si));
126  si.cb = sizeof(si);
128  si.wShowWindow = SW_SHOW;
129 
130  if (!CreateProcessW(NULL, lpPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
131  {
132  return FALSE;
133  }
134 
135  CloseHandle(pi.hThread);
136 
137  if (Wait)
138  {
140  }
141 
142  while (Wait)
143  {
144  dwRet = MsgWaitForMultipleObjects(1, &pi.hProcess, FALSE, INFINITE, QS_ALLEVENTS);
145  if (dwRet == WAIT_OBJECT_0 + 1)
146  {
147  while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
148  {
151  }
152  }
153  else
154  {
155  if (dwRet == WAIT_OBJECT_0 || dwRet == WAIT_FAILED)
156  break;
157  }
158  }
159 
160  CloseHandle(pi.hProcess);
161 
162  if (Wait)
163  {
167  }
168 
169  return TRUE;
170 }
171 
173 {
175  {
176  Directory.ReleaseBuffer();
177  return FALSE;
178  }
179 
180  Directory.ReleaseBuffer();
181  Directory += L"\\rapps";
182 
183  return (CreateDirectoryW(Directory.GetString(), NULL) || GetLastError() == ERROR_ALREADY_EXISTS);
184 }
185 
187 {
189  {
190  return;
191  }
192 
194  DWORD dwCategoryNum = 1;
195  DWORD dwDisp, dwData;
197 
198  if (key.Create(HKEY_LOCAL_MACHINE,
199  L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\ReactOS Application Manager",
201  {
202  return;
203  }
204 
206  {
207  return;
208  }
209 
212 
213  if ((key.SetStringValue(L"EventMessageFile",
214  szPath,
216  && (key.SetStringValue(L"CategoryMessageFile",
217  szPath,
219  && (key.SetDWORDValue(L"TypesSupported",
220  dwData) == ERROR_SUCCESS)
221  && (key.SetDWORDValue(L"CategoryCount",
222  dwCategoryNum) == ERROR_SUCCESS))
223 
224  {
225  hLog = RegisterEventSourceW(NULL, L"ReactOS Application Manager");
226  }
227 
228  key.Close();
229 }
230 
231 
233 {
234  if (hLog)
235  {
237  }
238 }
239 
240 
241 BOOL WriteLogMessage(WORD wType, DWORD dwEventID, LPCWSTR lpMsg)
242 {
244  {
245  return TRUE;
246  }
247 
248  if (!ReportEventW(hLog, wType, 0, dwEventID,
249  NULL, 1, 0, &lpMsg, NULL))
250  {
251  return FALSE;
252  }
253 
254  return TRUE;
255 }
256 
258  const ATL::CStringW& szRegName,
259  BOOL IsUserKey,
260  REGSAM keyWow)
261 {
262  BOOL bHasSucceded = FALSE;
265  ATL::CStringW szPath = ATL::CStringW(L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%ls") + szRegName;
266 
267  if (key.Open(IsUserKey ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
268  szPath.GetString(),
269  keyWow | KEY_READ) != ERROR_SUCCESS)
270  {
271  return FALSE;
272  }
273 
274  if (szVersionResult != NULL)
275  {
276  ULONG dwSize = MAX_PATH * sizeof(WCHAR);
277 
278  if (key.QueryStringValue(L"DisplayVersion",
279  szVersion.GetBuffer(MAX_PATH),
280  &dwSize) == ERROR_SUCCESS)
281  {
282  szVersion.ReleaseBuffer();
283  *szVersionResult = szVersion;
284  bHasSucceded = TRUE;
285  }
286  else
287  {
288  szVersion.ReleaseBuffer();
289  }
290  }
291  else
292  {
293  bHasSucceded = TRUE;
294  szVersion.ReleaseBuffer();
295  }
296  key.Close();
297 
298  return bHasSucceded;
299 }
300 
301 BOOL GetInstalledVersion(ATL::CStringW *pszVersion, const ATL::CStringW &szRegName)
302 {
303  return (!szRegName.IsEmpty()
304  && (GetInstalledVersion_WowUser(pszVersion, szRegName, TRUE, KEY_WOW64_32KEY)
305  || GetInstalledVersion_WowUser(pszVersion, szRegName, FALSE, KEY_WOW64_32KEY)
306  || GetInstalledVersion_WowUser(pszVersion, szRegName, TRUE, KEY_WOW64_64KEY)
307  || GetInstalledVersion_WowUser(pszVersion, szRegName, FALSE, KEY_WOW64_64KEY)));
308 }
309 
310 // CConfigParser
311 
312 CConfigParser::CConfigParser(const ATL::CStringW& FileName) : szConfigPath(GetINIFullPath(FileName))
313 {
314  CacheINILocale();
315 }
316 
318 {
319  ATL::CStringW szDir;
320  ATL::CStringW szBuffer;
321 
322  GetStorageDirectory(szDir);
323  szBuffer.Format(L"%ls\\rapps\\%ls", szDir.GetString(), FileName.GetString());
324 
325  return szBuffer;
326 }
327 
329 {
330  // TODO: Set default locale if call fails
331  // find out what is the current system lang code (e.g. "0a") and append it to SectionLocale
334 
337 
338  // turn "Section.0c0a" into "Section.0a", keeping just the neutral lang part
340 }
341 
343 {
344  DWORD dwResult;
345 
346  LPWSTR ResultStringBuffer = ResultString.GetBuffer(MAX_PATH);
347  // 1st - find localized strings (e.g. "Section.0c0a")
349  KeyName.GetString(),
350  NULL,
351  ResultStringBuffer,
352  MAX_PATH,
354 
355  if (!dwResult)
356  {
357  // 2nd - if they weren't present check for neutral sub-langs/ generic translations (e.g. "Section.0a")
359  KeyName.GetString(),
360  NULL,
361  ResultStringBuffer,
362  MAX_PATH,
364  if (!dwResult)
365  {
366  // 3rd - if they weren't present fallback to standard english strings (just "Section")
367  dwResult = GetPrivateProfileStringW(L"Section",
368  KeyName.GetString(),
369  NULL,
370  ResultStringBuffer,
371  MAX_PATH,
373  }
374  }
375 
376  ResultString.ReleaseBuffer();
377  return (dwResult != 0 ? TRUE : FALSE);
378 }
379 
381 {
383 
384  iResult = 0;
385 
386  // grab the text version of our entry
387  if (!GetString(KeyName, Buffer))
388  return FALSE;
389 
390  if (Buffer.IsEmpty())
391  return FALSE;
392 
393  // convert it to an actual integer
394  iResult = StrToIntW(Buffer.GetString());
395 
396  // we only care about values > 0
397  return (iResult > 0);
398 }
399 // CConfigParser
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:609
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING KeyName
Definition: ndis.h:4711
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
ATL::CStringW GetINIFullPath(const ATL::CStringW &FileName)
Definition: misc.cpp:317
BOOL WINAPI TranslateMessage(_In_ const MSG *)
ATL::CStringW m_szLocaleID
Definition: misc.h:32
INT WINAPI GetPrivateProfileStringW(LPCWSTR section, LPCWSTR entry, LPCWSTR def_val, LPWSTR buffer, UINT len, LPCWSTR filename)
Definition: profile.c:1142
#define TRUE
Definition: types.h:120
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
#define CloseHandle
Definition: compat.h:398
#define ERROR_SUCCESS
Definition: deptool.c:10
HRESULT hr
Definition: shlfolder.c:183
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
static const INT m_cchLocaleSize
Definition: misc.h:30
#define KEY_READ
Definition: nt_native.h:1023
#define pt(x, y)
Definition: drawing.c:79
#define _countof(array)
Definition: fontsub.cpp:30
#define HKEY_CURRENT_USER
Definition: winreg.h:11
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
CStringT Right(int nCount) const
Definition: cstringt.h:655
static HANDLE ULONG_PTR dwData
Definition: file.c:35
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1635
ATL::CStringW m_szCachedINISectionLocaleNeutral
Definition: misc.h:34
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:668
HWND WINAPI SetFocus(_In_opt_ HWND)
LCID WINAPI GetUserDefaultLCID(void)
Definition: lang.c:768
BOOL GetInt(const ATL::CStringW &KeyName, INT &iResult)
Definition: misc.cpp:380
#define LOCALE_ILANGUAGE
Definition: winnls.h:25
uint16_t * PWCHAR
Definition: typedefs.h:54
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2635
int32_t INT
Definition: typedefs.h:56
HANDLE WINAPI SetClipboardData(_In_ UINT, _In_opt_ HANDLE)
ATL::CStringW m_szCachedINISectionLocale
Definition: misc.h:33
INT GetWindowHeight(HWND hwnd)
Definition: misc.cpp:24
INT WINAPI GetLocaleInfoW(LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len)
Definition: lang.c:1098
INT GetClientWindowWidth(HWND hwnd)
Definition: misc.cpp:32
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:372
#define EVENTLOG_ERROR_TYPE
Definition: winnt_old.h:2630
LRESULT WINAPI DispatchMessageW(_In_ const MSG *)
INT GetClientWindowHeight(HWND hwnd)
Definition: misc.cpp:40
#define MIIM_STATE
Definition: winuser.h:716
unsigned int BOOL
Definition: ntddk_ex.h:94
const ATL::CStringW szConfigPath
Definition: misc.h:36
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
static DWORD cchBuffer
Definition: fusion.c:83
VOID InitLogs()
Definition: misc.cpp:186
#define CF_UNICODETEXT
Definition: constants.h:408
smooth NULL
Definition: ftsmooth.c:416
#define QS_ALLEVENTS
Definition: winuser.h:873
SETTINGS_INFO SettingsInfo
Definition: winmain.cpp:18
Definition: bufpool.h:45
#define STARTF_USESHOWWINDOW
Definition: winbase.h:472
#define SW_SHOW
Definition: winuser.h:769
VOID CacheINILocale()
Definition: misc.cpp:328
BOOL WINAPI DECLSPEC_HOTPATCH CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
Definition: proc.c:4593
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_WRITE
Definition: nt_native.h:1031
#define KEY_WOW64_64KEY
Definition: cmtypes.h:46
BOOL WINAPI GetMenuItemInfoW(_In_ HMENU, _In_ UINT, _In_ BOOL, _Inout_ LPMENUITEMINFOW)
BOOL WINAPI EmptyClipboard(void)
Definition: ntwrapper.h:190
INT GetWindowWidth(HWND hwnd)
Definition: misc.cpp:16
#define WAIT_OBJECT_0
Definition: winbase.h:387
DWORD cb
Definition: winbase.h:817
#define EVENTLOG_INFORMATION_TYPE
Definition: winnt_old.h:2632
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define EVENTLOG_WARNING_TYPE
Definition: winnt_old.h:2631
LONG HRESULT
Definition: typedefs.h:77
#define CSIDL_LOCAL_APPDATA
Definition: shlobj.h:2030
#define MAX_PATH
Definition: compat.h:26
PCXSTR GetString() const
Definition: atlsimpstr.h:361
static HANDLE hLog
Definition: misc.cpp:14
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
#define WAIT_FAILED
Definition: winbase.h:394
BOOL WINAPI CloseClipboard(void)
Definition: ntwrapper.h:178
static DWORD pi
Definition: protocol.c:150
DWORD WINAPI MsgWaitForMultipleObjects(_In_ DWORD nCount, _In_reads_opt_(nCount) CONST HANDLE *pHandles, _In_ BOOL fWaitAll, _In_ DWORD dwMilliseconds, _In_ DWORD dwWakeMask)
static LPCWSTR szVersion
Definition: asmcache.c:748
HWND hMainWnd
Definition: magnifier.c:32
static const WCHAR L[]
Definition: oid.c:1250
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
#define MFS_GRAYED
Definition: winuser.h:746
HKEY key
Definition: reg.c:42
WORD wShowWindow
Definition: winbase.h:829
HMENU WINAPI GetSubMenu(_In_ HMENU, _In_ int)
BOOL WINAPI ReportEventW(IN HANDLE hEventLog, IN WORD wType, IN WORD wCategory, IN DWORD dwEventID, IN PSID lpUserSid, IN WORD wNumStrings, IN DWORD dwDataSize, IN LPCWSTR *lpStrings, IN LPVOID lpRawData)
Definition: eventlog.c:1516
VOID CopyTextToClipboard(LPCWSTR lpszText)
Definition: misc.cpp:48
BOOL WINAPI DestroyMenu(_In_ HMENU)
BOOL WINAPI EnableWindow(_In_ HWND, _In_ BOOL)
BOOL WriteLogMessage(WORD wType, DWORD dwEventID, LPCWSTR lpMsg)
Definition: misc.cpp:241
BOOL GetInstalledVersion(ATL::CStringW *pszVersion, const ATL::CStringW &szRegName)
Definition: misc.cpp:301
bool IsEmpty() const
Definition: atlsimpstr.h:379
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
HINSTANCE hInst
Definition: dxdiag.c:13
PRTL_UNICODE_STRING_BUFFER Path
BOOL bLogEnabled
Definition: winmain.h:9
BOOL WINAPI SHGetSpecialFolderPathW(HWND hwndOwner, LPWSTR szPath, int nFolder, BOOL bCreate)
Definition: shellpath.c:2566
BOOL StartProcess(ATL::CStringW &Path, BOOL Wait)
Definition: misc.cpp:113
LPCWSTR szPath
Definition: env.c:35
#define REG_EXPAND_SZ
Definition: nt_native.h:1494
ACCESS_MASK REGSAM
Definition: winreg.h:69
BOOL GetString(const ATL::CStringW &KeyName, ATL::CStringW &ResultString)
Definition: misc.cpp:342
unsigned int UINT
Definition: ndis.h:50
BOOL WINAPI DeregisterEventSource(IN HANDLE hEventLog)
Definition: eventlog.c:473
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
CConfigParser(const ATL::CStringW &FileName="")
Definition: misc.cpp:312
INT WINAPI StrToIntW(LPCWSTR lpString)
Definition: string.c:411
BOOL WINAPI SetMenuDefaultItem(_In_ HMENU, _In_ UINT, _In_ UINT)
#define GMEM_DDESHARE
Definition: winbase.h:295
BOOL GetStorageDirectory(ATL::CStringW &Directory)
Definition: misc.cpp:172
#define msg(x)
Definition: auth_time.c:54
HMENU WINAPI LoadMenuW(_In_opt_ HINSTANCE, _In_ LPCWSTR)
unsigned int ULONG
Definition: retypes.h:1
base for all directory entries
Definition: entries.h:138
BOOL WINAPI SetForegroundWindow(_In_ HWND)
CAtlStringW CStringW
Definition: atlstr.h:130
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
#define KEY_WOW64_32KEY
Definition: cmtypes.h:45
BOOL WINAPI OpenClipboard(_In_opt_ HWND)
DWORD dwFlags
Definition: winbase.h:828
VOID FreeLogs()
Definition: misc.cpp:232
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
BOOL WINAPI PeekMessageW(_Out_ LPMSG, _In_opt_ HWND, _In_ UINT, _In_ UINT, _In_ UINT)
#define PM_REMOVE
Definition: winuser.h:1182
#define REG_NONE
Definition: nt_native.h:1492
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define INFINITE
Definition: serial.h:102
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
HANDLE WINAPI RegisterEventSourceW(IN LPCWSTR lpUNCServerName, IN LPCWSTR lpSourceName)
Definition: eventlog.c:1295
HMENU WINAPI GetMenu(_In_ HWND)
VOID ShowPopupMenu(HWND hwnd, UINT MenuID, UINT DefaultItem)
Definition: misc.cpp:74
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:60
BOOL WINAPI TrackPopupMenu(_In_ HMENU, _In_ UINT, _In_ int, _In_ int, _Reserved_ int, _In_ HWND, _Reserved_ LPCRECT)
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
struct Rect Rect
#define SUCCEEDED(hr)
Definition: intsafe.h:57
BOOL GetInstalledVersion_WowUser(ATL::CStringW *szVersionResult, const ATL::CStringW &szRegName, BOOL IsUserKey, REGSAM keyWow)
Definition: misc.cpp:257
Definition: path.c:42
IN BOOLEAN Wait
Definition: fatprocs.h:1529
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12