Solved

DestroyWindow()

Posted on 2001-08-24
4
315 Views
Last Modified: 2012-06-27
Hi everyone,

I have a problem concerning destroying a window (under windows using pure windows SDK).

I have a class (CWindowClass) where I in a member function called CWindowClass::Create() register and display a window using ::RegisterClass(...) and ::CreateWindow(...). In this class I have an OK-button which when pressed shall destroy the window, so far so good...

However, I want the window to destroy it self when I get the WM_NCDESTROY (either I send it myself or it is sent when the "x"-button in the upper corner is pressed). When I try to unregister the window it (offcourse) complains that there are still windows open, but I cannot close the window before I have processed the message.... Is there any good way to solve this problem???

Following is some code to explain the problem.

Please help me, Im totally stuck in the problem...

Adam


CWindowClass::CWindowClass()
{

}

CWindowClass::Create()
{
    WNDCLASS wcWinProp;

     GetClassInfo(g_hInst, "Button", &wcWinProp); /*g_hInst is a global variable holding the instance*/

    ...
    /*Change some properties for "Button" including name*/
    wcWinProp.lpszClassName = "TestWindow";
    ...

    /*Register the class*/
     if(::RegisterClass(&wcWinProp) == NULL) {
        DWORD dwErrorCode = GetLastError();

        ::MessageBox(NULL, "Class registration failed.", "Fatal Error", MB_OK);
        return false;
     }

    /*Create the window*/
    m_hwnd = ::CreateWindow(...);

    ::ShowWindow(m_hwnd, SW_SHOW);

    return true;
}


BOOL CWindowClass::OnReceiveMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){

...
...
   switch(uMsg) {

...

        case WM_NCDESTROY:
            ...
             if(::UnregisterClass("TestWindow", g_hInst) == NULL){
                    DWORD dwErrorCode = GetLastError();
                  ::MessageBox(NULL, "Class deregistration failed.", "Fatal Error", MB_OK);
             }
            ...
            return FALSE;
...
}
0
Comment
Question by:adamlundqvist
4 Comments
 
LVL 6

Expert Comment

by:MichaelS
ID: 6421418
>When I try to unregister the window
why you want to do that?

any way on your wm_close you can call a member or CWindowClass which will destroy window and unregister class.

wm_close:
pWindowClass->DestroyWindow(hwnd);


CWindowClass::DestroyWindow(HWND hwnd)
{    ::DestroyWindow(hwnd);
     Unre.....();
}
0
 
LVL 2

Expert Comment

by:abesoft
ID: 6422813
The problem is that you are trying to unregister the window class before the window is actually destroyed. The window (or at least the class handler) still exists while you are processing the message. What you probably want to do is to move the "Unregister" code out to a higher level.

Either put it in your message pump, so that when your app terminates it can unregister the window, or move it into your class's destructor (where it belongs, IMO).
0
 

Expert Comment

by:ken_ken
ID: 6422933
yep
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
ID: 6425955
There is usually no need to call ::UnregisterClass()  because all classes registered by an application are automatically unregistered when that app terminates.  The overhead of a class registration is miniscule on the grand scheme of things.  

Really... it just isn't done.  Just remove the UnregisterClass call and your code will work.

If you just can't let go... then do the unregister in the destructor after the Window (the actual HWND) is destroyed.  And if there is an error, ignore that error.

-- Dan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 28
thread-safe code in c++ 2 82
C++ question 3 60
c++ syntax question 9 43
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

939 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

6 Experts available now in Live!

Get 1:1 Help Now