Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. AfxRegisterWndClass crashes when called again

AfxRegisterWndClass crashes when called again

Scheduled Pinned Locked Moved C / C++ / MFC
c++
6 Posts 2 Posters 14 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    etechX2
    wrote on last edited by
    #1

    Hi, In my MFC project, I get crash as follows: 1. i call MyCreate() then MyUnCreate() 2. calling MyCreate() the second time, causes crash at AfxRegisterWndClass below. sudo code provided: CMyCwndSub is derived from CWnd. in another file: CMyCwndSub *p;

    void MyCreate()
    {
    //in here code looks for Window with title "MyWndWin".

    p->new CMyCwndSub;
    CString m = **AfxRegisterWndClass**(0);       //crashes in this line the 2nd time called.
    p->CreateEx(0, m, -T"MyWndWin",0,0,0,0,0,NULL,0);
    
    .
    .
    .
    

    }

    void MyUnCreate()
    {
    delete p;
    p = NULL;
    }

    V 1 Reply Last reply
    0
    • E etechX2

      Hi, In my MFC project, I get crash as follows: 1. i call MyCreate() then MyUnCreate() 2. calling MyCreate() the second time, causes crash at AfxRegisterWndClass below. sudo code provided: CMyCwndSub is derived from CWnd. in another file: CMyCwndSub *p;

      void MyCreate()
      {
      //in here code looks for Window with title "MyWndWin".

      p->new CMyCwndSub;
      CString m = **AfxRegisterWndClass**(0);       //crashes in this line the 2nd time called.
      p->CreateEx(0, m, -T"MyWndWin",0,0,0,0,0,NULL,0);
      
      .
      .
      .
      

      }

      void MyUnCreate()
      {
      delete p;
      p = NULL;
      }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Try to [UnregisterClassA function (winuser.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-unregisterclassa) in the MyUnCreate()

      E 1 Reply Last reply
      0
      • V Victor Nijegorodov

        Try to [UnregisterClassA function (winuser.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-unregisterclassa) in the MyUnCreate()

        E Offline
        E Offline
        etechX2
        wrote on last edited by
        #3

        would this work?.

        //inside mfc library
        void MyUnCreate()
        {
        ::UnregisterClass(_T("MyWndWin"), AfxGetInstanceHndle());
        delete p;
        p = NULL;
        }

        V 1 Reply Last reply
        0
        • E etechX2

          would this work?.

          //inside mfc library
          void MyUnCreate()
          {
          ::UnregisterClass(_T("MyWndWin"), AfxGetInstanceHndle());
          delete p;
          p = NULL;
          }

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          Didn't you test it? :confused: Also you must pass to UnregisterClass the className returned by AfxRegisterWndClass(...), not the caption of your window!

          E 1 Reply Last reply
          0
          • V Victor Nijegorodov

            Didn't you test it? :confused: Also you must pass to UnregisterClass the className returned by AfxRegisterWndClass(...), not the caption of your window!

            E Offline
            E Offline
            etechX2
            wrote on last edited by
            #5

            I did the following and still crashed. Maybe the AfxRegisterWndClass is set to release class automatically at the end when library exits. ?

            //inside mfc library
            void MyUnCreate()
            {
            ::UnregisterClass(ClassName, AfxGetInstanceHandle());
            delete p;
            p = NULL;
            }

            V 1 Reply Last reply
            0
            • E etechX2

              I did the following and still crashed. Maybe the AfxRegisterWndClass is set to release class automatically at the end when library exits. ?

              //inside mfc library
              void MyUnCreate()
              {
              ::UnregisterClass(ClassName, AfxGetInstanceHandle());
              delete p;
              p = NULL;
              }

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              etechX2 wrote:

              Maybe the AfxRegisterWndClass is set to release class automatically at the end when library exits. ?

              1. Please read the Microsoft documentation about AfxRegisterWndClass; 2. Why are you trying to unregister and then reregister already registered Class? just save its name and then use it again!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups