specific_win.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "platform/win/specific_win.h"
  8. #include "platform/win/main_window_win.h"
  9. #include "platform/win/notifications_manager_win.h"
  10. #include "platform/win/windows_app_user_model_id.h"
  11. #include "platform/win/windows_dlls.h"
  12. #include "platform/win/windows_autostart_task.h"
  13. #include "base/platform/base_platform_info.h"
  14. #include "base/platform/win/base_windows_co_task_mem.h"
  15. #include "base/platform/win/base_windows_shlobj_h.h"
  16. #include "base/platform/win/base_windows_winrt.h"
  17. #include "base/call_delayed.h"
  18. #include "ui/boxes/confirm_box.h"
  19. #include "lang/lang_keys.h"
  20. #include "mainwindow.h"
  21. #include "mainwidget.h"
  22. #include "history/history_location_manager.h"
  23. #include "storage/localstorage.h"
  24. #include "core/application.h"
  25. #include "window/window_controller.h"
  26. #include "core/crash_reports.h"
  27. #include <QtCore/QOperatingSystemVersion>
  28. #include <QtWidgets/QApplication>
  29. #include <QtGui/QDesktopServices>
  30. #include <QtGui/QWindow>
  31. #include <Shobjidl.h>
  32. #include <ShObjIdl_core.h>
  33. #include <shellapi.h>
  34. #include <openssl/conf.h>
  35. #include <openssl/engine.h>
  36. #include <openssl/err.h>
  37. #include <dbghelp.h>
  38. #include <Shlwapi.h>
  39. #include <Strsafe.h>
  40. #include <Windowsx.h>
  41. #include <WtsApi32.h>
  42. #include <SDKDDKVer.h>
  43. #include <sal.h>
  44. #include <Psapi.h>
  45. #include <strsafe.h>
  46. #include <ObjBase.h>
  47. #include <propvarutil.h>
  48. #include <functiondiscoverykeys.h>
  49. #include <intsafe.h>
  50. #include <guiddef.h>
  51. #include <locale.h>
  52. #include <ShellScalingApi.h>
  53. #ifndef DCX_USESTYLE
  54. #define DCX_USESTYLE 0x00010000
  55. #endif
  56. #ifndef WM_NCPOINTERUPDATE
  57. #define WM_NCPOINTERUPDATE 0x0241
  58. #define WM_NCPOINTERDOWN 0x0242
  59. #define WM_NCPOINTERUP 0x0243
  60. #endif
  61. using namespace ::Platform;
  62. namespace {
  63. bool themeInited = false;
  64. bool finished = true;
  65. QMargins simpleMargins, margins;
  66. HICON bigIcon = 0, smallIcon = 0, overlayIcon = 0;
  67. [[nodiscard]] uint64 WindowIdFromHWND(HWND value) {
  68. return (reinterpret_cast<uint64>(value) & 0xFFFFFFFFULL);
  69. }
  70. struct FindToActivateRequest {
  71. uint64 processId = 0;
  72. uint64 windowId = 0;
  73. HWND result = nullptr;
  74. uint32 resultLevel = 0; // Larger is better.
  75. };
  76. BOOL CALLBACK FindToActivate(HWND hwnd, LPARAM lParam) {
  77. const auto request = reinterpret_cast<FindToActivateRequest*>(lParam);
  78. DWORD dwProcessId;
  79. ::GetWindowThreadProcessId(hwnd, &dwProcessId);
  80. if ((uint64)dwProcessId != request->processId) {
  81. return TRUE;
  82. }
  83. // Found a Top-Level window.
  84. if (WindowIdFromHWND(hwnd) == request->windowId) {
  85. request->result = hwnd;
  86. request->resultLevel = 3;
  87. return FALSE;
  88. }
  89. const auto data = static_cast<uint32>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
  90. if ((data != 1 && data != 2) || (data <= request->resultLevel)) {
  91. return TRUE;
  92. }
  93. request->result = hwnd;
  94. request->resultLevel = data;
  95. return TRUE;
  96. }
  97. void DeleteMyModules() {
  98. constexpr auto kMaxPathLong = 32767;
  99. auto exePath = std::array<WCHAR, kMaxPathLong + 1>{ 0 };
  100. const auto exeLength = GetModuleFileName(
  101. nullptr,
  102. exePath.data(),
  103. kMaxPathLong + 1);
  104. if (!exeLength || exeLength >= kMaxPathLong + 1) {
  105. return;
  106. }
  107. const auto exe = std::wstring(exePath.data());
  108. const auto last1 = exe.find_last_of('\\');
  109. const auto last2 = exe.find_last_of('/');
  110. const auto last = std::max(
  111. (last1 == std::wstring::npos) ? -1 : int(last1),
  112. (last2 == std::wstring::npos) ? -1 : int(last2));
  113. if (last < 0) {
  114. return;
  115. }
  116. const auto modules = exe.substr(0, last + 1) + L"modules";
  117. const auto deleteOne = [&](const wchar_t *name, const wchar_t *arch) {
  118. const auto path = modules + L'\\' + arch + L'\\' + name;
  119. DeleteFile(path.c_str());
  120. };
  121. const auto deleteBoth = [&](const wchar_t *name) {
  122. deleteOne(name, L"x86");
  123. deleteOne(name, L"x64");
  124. };
  125. const auto removeOne = [&](const std::wstring &name) {
  126. const auto path = modules + L'\\' + name;
  127. RemoveDirectory(path.c_str());
  128. };
  129. const auto removeBoth = [&](const std::wstring &name) {
  130. removeOne(L"x86\\" + name);
  131. removeOne(L"x64\\" + name);
  132. };
  133. deleteBoth(L"d3d\\d3dcompiler_47.dll");
  134. removeBoth(L"d3d");
  135. removeOne(L"x86");
  136. removeOne(L"x64");
  137. RemoveDirectory(modules.c_str());
  138. }
  139. bool ManageAppLink(
  140. bool create,
  141. bool silent,
  142. const GUID &folderId,
  143. const wchar_t *args,
  144. const wchar_t *description) {
  145. if (cExeName().isEmpty()) {
  146. return false;
  147. }
  148. PWSTR startupFolder;
  149. HRESULT hr = SHGetKnownFolderPath(
  150. folderId,
  151. KF_FLAG_CREATE,
  152. nullptr,
  153. &startupFolder);
  154. const auto guard = gsl::finally([&] {
  155. CoTaskMemFree(startupFolder);
  156. });
  157. if (!SUCCEEDED(hr)) {
  158. WCHAR buffer[64];
  159. const auto size = base::array_size(buffer) - 1;
  160. const auto length = StringFromGUID2(folderId, buffer, size);
  161. if (length > 0 && length <= size) {
  162. buffer[length] = 0;
  163. if (!silent) LOG(("App Error: could not get %1 folder: %2").arg(buffer).arg(hr));
  164. }
  165. return false;
  166. }
  167. const auto lnk = QString::fromWCharArray(startupFolder)
  168. + '\\'
  169. + AppFile.utf16()
  170. + u".lnk"_q;
  171. if (!create) {
  172. QFile::remove(lnk);
  173. return true;
  174. }
  175. const auto shellLink = base::WinRT::TryCreateInstance<IShellLink>(
  176. CLSID_ShellLink);
  177. if (!shellLink) {
  178. if (!silent) LOG(("App Error: could not create instance of IID_IShellLink %1").arg(hr));
  179. return false;
  180. }
  181. QString exe = QDir::toNativeSeparators(cExeDir() + cExeName()), dir = QDir::toNativeSeparators(QDir(cWorkingDir()).absolutePath());
  182. shellLink->SetArguments(args);
  183. shellLink->SetPath(exe.toStdWString().c_str());
  184. shellLink->SetWorkingDirectory(dir.toStdWString().c_str());
  185. shellLink->SetDescription(description);
  186. if (const auto propertyStore = shellLink.try_as<IPropertyStore>()) {
  187. PROPVARIANT appIdPropVar;
  188. hr = InitPropVariantFromString(AppUserModelId::Id().c_str(), &appIdPropVar);
  189. if (SUCCEEDED(hr)) {
  190. hr = propertyStore->SetValue(AppUserModelId::Key(), appIdPropVar);
  191. PropVariantClear(&appIdPropVar);
  192. if (SUCCEEDED(hr)) {
  193. hr = propertyStore->Commit();
  194. }
  195. }
  196. }
  197. const auto persistFile = shellLink.try_as<IPersistFile>();
  198. if (!persistFile) {
  199. if (!silent) LOG(("App Error: could not create interface IID_IPersistFile %1").arg(hr));
  200. return false;
  201. }
  202. hr = persistFile->Save(lnk.toStdWString().c_str(), TRUE);
  203. if (!SUCCEEDED(hr)) {
  204. if (!silent) LOG(("App Error: could not save IPersistFile to path %1").arg(lnk));
  205. return false;
  206. }
  207. return true;
  208. }
  209. } // namespace
  210. QString psAppDataPath() {
  211. // 直接返回应用程序目录
  212. return cExeDir();
  213. // 以下是原代码,现在被注释掉
  214. /*
  215. static const int maxFileLen = MAX_PATH * 10;
  216. WCHAR wstrPath[maxFileLen];
  217. if (GetEnvironmentVariable(L"APPDATA", wstrPath, maxFileLen)) {
  218. QDir appData(QString::fromStdWString(std::wstring(wstrPath)));
  219. #ifdef OS_WIN_STORE
  220. return appData.absolutePath() + u"/Telegram Desktop UWP/"_q;
  221. #else // OS_WIN_STORE
  222. return appData.absolutePath() + '/' + AppName.utf16() + '/';
  223. #endif // OS_WIN_STORE
  224. }
  225. return QString();
  226. */
  227. }
  228. QString psAppDataPathOld() {
  229. static const int maxFileLen = MAX_PATH * 10;
  230. WCHAR wstrPath[maxFileLen];
  231. if (GetEnvironmentVariable(L"APPDATA", wstrPath, maxFileLen)) {
  232. QDir appData(QString::fromStdWString(std::wstring(wstrPath)));
  233. return appData.absolutePath() + '/' + AppNameOld.utf16() + '/';
  234. }
  235. return QString();
  236. }
  237. void psDoCleanup() {
  238. try {
  239. Platform::AutostartToggle(false);
  240. psSendToMenu(false, true);
  241. AppUserModelId::CleanupShortcut();
  242. DeleteMyModules();
  243. } catch (...) {
  244. }
  245. }
  246. int psCleanup() {
  247. __try
  248. {
  249. psDoCleanup();
  250. }
  251. __except(EXCEPTION_EXECUTE_HANDLER)
  252. {
  253. return 0;
  254. }
  255. return 0;
  256. }
  257. void psDoFixPrevious() {
  258. try {
  259. static const int bufSize = 4096;
  260. DWORD checkType = 0;
  261. DWORD checkSize = bufSize * 2;
  262. WCHAR checkStr[bufSize] = { 0 };
  263. HKEY newKey1 = nullptr;
  264. HKEY newKey2 = nullptr;
  265. HKEY oldKey1 = nullptr;
  266. HKEY oldKey2 = nullptr;
  267. const auto appId = AppId.utf16();
  268. const auto newKeyStr1 = QString("Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  269. const auto newKeyStr2 = QString("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  270. const auto oldKeyStr1 = QString("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  271. const auto oldKeyStr2 = QString("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  272. const auto newKeyRes1 = RegOpenKeyEx(HKEY_CURRENT_USER, newKeyStr1.c_str(), 0, KEY_READ, &newKey1);
  273. const auto newKeyRes2 = RegOpenKeyEx(HKEY_CURRENT_USER, newKeyStr2.c_str(), 0, KEY_READ, &newKey2);
  274. const auto oldKeyRes1 = RegOpenKeyEx(HKEY_LOCAL_MACHINE, oldKeyStr1.c_str(), 0, KEY_READ, &oldKey1);
  275. const auto oldKeyRes2 = RegOpenKeyEx(HKEY_LOCAL_MACHINE, oldKeyStr2.c_str(), 0, KEY_READ, &oldKey2);
  276. const auto existNew1 = (newKeyRes1 == ERROR_SUCCESS) && (RegQueryValueEx(newKey1, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  277. const auto existNew2 = (newKeyRes2 == ERROR_SUCCESS) && (RegQueryValueEx(newKey2, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  278. const auto existOld1 = (oldKeyRes1 == ERROR_SUCCESS) && (RegQueryValueEx(oldKey1, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  279. const auto existOld2 = (oldKeyRes2 == ERROR_SUCCESS) && (RegQueryValueEx(oldKey2, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  280. if (newKeyRes1 == ERROR_SUCCESS) RegCloseKey(newKey1);
  281. if (newKeyRes2 == ERROR_SUCCESS) RegCloseKey(newKey2);
  282. if (oldKeyRes1 == ERROR_SUCCESS) RegCloseKey(oldKey1);
  283. if (oldKeyRes2 == ERROR_SUCCESS) RegCloseKey(oldKey2);
  284. if (existNew1 || existNew2) {
  285. if (existOld1) RegDeleteKey(HKEY_LOCAL_MACHINE, oldKeyStr1.c_str());
  286. if (existOld2) RegDeleteKey(HKEY_LOCAL_MACHINE, oldKeyStr2.c_str());
  287. }
  288. QString userDesktopLnk, commonDesktopLnk;
  289. WCHAR userDesktopFolder[MAX_PATH], commonDesktopFolder[MAX_PATH];
  290. HRESULT userDesktopRes = SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, 0, SHGFP_TYPE_CURRENT, userDesktopFolder);
  291. HRESULT commonDesktopRes = SHGetFolderPath(0, CSIDL_COMMON_DESKTOPDIRECTORY, 0, SHGFP_TYPE_CURRENT, commonDesktopFolder);
  292. if (SUCCEEDED(userDesktopRes)) {
  293. userDesktopLnk = QString::fromWCharArray(userDesktopFolder) + "\\Telegram.lnk";
  294. }
  295. if (SUCCEEDED(commonDesktopRes)) {
  296. commonDesktopLnk = QString::fromWCharArray(commonDesktopFolder) + "\\Telegram.lnk";
  297. }
  298. QFile userDesktopFile(userDesktopLnk), commonDesktopFile(commonDesktopLnk);
  299. if (QFile::exists(userDesktopLnk) && QFile::exists(commonDesktopLnk) && userDesktopLnk != commonDesktopLnk) {
  300. QFile::remove(commonDesktopLnk);
  301. }
  302. } catch (...) {
  303. }
  304. }
  305. int psFixPrevious() {
  306. __try
  307. {
  308. psDoFixPrevious();
  309. }
  310. __except(EXCEPTION_EXECUTE_HANDLER)
  311. {
  312. return 0;
  313. }
  314. return 0;
  315. }
  316. namespace Platform {
  317. namespace ThirdParty {
  318. namespace {
  319. void StartOpenSSL() {
  320. // Don't use dynamic OpenSSL config, it can load unwanted DLLs.
  321. OPENSSL_load_builtin_modules();
  322. ENGINE_load_builtin_engines();
  323. ERR_clear_error();
  324. OPENSSL_no_config();
  325. }
  326. } // namespace
  327. void start() {
  328. StartOpenSSL();
  329. Dlls::CheckLoadedModules();
  330. }
  331. } // namespace ThirdParty
  332. void start() {
  333. // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale#utf-8-support
  334. setlocale(LC_ALL, ".UTF8");
  335. const auto appUserModelId = AppUserModelId::Id();
  336. SetCurrentProcessExplicitAppUserModelID(appUserModelId.c_str());
  337. LOG(("AppUserModelID: %1").arg(appUserModelId));
  338. }
  339. void finish() {
  340. }
  341. void SetApplicationIcon(const QIcon &icon) {
  342. QApplication::setWindowIcon(icon);
  343. }
  344. QString SingleInstanceLocalServerName(const QString &hash) {
  345. return u"Global\\"_q + hash + '-' + cGUIDStr();
  346. }
  347. #if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
  348. std::optional<bool> IsDarkMode() {
  349. static const auto kSystemVersion = QOperatingSystemVersion::current();
  350. static const auto kDarkModeAddedVersion = QOperatingSystemVersion(
  351. QOperatingSystemVersion::Windows,
  352. 10,
  353. 0,
  354. 17763);
  355. static const auto kSupported = (kSystemVersion >= kDarkModeAddedVersion);
  356. if (!kSupported) {
  357. return std::nullopt;
  358. }
  359. HIGHCONTRAST hcf = {};
  360. hcf.cbSize = static_cast<UINT>(sizeof(HIGHCONTRAST));
  361. if (SystemParametersInfo(SPI_GETHIGHCONTRAST, hcf.cbSize, &hcf, FALSE)
  362. && (hcf.dwFlags & HCF_HIGHCONTRASTON)) {
  363. return std::nullopt;
  364. }
  365. const auto keyName = L""
  366. "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
  367. const auto valueName = L"AppsUseLightTheme";
  368. auto key = HKEY();
  369. auto result = RegOpenKeyEx(HKEY_CURRENT_USER, keyName, 0, KEY_READ, &key);
  370. if (result != ERROR_SUCCESS) {
  371. return std::nullopt;
  372. }
  373. DWORD value = 0, type = 0, size = sizeof(value);
  374. result = RegQueryValueEx(key, valueName, 0, &type, (LPBYTE)&value, &size);
  375. RegCloseKey(key);
  376. if (result != ERROR_SUCCESS) {
  377. return std::nullopt;
  378. }
  379. return (value == 0);
  380. }
  381. #endif // Qt < 6.5.0
  382. bool AutostartSupported() {
  383. return true;
  384. }
  385. void AutostartRequestStateFromSystem(Fn<void(bool)> callback) {
  386. #ifdef OS_WIN_STORE
  387. AutostartTask::RequestState([=](bool enabled) {
  388. crl::on_main([=] {
  389. callback(enabled);
  390. });
  391. });
  392. #endif // OS_WIN_STORE
  393. }
  394. void AutostartToggle(bool enabled, Fn<void(bool)> done) {
  395. #ifdef OS_WIN_STORE
  396. const auto requested = enabled;
  397. const auto callback = [=](bool enabled) { crl::on_main([=] {
  398. if (!Core::IsAppLaunched()) {
  399. return;
  400. }
  401. done(enabled);
  402. if (!requested || enabled) {
  403. return;
  404. } else if (const auto window = Core::App().activeWindow()) {
  405. window->show(Ui::MakeConfirmBox({
  406. .text = tr::lng_settings_auto_start_disabled_uwp(),
  407. .confirmed = [](Fn<void()> close) {
  408. AutostartTask::OpenSettings();
  409. close();
  410. },
  411. .confirmText = tr::lng_settings_open_system_settings(),
  412. }));
  413. }
  414. }); };
  415. AutostartTask::Toggle(
  416. enabled,
  417. done ? Fn<void(bool)>(callback) : nullptr);
  418. #else // OS_WIN_STORE
  419. const auto silent = !done;
  420. const auto success = ManageAppLink(
  421. enabled,
  422. silent,
  423. FOLDERID_Startup,
  424. L"-autostart",
  425. L"Telegram autorun link.\n"
  426. "You can disable autorun in Telegram settings.");
  427. if (done) {
  428. done(enabled && success);
  429. }
  430. #endif // OS_WIN_STORE
  431. }
  432. bool AutostartSkip() {
  433. #ifdef OS_WIN_STORE
  434. return false;
  435. #else // OS_WIN_STORE
  436. return !cAutoStart();
  437. #endif // OS_WIN_STORE
  438. }
  439. void WriteCrashDumpDetails() {
  440. #ifndef TDESKTOP_DISABLE_CRASH_REPORTS
  441. PROCESS_MEMORY_COUNTERS data = { 0 };
  442. if (Dlls::GetProcessMemoryInfo
  443. && Dlls::GetProcessMemoryInfo(
  444. GetCurrentProcess(),
  445. &data,
  446. sizeof(data))) {
  447. const auto mb = 1024 * 1024;
  448. CrashReports::dump()
  449. << "Memory-usage: "
  450. << (data.PeakWorkingSetSize / mb)
  451. << " MB (peak), "
  452. << (data.WorkingSetSize / mb)
  453. << " MB (current)\n";
  454. CrashReports::dump()
  455. << "Pagefile-usage: "
  456. << (data.PeakPagefileUsage / mb)
  457. << " MB (peak), "
  458. << (data.PagefileUsage / mb)
  459. << " MB (current)\n";
  460. }
  461. #endif // TDESKTOP_DISABLE_CRASH_REPORTS
  462. }
  463. void SetWindowPriority(not_null<QWidget*> window, uint32 priority) {
  464. const auto hwnd = reinterpret_cast<HWND>(window->winId());
  465. Assert(hwnd != nullptr);
  466. SetWindowLongPtr(hwnd, GWLP_USERDATA, static_cast<LONG_PTR>(priority));
  467. }
  468. uint64 ActivationWindowId(not_null<QWidget*> window) {
  469. return WindowIdFromHWND(reinterpret_cast<HWND>(window->winId()));
  470. }
  471. void ActivateOtherProcess(uint64 processId, uint64 windowId) {
  472. auto request = FindToActivateRequest{
  473. .processId = processId,
  474. .windowId = windowId,
  475. };
  476. ::EnumWindows((WNDENUMPROC)FindToActivate, (LPARAM)&request);
  477. if (const auto hwnd = request.result) {
  478. ::SetForegroundWindow(hwnd);
  479. ::SetFocus(hwnd);
  480. }
  481. }
  482. } // namespace Platform
  483. namespace {
  484. void _psLogError(const char *str, LSTATUS code) {
  485. LPWSTR errorTextFormatted = nullptr;
  486. auto formatFlags = FORMAT_MESSAGE_FROM_SYSTEM
  487. | FORMAT_MESSAGE_ALLOCATE_BUFFER
  488. | FORMAT_MESSAGE_IGNORE_INSERTS;
  489. FormatMessage(
  490. formatFlags,
  491. NULL,
  492. code,
  493. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  494. (LPTSTR)&errorTextFormatted,
  495. 0,
  496. 0);
  497. auto errorText = errorTextFormatted
  498. ? errorTextFormatted
  499. : L"(Unknown error)";
  500. LOG((str).arg(code).arg(QString::fromStdWString(errorText)));
  501. LocalFree(errorTextFormatted);
  502. }
  503. bool _psOpenRegKey(LPCWSTR key, PHKEY rkey) {
  504. DEBUG_LOG(("App Info: opening reg key %1...").arg(QString::fromStdWString(key)));
  505. LSTATUS status = RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_QUERY_VALUE | KEY_WRITE, rkey);
  506. if (status != ERROR_SUCCESS) {
  507. if (status == ERROR_FILE_NOT_FOUND) {
  508. status = RegCreateKeyEx(HKEY_CURRENT_USER, key, 0, 0, REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE | KEY_WRITE, 0, rkey, 0);
  509. if (status != ERROR_SUCCESS) {
  510. QString msg = u"App Error: could not create '%1' registry key, error %2"_q.arg(QString::fromStdWString(key)).arg(u"%1: %2"_q);
  511. _psLogError(msg.toUtf8().constData(), status);
  512. return false;
  513. }
  514. } else {
  515. QString msg = u"App Error: could not open '%1' registry key, error %2"_q.arg(QString::fromStdWString(key)).arg(u"%1: %2"_q);
  516. _psLogError(msg.toUtf8().constData(), status);
  517. return false;
  518. }
  519. }
  520. return true;
  521. }
  522. bool _psSetKeyValue(HKEY rkey, LPCWSTR value, QString v) {
  523. static const int bufSize = 4096;
  524. DWORD defaultType, defaultSize = bufSize * 2;
  525. WCHAR defaultStr[bufSize] = { 0 };
  526. if (RegQueryValueEx(rkey, value, 0, &defaultType, (BYTE*)defaultStr, &defaultSize) != ERROR_SUCCESS || defaultType != REG_SZ || defaultSize != (v.size() + 1) * 2 || QString::fromStdWString(defaultStr) != v) {
  527. WCHAR tmp[bufSize] = { 0 };
  528. if (!v.isEmpty()) StringCbPrintf(tmp, bufSize, v.replace(QChar('%'), u"%%"_q).toStdWString().c_str());
  529. LSTATUS status = RegSetValueEx(rkey, value, 0, REG_SZ, (BYTE*)tmp, (wcslen(tmp) + 1) * sizeof(WCHAR));
  530. if (status != ERROR_SUCCESS) {
  531. QString msg = u"App Error: could not set %1, error %2"_q.arg(value ? ('\'' + QString::fromStdWString(value) + '\'') : u"(Default)"_q).arg("%1: %2");
  532. _psLogError(msg.toUtf8().constData(), status);
  533. return false;
  534. }
  535. }
  536. return true;
  537. }
  538. }
  539. namespace Platform {
  540. PermissionStatus GetPermissionStatus(PermissionType type) {
  541. if (type == PermissionType::Microphone) {
  542. PermissionStatus result = PermissionStatus::Granted;
  543. HKEY hKey;
  544. LSTATUS res = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore\\microphone", 0, KEY_QUERY_VALUE, &hKey);
  545. if (res == ERROR_SUCCESS) {
  546. wchar_t buf[20];
  547. DWORD length = sizeof(buf);
  548. res = RegQueryValueEx(hKey, L"Value", NULL, NULL, (LPBYTE)buf, &length);
  549. if (res == ERROR_SUCCESS) {
  550. if (wcscmp(buf, L"Deny") == 0) {
  551. result = PermissionStatus::Denied;
  552. }
  553. }
  554. RegCloseKey(hKey);
  555. }
  556. return result;
  557. }
  558. return PermissionStatus::Granted;
  559. }
  560. void RequestPermission(PermissionType type, Fn<void(PermissionStatus)> resultCallback) {
  561. resultCallback(PermissionStatus::Granted);
  562. }
  563. void OpenSystemSettingsForPermission(PermissionType type) {
  564. if (type == PermissionType::Microphone) {
  565. crl::on_main([] {
  566. ShellExecute(
  567. nullptr,
  568. L"open",
  569. L"ms-settings:privacy-microphone",
  570. nullptr,
  571. nullptr,
  572. SW_SHOWDEFAULT);
  573. });
  574. }
  575. }
  576. bool OpenSystemSettings(SystemSettingsType type) {
  577. if (type == SystemSettingsType::Audio) {
  578. crl::on_main([] {
  579. WinExec("control.exe mmsys.cpl", SW_SHOW);
  580. //QDesktopServices::openUrl(QUrl("ms-settings:sound"));
  581. });
  582. }
  583. return true;
  584. }
  585. void NewVersionLaunched(int oldVersion) {
  586. if (oldVersion <= 4009009) {
  587. AppUserModelId::CheckPinned();
  588. }
  589. if (oldVersion > 0 && oldVersion < 2008012) {
  590. // Reset icons cache, because we've changed the application icon.
  591. if (Dlls::SHChangeNotify) {
  592. Dlls::SHChangeNotify(
  593. SHCNE_ASSOCCHANGED,
  594. SHCNF_IDLIST,
  595. nullptr,
  596. nullptr);
  597. }
  598. }
  599. }
  600. QImage DefaultApplicationIcon() {
  601. return Window::Logo();
  602. }
  603. } // namespace Platform
  604. void psSendToMenu(bool send, bool silent) {
  605. ManageAppLink(
  606. send,
  607. silent,
  608. FOLDERID_SendTo,
  609. L"-sendpath",
  610. L"Telegram send to link.\n"
  611. "You can disable send to menu item in Telegram settings.");
  612. }
  613. bool psLaunchMaps(const Data::LocationPoint &point) {
  614. const auto aar = base::WinRT::TryCreateInstance<
  615. IApplicationAssociationRegistration
  616. >(CLSID_ApplicationAssociationRegistration);
  617. if (!aar) {
  618. return false;
  619. }
  620. auto handler = base::CoTaskMemString();
  621. const auto result = aar->QueryCurrentDefault(
  622. L"bingmaps",
  623. AT_URLPROTOCOL,
  624. AL_EFFECTIVE,
  625. handler.put());
  626. if (FAILED(result)
  627. || !handler
  628. || !handler.data()
  629. || std::wstring(handler.data()) == L"bingmaps") {
  630. return false;
  631. }
  632. const auto url = u"bingmaps:?lvl=16&collection=point.%1_%2_Point"_q;
  633. return QDesktopServices::openUrl(
  634. url.arg(point.latAsString()).arg(point.lonAsString()));
  635. }
  636. // Stub while we still support Windows 7.
  637. extern "C" {
  638. STDAPI GetDpiForMonitor(
  639. _In_ HMONITOR hmonitor,
  640. _In_ MONITOR_DPI_TYPE dpiType,
  641. _Out_ UINT *dpiX,
  642. _Out_ UINT *dpiY) {
  643. return Dlls::GetDpiForMonitor
  644. ? Dlls::GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
  645. : E_FAIL;
  646. }
  647. } // extern "C"