Thread: Can't store HWND in class?

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Can't store HWND in class?

    I was having some strange problems with my windows app and I finally found out that the handle to my main window, stored in a class, is somehow becoming null. Apparently, something I'm doing is a no-no, but I can't explain it. For example:
    Code:
    class Winbase
    {
    public:
    //...
    private:
      HWND m_hwnd;
    };
    
    bool Winbase::Init(HINSTANCE hinst, WNDPROC WinProc)
    {
    //...
    HWND m_hwnd = CreateWindowEx(0,m_wc.lpszClassName,"A window",WS_OVERLAPPEDWINDOW|WS_SYSMENU,
      0,0,GetSystemMetrics(SM_CXMAXIMIZED),GetSystemMetrics(SM_CYMAXIMIZED),
    	0,0,hinst,0);
    
    Test();
    if(!m_hwnd)
        MessageBox(NULL,"Handle is bad","In Init()",MB_OK);
    //...
    }
    
    void Winbase::Test()
    {
    if(!m_hwnd)
        MessageBox(NULL,"Handle is bad","In Test()",MB_OK);
    }
    The code above produces the message box titled "In Test()" but not a message box titled "In Init()". It seems the handle is valid only as long as I'm in the Init() function (after CreateWindowEx), but I don't understand why
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    I'm probably wrong but it could be because you are declaring the variable again in Init()
    Code:
    bool Winbase::Init(HINSTANCE hinst, WNDPROC WinProc)
    {
    //...
    /*HWND*/ m_hwnd = CreateWindowEx(0,m_wc.lpszClassName,"A window",WS_OVERLAPPEDWINDOW|WS_SYSMENU,
      0,0,GetSystemMetrics(SM_CXMAXIMIZED),GetSystemMetr  ics(SM_CYMAXIMIZED),
    	0,0,hinst,0);
    
    Test();
    if(!m_hwnd)
        MessageBox(NULL,"Handle is bad","In Init()",MB_OK);
    //...
    }
    I would say something like that should have produced an error :s

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I guess I was staring at that code too long last night. I thought there was something really simple but I just couldn't see it heh
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 windows, cpu 100%
    By phil in forum Windows Programming
    Replies: 3
    Last Post: 08-01-2005, 08:45 AM
  2. My first "real" windows app
    By JoshR in forum Windows Programming
    Replies: 2
    Last Post: 07-28-2005, 07:40 AM
  3. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  4. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 07:23 AM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM