if (List[i] == Owner)
{
- if (i > 0) hWndInsertAfter = List[i-1];
- else hWndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
+ /* We found its Owner, so we must handle it here. */
+ if (i > 0)
+ {
+ if (List[i - 1] != UserHMGetHandle(Window))
+ {
+ /*
+ * If the popup to be inserted is not already just
+ * before the Owner, insert it there. The modified
+ * hWndInsertAfter will be handled below.
+ *
+ * (NOTE: Do not allow hWndInsertAfter to become equal
+ * to the popup's window handle, as this would cause
+ * the popup to link to itself).
+ */
+ hWndInsertAfter = List[i - 1];
+ }
+ else
+ {
+ /* If the popup to be inserted is already
+ * before the Owner, we are done. */
+ ExFreePoolWithTag(List, USERTAG_WINDOWLIST);
+ return hWndInsertAfter;
+ }
+ }
+ else
+ {
+ hWndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
+ }
break;
}