Properly handling cases like calling SetWindowPos while processing
WM_NCCREATE.

diff --git a/documentation/gui b/documentation/gui
index cbbcbfb..43bac75 100644
--- a/documentation/gui
+++ b/documentation/gui
@@ -54,7 +54,7 @@
 
 CreateWindow (for child window, not initially visible)
 Messages sent:
- WM_NCCREATE
+ WM_NCCREATE (Note that win->parent->child will not contain win. link is done after sucessfull WM_NCCREATE)
  WM_NCCALCSIZE (wParam=0)
  WM_CREATE
  WM_SIZE
diff --git a/windows/win.c b/windows/win.c
index 27db814..5dc7bea 100644
--- a/windows/win.c
+++ b/windows/win.c
@@ -310,6 +310,7 @@
 BOOL WIN_UnlinkWindow( HWND hwnd )
 {    
     WND *wndPtr, **ppWnd;
+    BOOL ret = FALSE;
 
     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
     else if(!wndPtr->parent)
@@ -317,11 +318,16 @@
         WIN_ReleaseWndPtr(wndPtr);
         return FALSE;
     }
+
     ppWnd = &wndPtr->parent->child;
-    while (*ppWnd != wndPtr) ppWnd = &(*ppWnd)->next;
-    *ppWnd = wndPtr->next;
+    while (*ppWnd && *ppWnd != wndPtr) ppWnd = &(*ppWnd)->next;
+    if (*ppWnd)
+    {
+        *ppWnd = wndPtr->next;
+        ret = TRUE;
+    }
     WIN_ReleaseWndPtr(wndPtr);
-    return TRUE;
+    return ret;
 }
 
 
diff --git a/windows/winpos.c b/windows/winpos.c
index d07f01b..b64e626 100644
--- a/windows/winpos.c
+++ b/windows/winpos.c
@@ -2525,8 +2525,13 @@
 
     if(!(winpos.flags & SWP_NOZORDER))
     {
-        WIN_UnlinkWindow( winpos.hwnd );
-        WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
+       /* upon window creation (while processing WM_NCCREATE), wndPtr->parent is set correctly
+	* 	but wndPtr is not yet in wndPtr->parent->child list
+	* in those cases (SetWindowPos called while processing WM_NCCREATE),
+	*	do not unlink/link winPtr in parent->child
+	*/
+       if ( WIN_UnlinkWindow( winpos.hwnd ) )
+	  WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
     }
 
     /* Reset active DCEs */