Pointer to a class inside the WindowsProcedure function

This is a discussion on Pointer to a class inside the WindowsProcedure function within the Windows Programming forums, part of the Platform Specific Boards category; Basically, i have a class which starts a thread that spawns a new window. The function running inside the new ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    48

    Pointer to a class inside the WindowsProcedure function

    Basically, i have a class which starts a thread that spawns a new window. The function running inside the new thread gets a pointer to the class object to acces inside variables. The problem comes with the windows procedure used by the new window created. How can i have a pointer to the class object inside the callback function?

    Code:
    //UserInterface.h
    
    #include "Main.h"
    
    struct UserInterface_str
    {
           char * szClassName;
           char * chTitle;
           
    };
    
    UserInterface_str * CreateUserInterface(char * szClassName, char * chTitle)
    {
              
        UserInterface_str * strUserInterface;
        strUserInterface = new UserInterface_str;
        
        strUserInterface->szClassName = new char[strlen(szClassName)];
        strcpy(strUserInterface->szClassName, szClassName);
        
        strUserInterface->chTitle = new char[strlen(chTitle)];
       strcpy(strUserInterface->chTitle, chTitle);
        
        
        return strUserInterface;
    }
    
    
    
    class UserInterface_c;
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    unsigned __stdcall UserInterfaceThread_1(void* pArg  );
    unsigned __stdcall UserInterfaceThread_2(void* pArg  );
    
    class UserInterface_c
    {  protected: 
                  InputQueue_c InputQueue;
                  HANDLE hThread_1, hThread_2; 
                  unsigned ThreadID_1, ThreadID_2;
                  
       
       public:
              HANDLE mInput;
              char * szClassName;
              char * chTitle;
              HWND hwnd;               
              MSG messages;            
              WNDCLASSEX wincl;
              HINSTANCE hInstance;
              
              HWND hwndReceiveEditBox;
              
              
              
              
              
              
              UserInterface_c() 
              {                 
                                chTitle=new char[7];                            
                                szClassName=new char[11];
                                strcpy(chTitle,"Default");
                                strcpy(szClassName,"WindowsApp");
                                hInstance = (HINSTANCE)GetModuleHandle(NULL);  
                                
                                
              }
              ~UserInterface_c() {}
              void Input(Data_str* strData) 
              {
                   InputQueue.Input(strData);
              }
              void Start(void * pArg)
              {
                 UserInterface_str *  strUserInterface = (UserInterface_str *) pArg;
                 
                 if(strUserInterface != NULL)
                 {
                                     delete(chTitle);                                 
                                     chTitle = new char [strlen(strUserInterface->chTitle)];
                                     strcpy(chTitle, strUserInterface->chTitle);
                                     
                                     delete(szClassName);
                                     szClassName = new char [strlen(strUserInterface->szClassName)];
                                     strcpy(szClassName, strUserInterface->szClassName);
                                     
                 }
                 
                 hThread_1 =(HANDLE) _beginthreadex(NULL,0, &UserInterfaceThread_1, (LPVOID) this, 0, &ThreadID_1);
                 hThread_2 =(HANDLE) _beginthreadex(NULL,0, &UserInterfaceThread_2, (LPVOID) this, 0, &ThreadID_2); 
                   
              }
                   
    };
    
    
    unsigned __stdcall UserInterfaceThread_1(void* pArg  )
    {
             UserInterface_c * pUserInterface = reinterpret_cast<UserInterface_c *>(pArg);
             
             /* The Window structure */
             pUserInterface->wincl.hInstance = pUserInterface->hInstance;
             pUserInterface->wincl.lpszClassName = pUserInterface->szClassName;
             pUserInterface->wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
             pUserInterface->wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
             pUserInterface->wincl.cbSize = sizeof (WNDCLASSEX);
            
               /* Use default icon and mouse-pointer */
             pUserInterface->wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
             pUserInterface->wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
             pUserInterface->wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
             pUserInterface->wincl.lpszMenuName = NULL;                 /* No menu */
             pUserInterface->wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
             pUserInterface->wincl.cbWndExtra = 0;                      /* structure or the window instance */
             /* Use Windows's default color as the background of the window */
             pUserInterface->wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
             
             if ( !RegisterClassEx (&pUserInterface->wincl) ) cout<<"Failed";
             
             pUserInterface->hwnd = CreateWindowEx (
                                  0,                   /* Extended possibilites for variation */
                                  pUserInterface->szClassName,         /* Classname */
                                  pUserInterface->chTitle,       /* Title Text */
                                  WS_OVERLAPPEDWINDOW, /* default window */
                                  CW_USEDEFAULT,       /* Windows decides the position */
                                  CW_USEDEFAULT,       /* where the window ends up on the screen */
                                  544,                 /* The programs width */
                                  375,                 /* and height in pixels */
                                  HWND_DESKTOP,        /* The window is a child-window to desktop */
                                  NULL,                /* No menu */
                                  pUserInterface->hInstance,       /* Program Instance handler */
                                  NULL                 /* No Window Creation data */
                                  );
             
             ShowWindow (pUserInterface->hwnd, 1);
             DWORD dwReceiveMessage;
             HANDLE hReceiveMessage;
        
        
      
    
    
        
             while (GetMessage (&pUserInterface->messages, NULL, 0, 0))
             {      
                    TranslateMessage(&pUserInterface->messages);       
                    DispatchMessage(&pUserInterface->messages);
             }
             
               
             return 0;
    }
    
    unsigned __stdcall UserInterfaceThread_2(void* pArg  )
    {
             UserInterface_c * pUserInterface = reinterpret_cast<UserInterface_c *>(pArg);
             
             
             return 0;
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
              {
                      switch(message)
                      {
                                     case WM_CREATE:
                                          {
                                                    //hwndReceiveEditBox=NULL;
                                                             
                                                    
                                                   /* hwndReceiveEditBox = CreateWindow( "edit", 
                                                     "",                                                     
                                                     WS_VISIBLE|WS_CHILD|WS_BORDER|WS_VSCROLL|ES_READONLY|ES_MULTILINE|ES_WANTRETURN|ES_AUTOHSCROLL|ES_AUTOVSCROLL,
                                                     0, 
                                                     0, xReceiveEditBox, 
                                                     yReceiveEditBox, 
                                                     hwnd, 
                                                     (HMENU)2, 
                                                     ((LPCREATESTRUCT) lParam)->hInstance, 
                                                     NULL);
                                                     ReleaseDC (hwnd, hdc); */
                                          
                                          }
                      }
                      
                      
                      
                      
                              return DefWindowProc(hwnd, message, wParam, lParam);
                      
              }
    
    
    
    
    
    int main()
    {   
        
        
        
        UserInterface_c cModuleUserInterface;
        cModuleUserInterface.Start(CreateUserInterface("UserInterface","UserInterface 1"));
        
        cin.get();
        return 0;
    }
    Last edited by like_no_other; 06-01-2009 at 08:10 AM.

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If the window procedure is not shared among multiple windows, then using a static pointer is probably the easiest way.

    In your WM_CREATE handler:
    Code:
    static UserInterface_c* pMyWindow;
    ...
    pMyWindow = (UserInterface_c*)((LPCREATESTRUCT) lParam)->lpCreateParams;
    Then you would need to pass the window pointer to the CreateWindowEx() call.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    i want to be able to spawn multiple windows by creating new class objects. I'll look into codeplug's suggestion. But, should I use a readymade gui framework, what would you recommend?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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