diff --git a/src/modules/MouseWithoutBorders/App/Class/Common.cs b/src/modules/MouseWithoutBorders/App/Class/Common.cs index 63d73ebbcb..d087ca59af 100644 --- a/src/modules/MouseWithoutBorders/App/Class/Common.cs +++ b/src/modules/MouseWithoutBorders/App/Class/Common.cs @@ -735,7 +735,7 @@ namespace MouseWithoutBorders SendPackage(src, PackageType.ClipboardCapture); } - internal static void ShowToolTip(string tip, int timeOutInMilliseconds = 5000, ToolTipIcon icon = ToolTipIcon.Info, bool showBalloonTip = true) + internal static void ShowToolTip(string tip, int timeOutInMilliseconds = 5000, ToolTipIcon icon = ToolTipIcon.Info, bool showBalloonTip = true, bool forceEvenIfHidingOldUI = false) { if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop) { @@ -752,7 +752,7 @@ namespace MouseWithoutBorders { if (MainForm != null) { - MainForm.ShowToolTip(tip, timeOutInMilliseconds); + MainForm.ShowToolTip(tip, timeOutInMilliseconds, forceEvenIfHidingOldUI: forceEvenIfHidingOldUI); } else { diff --git a/src/modules/MouseWithoutBorders/App/Class/Program.cs b/src/modules/MouseWithoutBorders/App/Class/Program.cs index 964d0b1d3a..252901334e 100644 --- a/src/modules/MouseWithoutBorders/App/Class/Program.cs +++ b/src/modules/MouseWithoutBorders/App/Class/Program.cs @@ -46,6 +46,8 @@ namespace MouseWithoutBorders.Class private static readonly string ServiceModeArg = "UseService"; + public static bool ShowServiceModeErrorTooltip; + [STAThread] private static void Main() { @@ -98,6 +100,7 @@ namespace MouseWithoutBorders.Class { Common.Log("Couldn't start the service. Will try to continue as not a service."); Common.Log(ex); + ShowServiceModeErrorTooltip = true; serviceMode = false; Setting.Values.UseService = false; } diff --git a/src/modules/MouseWithoutBorders/App/Form/frmScreen.cs b/src/modules/MouseWithoutBorders/App/Form/frmScreen.cs index ec66e2e4dd..8decdebe35 100644 --- a/src/modules/MouseWithoutBorders/App/Form/frmScreen.cs +++ b/src/modules/MouseWithoutBorders/App/Form/frmScreen.cs @@ -893,11 +893,23 @@ namespace MouseWithoutBorders } } - internal void ShowToolTip(string txt, int timeOutInMilliseconds, ToolTipIcon icon = ToolTipIcon.Info) + internal void ShowToolTip(string txt, int timeOutInMilliseconds, ToolTipIcon icon = ToolTipIcon.Info, bool forceEvenIfHidingOldUI = false) { if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop) { + var oldNotifyVisibility = NotifyIcon.Visible; + + // In order to show tooltips, the icon needs to be shown. + if (forceEvenIfHidingOldUI) + { + NotifyIcon.Visible = true; + } + NotifyIcon.ShowBalloonTip(timeOutInMilliseconds, Application.ProductName, txt, icon); + if (forceEvenIfHidingOldUI) + { + NotifyIcon.Visible = oldNotifyVisibility; + } } } @@ -939,6 +951,11 @@ namespace MouseWithoutBorders NotifyIcon.Visible = false; NotifyIcon.Visible = Setting.Values.ShowOriginalUI; } + + if (Program.ShowServiceModeErrorTooltip) + { + Common.ShowToolTip("Couldn't start the service. Will continue as not a service. Service mode must be enabled in the Settings again.", 10000, forceEvenIfHidingOldUI: true); + } } private void MenuAbout_Click(object sender, EventArgs e) diff --git a/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp b/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp index e942dfe734..b4b24b6e7c 100644 --- a/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp +++ b/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp @@ -243,13 +243,27 @@ private: } } + // Pass local app data of the current user to the service + PWSTR cLocalAppPath; + winrt::check_hresult(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &cLocalAppPath)); + CoTaskMemFree(cLocalAppPath); + + std::wstring localAppPath{ cLocalAppPath }; + std::wstring binaryWithArgsPath = L"\""; + binaryWithArgsPath += servicePath; + binaryWithArgsPath += L"\" "; + binaryWithArgsPath += escapeDoubleQuotes(localAppPath); + bool alreadyRegistered = false; + bool isServicePathCorrect = true; if (pServiceConfig) { std::wstring_view existingServicePath{ pServiceConfig->lpBinaryPathName }; - existingServicePath = existingServicePath.substr(1, existingServicePath.find(L'"', 1) - 1); - alreadyRegistered = std::filesystem::path{ existingServicePath } == servicePath; - LocalFree(pServiceConfig); + alreadyRegistered = true; + isServicePathCorrect = (existingServicePath == binaryWithArgsPath); + if (isServicePathCorrect) { + Logger::warn(L"The service path is not correct. Current: {} Expected: {}", existingServicePath, binaryWithArgsPath); + } if (alreadyRegistered && pServiceConfig->dwStartType == SERVICE_DISABLED) { @@ -274,47 +288,34 @@ private: } } } - - if (alreadyRegistered) - { - SERVICE_DELAYED_AUTO_START_INFO delayedAutoStartInfo; - if (!QueryServiceConfig2W(schService, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, reinterpret_cast(&delayedAutoStartInfo), sizeof(delayedAutoStartInfo), &bytesNeeded) || delayedAutoStartInfo.fDelayedAutostart) - { - alreadyRegistered = false; - // Wait until the service has been actually deleted so we can recreate it again - for (int i = 0; i < 10; ++i) - { - schService = OpenServiceW(schSCManager, SERVICE_NAME, SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG); - if (schService) - { - CloseServiceHandle(schService); - Sleep(1000); - } - else - { - break; - } - } - } - } + LocalFree(pServiceConfig); } if (alreadyRegistered) { + if (!isServicePathCorrect) { + if (!ChangeServiceConfigW(schService, + SERVICE_NO_CHANGE, + SERVICE_NO_CHANGE, + SERVICE_NO_CHANGE, + binaryWithArgsPath.c_str(), + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr)) + { + Logger::error(L"Failed to update the service's path. ERROR: {}", GetLastError()); + } + else + { + Logger::info(L"Updated the service's path."); + } + } return; } - // Pass local app data of the current user to the service - PWSTR cLocalAppPath; - winrt::check_hresult(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &cLocalAppPath)); - CoTaskMemFree(cLocalAppPath); - - std::wstring localAppPath{ cLocalAppPath }; - std::wstring binaryWithArgsPath = L"\""; - binaryWithArgsPath += servicePath; - binaryWithArgsPath += L"\" "; - binaryWithArgsPath += escapeDoubleQuotes(localAppPath); - schService = CreateServiceW( schSCManager, SERVICE_NAME,