HavenDV / H.NotifyIcon

TrayIcon for WPF/WinUI/Uno/MAUI
MIT License
543 stars 43 forks source link

Unable to create a TrayIcon: TryCreate failed. #103

Open diazynez opened 1 year ago

diazynez commented 1 year ago

Describe the bug

In Scighost/Starward using the H.NotifyIcon project, the TrayIcon cannot be generated. Starward's author stated that the problem came from an internal error by H.NotifyIcon, leaving him unable to determine the specific cause. Link here > https://github.com/Scighost/Starward/issues/251 The log is as follows.

Steps to reproduce the bug

No response

Expected behavior

No response

Screenshots

[20:56:32.288] [EROR] Starward.Services.SystemTrayService Initialize system tray System.InvalidOperationException: TryCreate failed. at H.NotifyIcon.Core.TrayIcon.Create() in //src/libs/H.NotifyIcon/Core/TrayIcon.cs:line 324 at H.NotifyIcon.TaskbarIcon.ForceCreate(Boolean ) in //src/libs/H.NotifyIcon.Shared/TaskbarIcon.cs:line 134 at Starward.Services.SystemTrayService.Initialize(GameBiz gameBiz) in D:\a\Starward\Starward\src\Starward\Services\SystemTrayService.cs:line 72

NuGet package version

No response

Platform

WinUI

IDE

Visual Studio 2022

Windows Version

Windows 10

WindowsAppSDK Version

1.1, Other

WindowsAppSDK Type

Packaged

Manifest

No response

Additional context

No response

HavenDV commented 1 year ago

Unfortunately, this error simply indicates that the Windows API request failed and it does not provide any information about why this happened. I'd be happy to hear about more specific details, but I don't have time to do it myself yet

diazynez commented 1 year ago

That's a shame :( , but I don't know how to get a more detailed error log, I'm just an average and ignorant user of C#.

HavenDV commented 1 year ago

The fact of the matter is that it doesn't exist. The lowest level does not provide any information. We can only try something and select the conditions for the error to occur, and try to understand the problem based on the conditions

mlrsyz commented 1 year ago

Some netizens returned to normal after executing the following script to clean thumbnails. The main ones should be the two lines in the registry.

taskkill /f /im explorer.exe del /f /s /q /a "%LocalAppData%\IconCache.db" cd /d "%LocalAppData%\Microsoft\Windows\Explorer" del /f /s /q /a thumbcache*.db del /f /s /q /a iconcache*.db reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams /f reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream /f start explorer

diazynez commented 1 year ago

Some netizens returned to normal after executing the following script to clean thumbnails. The main ones should be the two lines in the registry.

taskkill /f /im explorer.exe del /f /s /q /a "%LocalAppData%\IconCache.db" cd /d "%LocalAppData%\Microsoft\Windows\Explorer" del /f /s /q /a thumbcache.db del /f /s /q /a iconcache.db reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams /f reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream /f start explorer

I personally tried to find this method is indeed feasible, after running the script has successfully solved the problem, thank you for your generous answer!

HavenDV commented 1 year ago

Unfortunately, registry manipulation requires administrator rights, so this is not applicable in the library. I'll take a deeper look when I have some free time.

Khiro95 commented 4 months ago

I've faced this issue today while using TrayIconWithContextMenu with constant name and the solution, in my case, was to simply change the name of the tray icon to make it generate different GUID. It turned-out that the same GUID should be used from the same path, and that was the issue in my case because the Debug version worked fine but the Release version was crashing.

From NotificationIcon Sample:

Notification icons specified with a GUID are protected against spoofing by validating that only a single application registers them. This registration is performed the first time you call Shell_NotifyIcon(NIM_ADD, ...) and the full path name of the calling application is stored. If you later move your binary file to a different location, the system will not allow the icon to be added again. Please see Shell_NotifyIcon for more information.

Khiro95 commented 4 months ago

@HavenDV So maybe using the parameterless constructor of TrayIcon & TrayIconWithContextMenu should be the recommended way?

HavenDV commented 4 months ago

@HavenDV So maybe using the parameterless constructor of TrayIcon & TrayIconWithContextMenu should be the recommended way?

Yes, I use it also in TaskbarIcon class of main lib.