According to the documentaions, DestroyWindow() is suppose to kill the window I spicify by its handle. However, I am getting a different result. When using DestroyWindow() to destroy a child window, the function terminates the application as well (the parent). I don't know why this is happening? Is this a bug?! Or am I missing something? Please advise?

Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This should not  be happening.  I suspect you are specifying a handle to the parent (application) window, not the child.
How are you getting the handle?  Have you been able to confirm that it is indeed the right handle?

Another posibility is that the child window's window procedure has code that terminates the application or closes the parent when it closed.  A similar possibility is that the parent window's window procedure has code that closes the window or terminates the application when the child window is closed.  

If these windows belong to an application written by someone else, these are possibilities.  If you wrote the application in question, then this is not likely (or you would know what is going on and wouldn't have asked.)
Zainal062797Author Commented:
This is the code I am using. I wrote it all. It is so simple (for testing). Try to complie it and see for yourself.


LRESULT CALLBACK FireAndRescueWndProc (HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     switch (iMsg)

          case WM_DESTROY :

               PostQuitMessage (0) ;

               return(0) ;

     return DefWindowProc (hWnd, iMsg, wParam, lParam) ;

}  // end of  FireAndRescueWndProc()

BOOL RegisterFireAndRescueClass()
     WNDCLASSEX   wndclass ;

     wndclass.cbSize        = sizeof (wndclass) ;         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = FireAndRescueWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = NULL;  
     wndclass.hIcon         = NULL ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (DKGRAY_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = "FireAndRescue" ;
     wndclass.hIconSm       = NULL ;

     return (RegisterClassEx (&wndclass)) ;

}  // end of  RegisterFireAndRescueClass()

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
     static char szAppName[] = "HelloWin" ;
     HWND        hwnd ;
     MSG         msg ;
     WNDCLASSEX  wndclass ;

     wndclass.cbSize        = sizeof (wndclass) ;         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

     RegisterClassEx (&wndclass) ;
      RegisterFireAndRescueClass() ;

     hwnd = CreateWindow (szAppName,         // window class name
                        "The Hello Program",     // window caption
                    WS_OVERLAPPEDWINDOW,     // window style
                    CW_USEDEFAULT,           // initial x position
                    CW_USEDEFAULT,           // initial y position
                    CW_USEDEFAULT,           // initial x size
                    CW_USEDEFAULT,           // initial y size
                    NULL,                    // parent window handle
                    NULL,                    // window menu handle
                    hInstance,               // program instance handle
                        NULL) ;                         // creation parameters

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     return msg.wParam ;

      static HINSTANCE            hInstance ;
     HDC                              hdc ;
     PAINTSTRUCT                  ps ;
     RECT                              rect ;
      static HWND                  hFireAndRescueWnd ;
      static HWND                  hButton ;

     switch (iMsg)
          case WM_CREATE :
                  hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
               // PlaySound ("hellowin.wav", NULL, SND_FILENAME | SND_ASYNC) ;
                  // create the fire and rescue window
                  hFireAndRescueWnd = CreateWindow ("FireAndRescue", "",
                                                            WS_CHILD | WS_BORDER | WS_VISIBLE,
                                                            20, 20, 100, 100,
                                                            (hwnd), NULL, hInstance, NULL) ;

                  hButton = CreateWindow ("BUTTON", "Kill Window",
                                                            WS_CHILD | WS_BORDER | WS_VISIBLE,
                                                            200, 20, 100, 100,
                                                            (hwnd), 1, hInstance, NULL) ;

               return 0 ;

          case WM_PAINT :
                 hdc = BeginPaint (hwnd, &ps) ;

               GetClientRect (hwnd, &rect) ;

               DrawText (hdc, "Hello, Windows 95!", -1, &rect,
                               DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

                EndPaint (hwnd, &ps) ;

               return 0 ;

          case WM_COMMAND :
                  switch (LOWORD (wParam))
                        case 1 :

                              MessageBeep(0) ;
                              DestroyWindow (hFireAndRescueWnd) ;

                              return(0) ;

                  return(0) ;

          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;

     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Because of the PostQuitMessage in your FireAndRescueWndProc

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Zainal062797Author Commented:
Okay, then what I am suppose to do to in order to only kill the child window?

The child window procedure should not call PostQuitMessage() in the WM_DESTROY handler.  Only the parent window should do that (in most cases).  Calling PostQuiteMessage() will cause your application to termiante.  You want to do that only when the parent is closed, not he child.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.