Hi, I'm Henry! I will help you with this.
Here's a breakdown of potential causes, things to investigate:
Potential Causes & Areas to Investigate:
- Shell Notification/Refresh Logic Change in Windows 11:
- Explorer might perform an initial scan for battery devices at login/startup and then rely on specific notifications that your SwDeviceCreate() flow (or the underlying PnP system's notification to the shell) isn't triggering correctly or promptly for the already running Explorer instance.
- Timing of SwDeviceCreate():
- Creating the device after the user has logged in and Explorer is fully initialized is the critical factor. The shell might have already "decided" there's no battery.
- Driver Capabilities & Properties:
- Are there any specific PnP device capabilities or properties that the Windows 11 shell might query to determine if it should display a battery icon, beyond the basic battery class presence? While unlikely to be the root cause if Settings sees it, it's a remote possibility.
Troubleshooting & Further Investigation:
- Programmatic Shell Refresh (From your Service):
- Since your service has elevated privileges, after SwDeviceCreate succeeds, you could try programmatically telling the shell to refresh.
- The way is SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_FLUSHNOWAIT, NULL, NULL); which forces a lot of the shell to re-evaluate things.
- You could also try broadcasting a WM_SETTINGCHANGE message. For example: SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)L"Policy", SMTO_ABORTIFHUNG, 5000, NULL); or specifically for power-related settings if such a string exists.
- Event Tracing for Windows (ETW):
- Monitor ETW traces focusing on:
- PnP events: Microsoft-Windows-Kernel-PnP
- Power events: Microsoft-Windows-PowerCfg, Microsoft-Windows-Kernel-Power
- Shell events: This is trickier, but look for providers related to Explorer, ShellExperienceHost, or SystemTray.
- Capture traces during the scenario:
- Start tracing.
- Log in.
- Trigger your application to call SwDeviceCreate().
- Observe icon behavior.
- Kill and restart explorer.exe.
- Observe icon behavior.
- Trigger SwDeviceClose().
- Stop tracing.
- Compare the events when the icon doesn't appear vs. when it does after an Explorer restart. Look for specific notifications that Explorer might be missing or ignoring.
- API Monitor / Process Monitor:
- Use Process Monitor (Sysinternals) to filter on explorer.exe. Look for registry queries or file accesses related to power, battery, or system icons when the device is created. See what happens differently before and after the explorer.exe restart.
- API Monitor could show what Windows APIs Explorer is calling related to device enumeration or UI updates.
- Check SW_DEVICE_CREATE_INFO:
- Review all parameters passed to SwDeviceCreate, especially pszInstanceId, pszzHardwareIds, pszzCompatibleIds, and CapabilityFlags. Ensure they are absolutely correct and consistent. Perhaps a subtle change in how Win11 interprets these for shell purposes.
Please do share if you find a solution.