Making a (atom) class in a (struct) class makes big errors!

This is a discussion on Making a (atom) class in a (struct) class makes big errors! within the Windows Programming forums, part of the Platform Specific Boards category; A while ago I tried to create some functions that would automaticly handle classes and their windows for me. But ...

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534

    Making a (atom) class in a (struct) class makes big errors!

    A while ago I tried to create some functions that would automaticly handle classes and their windows for me. But the program would crash when I called CreateWindow(), I found that the problem was dealing with the class. But how so I am unsure of. Now I tried to make another one like it from scratch. Again, the same problem. Does anyone know why the class isn't working with the window? Code below.
    Code:
    // Script by Yarin Licht.
    // Made September, 2007.
    
    #include <windows.h>
    #include <commctrl.h>
    
    struct POD {
       WNDCLASSEX wincl;
       int nWide, nHigh;
       // Got rid of a line
       char* pszMenu;
       HWND hwnd;
       MSG msg;
    };
    
    HANDLE NewApp(HINSTANCE hInst, char *pszClass, char *pszMenu, WNDPROC wndproc)
    {
       POD *poda = new POD[1];
       poda[0].hwnd = NULL;
       ZeroMemory(&poda[0].wincl, sizeof(WNDCLASSEX));
       poda[0].wincl.hInstance = hInst;
       poda[0].wincl.lpszClassName = pszClass;
       poda[0].wincl.lpfnWndProc = wndproc;
       poda[0].wincl.style = CS_DBLCLKS;
       poda[0].wincl.cbSize = sizeof(WNDCLASSEX);
       poda[0].wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
       poda[0].wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
       poda[0].wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
       poda[0].wincl.lpszMenuName = pszMenu;
       poda[0].wincl.cbClsExtra = 0;
       poda[0].wincl.cbWndExtra = 0;
       poda[0].wincl.hbrBackground = (HBRUSH)COLOR_3DSHADOW;
       if(!RegisterClassEx(&poda[0].wincl)) return NULL;
       return (HANDLE)poda;
    }
    
    #define pod (((POD*)hPod)[0])
    
    int MakeApp(HANDLE hPod, char *pszTitle, int nWide, int nHigh)
    {
       int WINDOW_WIDTH = nWide, WINDOW_HEIGHT = nHigh;
       WINDOW_WIDTH += GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
       WINDOW_HEIGHT += GetSystemMetrics(SM_CXFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
       if(pod.wincl.lpszMenuName) WINDOW_HEIGHT += GetSystemMetrics(SM_CYMENU);
       pod.hwnd = CreateWindowEx(WS_EX_CONTROLPARENT, pod.wincl.pszClassName, pszTitle,
                                 WS_OVERLAPPEDWINDOW &~WS_THICKFRAME &~WS_MAXIMIZEBOX,
                                 (GetSystemMetrics(SM_CXSCREEN)/2) - (WINDOW_WIDTH/2),
                                 (GetSystemMetrics(SM_CYSCREEN)/2) - (WINDOW_HEIGHT/2),
                                 WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, pod.wincl.hInstance, 0);
       if(!pod.hwnd) return FALSE;
       ShowWindow(pod.hwnd, SW_SHOWNORMAL);
       return TRUE;
    }
    
    WPARAM RunApp(HANDLE hPod)
    {
       while(GetMessage(&pod.msg, NULL, 0, 0)) {
          TranslateMessage(&pod.msg);
          if(pod.msg.message == WM_DESTROY) break;
          DispatchMessage(&pod.msg);   }
       return pod.msg.wParam;
    }
    
    void FreeApp(HANDLE hPod)
    {
       if(pod.hwnd) SendMessage(pod.hwnd, WM_DESTROY, 0, 0);
       UnregisterClass(pod.wincl.pszClassName, pod.wincl.hInstance);
       delete [ ](POD*)hPod;
       return;
    }
    
    // End of script file.
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       switch(message)
       {
          case WM_DESTROY:
          {
             PostQuitMessage(0);
             break;
          }
          default:
             return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return TRUE;
    }
    
    int WINAPI WinMain(HINSTANCE hThis, HINSTANCE hPrevInst, LPSTR lpszArgs, int nShow)
    {
       HANDLE hApp = NULL;
       hApp = NewApp(hThis, "TestClass", NULL, WindowProcedure);
       MakeApp(hApp, "Test App", 300, 300);
       RunApp(hApp);
       FreeApp(hApp);
       return TRUE;
    }
    
    // End of script file.
    Last edited by Yarin; 09-10-2007 at 10:09 PM.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    From first glance, I see that pod.pszClass is used without being initialized. (i.e. NewApp() does not allocate memory for pod.pszClass, but MakeApp() expects it to point to something valid.)

    p.s. your macro "pod" is bound to cause some confusion with local var "pod" as well..
    Last edited by @nthony; 09-10-2007 at 07:06 PM.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534
    Okay, I got that fixed, but the same problem is still there.
    Please help!
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Not 100&#37; but I think the WNDPROC has to be a 'static' method.

    This relates to how the class stores links to methods (function pointers) without scope (ie uses WndProc rather than the correct this->WinProc or MyClass::WinProc). That is WIN32 does not support (.NET) delegates.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534
    Through some random finingling I got it working. But what you said, novacain, makes scence, so I'm doing that to.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Big class and pointer to that.
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2002, 11:25 AM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Advice on Class with Stuct
    By rippascal in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21