Solved

DestroyWindow()

Posted on 1998-09-09
6
317 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:Zainal062797
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1414293
This should not  be happening.  I suspect you are specifying a handle to the parent (application) window, not the child.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1414294
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.)
0
 

Author Comment

by:Zainal062797
ID: 1414295
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 WndProc (HWND, UINT, WPARAM, LPARAM) ;

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) ;
     wndclass.style         = 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) ;
     wndclass.style         = 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 ;
     }





LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     {
      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) ;
     }

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Accepted Solution

by:
danny_pav earned 50 total points
ID: 1414296
Because of the PostQuitMessage in your FireAndRescueWndProc
0
 

Author Comment

by:Zainal062797
ID: 1414297
Okay, then what I am suppose to do to in order to only kill the child window?

Thnaks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1414298
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now