3

My program's call to EnumWindows() returns FALSE and GetLastError() occasionally returns ERROR_ALREADY_EXISTS (#183, "Cannot create a file when that file already exists"). What does that error mean in that context?

Here is a code snippet:

static BOOL CALLBACK CollectTopLevelWindowsEnum(HWND hWnd, LPARAM lParam)
{
    // This one is good
    s_windows.push_back(hWnd);

    return TRUE;
}

...

if (!EnumWindows(CollectTopLevelWindowsEnum, NULL)) {
    DWORD lastError = GetLastError();
    if (lastError != ERROR_SUCCESS) {
        TRACE("EnumWindows failed: %d.\n", lastError);
    }
}
CoreTech
  • 2,345
  • 2
  • 17
  • 24
  • 2
    I'm puzzled. Isn't it the callback duty to `SetLastError` ? Could it be that the `push_back` operation throws and `return TRUE` is never reached ? What happes if you `SetLastError(0)` before `EnumWindows` ? – ixe013 Feb 20 '12 at 18:02
  • 3
    Tough one. If this is a very occasional error then I'd guess at a race caused by a process creating a window while you are enumerating. Here's somebody else with a very similar problem: http://stackoverflow.com/questions/6631089/findwindow-error-183 – Hans Passant Feb 20 '12 at 18:05
  • @ixe013 - push_back doesn't throw; if it did, the code would get an exception and not an error code. I'll try calling SetLastError(0) before EnumWindows and see if that helps, thanks. – CoreTech Feb 20 '12 at 18:09
  • 1
    Thanks @Hans - The situation is very rare, but once it happens it continues to happen every time. I have seen EnumWindows fail several times per minute for over an hour (until the PC was rebooted). Not the usual signature of a race condition... – CoreTech Feb 20 '12 at 18:39
  • 3
    Next time it happens, start killing of processes one-by-one with Taskmgr. With some luck you'll find the evil-doer. – Hans Passant Feb 20 '12 at 19:09
  • Did you ever figure this one out? – jacobsee Dec 07 '12 at 17:46

0 Answers0