Thread: OOP with windows message loops problem

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    OOP with windows message queue problem

    Hi all,

    I have 2 classes Class1 and mainWindow. mainWindow has a function with the following signature:

    Code:
    mainWindow::DetectorProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    Now Class1 has the WinMain entry point and it also has the WinProc procedure to deal with the messages. Now what I want to do is create a dialogue from a resource file which I have created and to tell it to use the DetectorProc function in the other class. I tried this but with no luck, this is in the WinProc procedure of Class1.

    Code:
    case WM_CREATE:
    			{
    				mainWindow = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_GUIMAIN),
    					hwnd, mainWindow::DetectorProc);
    			
    				if(mainWindow != NULL)
    				{
    					ShowWindow(mainWindow, SW_SHOW);
    				}
    			}
    			break;
    However I get the following error message:

    Code:
    error C2664: 'CreateDialogParamA' : cannot convert parameter 4 from 'int (__stdcall mainWindow::*)(struct HWND__ *,unsigned in
    t,unsigned int,long)' to 'int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'
    I tried doin the following but with no luck (it compiled fine but it seems tha the DetectorProc procedure wasnt getting the messages)

    Code:
    case WM_CREATE:
    			{
    typedef int (CALLBACK *DetectorProc)(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
    
    				DetectorProc dp;
    				dp = (DetectorProc)::GetProcAddress((HINSTANCE)mainWindow, "DetectorProc");
    				mainWindow = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_GUIMAIN),
    					hwnd, dp);
    			
    				if(mainWindow != NULL)
    				{
    					ShowWindow(mainWindow, SW_SHOW);
    				}
    			}
    			break;
    Hopefully you can understand what im trying to do and can show me the right path as im pretty sure im not suppossed to use GetProcAddress as I have done above.

    Thanks for any help
    Last edited by cloudy; 01-21-2006 at 12:57 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well most C++ class member functions receive a 'this' pointer as a hidden parameter, which is obviously not going to be accounted for when windows calls your callback function.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you want to have your window procedure in a class, it needs to be static like this:
    Code:
    class mainWindow:
    {
    public:
    static LRESULT CALLBACK EditProc(HWND,UINT,WPARAM,LPARAM);
    }
    However, you won't be able to access any non-static data in the class so you need to associate an object of your class with the hwnd of the window in some way. For example, using a std::map:
    Code:
    static std::map<HWND,mainWindow*> Win;
    switch (msg)
    {
    //...
    case WM_NCCREATE:
        {
          Win[hwnd] = new mainWindow;
        }
    
    }
    I know this has been discussed before so I'll see if I can find a link or two to threads that give a better explanation.
    "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

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    "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. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. shutting down windows
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2002, 12:28 PM