- 1 year ago
IShellWindows::FindWindowSW returns S_FALSE for desktop if explorer restarted
The IShellWindows::FindWindowSW
method returning S_FALSE
(0x00000001) when attempting to find the desktop window after restarting Windows Explorer is an expected behavior.
When you restart Windows Explorer, the desktop window undergoes a recreation process. During this process, the previous desktop window is destroyed, and a new desktop window is created. As a result, the previous instance of the desktop window becomes invalid, and any attempt to find it using IShellWindows::FindWindowSW
will return S_FALSE
to indicate that the window was not found.
To work around this issue, you can perform the following steps:
Wait for the desktop window to be fully recreated: After restarting Windows Explorer, give it some time to complete the desktop window recreation process. You can introduce a delay or use a mechanism to check if the desktop window is available before attempting to find it again.
Retry the
IShellWindows::FindWindowSW
method: After waiting for an appropriate amount of time, retry callingIShellWindows::FindWindowSW
to find the desktop window. This time, it should return a valid window handle instead ofS_FALSE
.
Here's an example of how you can implement the retry logic in C++ using the IShellWindows
interface:
cpp#include <Shobjidl.h>
HWND FindDesktopWindow()
{
IShellWindows *pShellWindows;
HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pShellWindows));
if (SUCCEEDED(hr))
{
VARIANT vEmpty;
VariantInit(&vEmpty);
HWND hwndDesktop;
hr = pShellWindows->FindWindowSW(&vEmpty, &vEmpty, SWC_DESKTOP, reinterpret_cast<long*>(&hwndDesktop), SWFO_NEEDDISPATCH);
pShellWindows->Release();
if (SUCCEEDED(hr))
{
if (hwndDesktop != NULL)
{
// Desktop window found successfully
return hwndDesktop;
}
else
{
// Desktop window not found, retry or return an error
}
}
else if (hr == S_FALSE)
{
// Desktop window not found, retry or return an error
}
}
// Error occurred or desktop window not found
return NULL;
}
Remember to handle any potential error conditions and adjust the retry logic as needed in your specific implementation.
Please note that this workaround assumes the use of the IShellWindows
interface to find the desktop window. If you are using a different approach, please provide more details about your code or the specific API you're using, and I'll be glad to assist you further.