CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: c++ question

  1. #1
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Question c++ question

    Hi, take a look at this:

    LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    // Route all Windows messages to the game engine
    return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
    }

    Here i am routing windows messages to my game engine so that HandleEvent can be the function to process the messagess. My question is:

    I dont quite understand how returning HandleEvent(hWindow, msg, wParam, lParam); to WndProc() routes windows messages to the HandleEvent() function, could you explain to me how windows reads this code so that it routes the messages?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: c++ question

    Think about this by such way: you call HandleEvent function with hWindow, msg, wParam, lParam parameters. It makes all necessary handling and returns some value. This value is returned by WndProc function.

  3. #3
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: c++ question

    Hmm, im still abit confused on how wndproc actually knows to route the messages to handleevent just by returning handleevent, because with a normal function if we returned a value that function would hold that value, like in this example:

    int sum()

    {
    int a = 5 + 2;
    return a;
    }

    x = sum();

    Here, because we have returned int a to sum(), we can assign its returned value to a
    variable, now, x is equal to 7.

    So i cant see how wndproc knows to route all messages to handleevent.

    Thanks for the replies

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: c++ question

    Quote Originally Posted by TpOreilly View Post
    Hmm, im still abit confused on how wndproc actually knows to route the messages to handleevent just by returning handleevent
    Rather than think of it as 'returning HandleEvent', think of it as 'calling HandleEvent, then returning the return value of HandleEvent. These concepts -- calling functions && function return values, are the *basics* of C++. Looks like you skipped past your 'beginning C++' book and jumped ahead to 'game development in Win32/MFC & c++' or something. Thats...gonna be difficult for you.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: c++ question

    Quote Originally Posted by TpOreilly View Post
    Hmm, im still abit confused on how wndproc actually knows to route the messages to handleevent just by returning handleevent, because with a normal function if we returned a value that function would hold that value, like in this example:
    What you are showing has nothing to do with how the Windows OS works. You are comparing apples to elephants.

    The operating system calls WndProc, then inside of your WndProc, you're calling function X (doesn't matter what X is). X returns a value that you are now to send back to the OS. Then the OS, given the return value, does something with it -- what that something is, we can only speculate since we don't have the source code to the OS kernel.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: c++ question

    Code:
    LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        LRESULT result = GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
        return result;
    }
    When written by such way, is it clear?

  7. #7
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: c++ question

    Thanks for the replies, i have a few questions:

    1.When we use example(); in a return value, in an if statement, in ANYTHING, does that function always get called in whatever way its being used?

    2. So, in this code, i am returning the return value of handleevent right? The function returns this:

    return DefWindowProc(hWindow, msg, wParam, lParam);

    I have a little look on the internet, but i havnt found an explanation of what DefWindowproc() does in term which i can understand, could you explain it to me please. Thanks


  8. #8
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: c++ question

    I would understand it if in the WndProc() we just called HandleEvent() instead of returning it, so if we put this...

    LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
    }

    ...Id understand that we are calling the HandleEvent to do its bit. But its when we return the return value of HandleEvent().

  9. #9
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: c++ question

    ??

  10. #10
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: c++ question

    Ahh ive got it!

    when a function calls another funtion, it can pass values to that function (either by copy, by reference or by pointer). So here we are passing the variables from WndProc() to the HandleEvent() function.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: c++ question

    Quote Originally Posted by TpOreilly View Post
    I would understand it if in the WndProc() we just called HandleEvent() instead of returning it, so if we put this...
    Maybe you'll understand this better:
    Code:
    LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
    {
       LRESULT theReturnValue = GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
    
       return theReturnValue;
    }
    Now is that more understandable what's going on?

    As to WndProc -- it is very important you return the correct value from WndProc. If you don't do that, the OS kernel function that called WndProc will not work correctly. That kernel function is relying on your function to return a proper value. If you don't return a value, you are then returning an unknown item back to the kernel, and you have no idea what can happen after that.

    What is the correct value to return from WndProc? That is where reading the documentation (i.e. MSDN, for example) comes into play. Depending on the message parameter, you return a different value, and that value is whatever the documentation tells you to return. Don't just make up your own number -- it isn't a "don't care" scenario. Windows programs that act erratically can trace the erratic behaviour back to not return proper values from WndProc.

    The bottom line is this -- your WndProc returns a value back to the OS kernel -- not just any value, but a value that means something to Windows after that WndProc is called.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 27th, 2011 at 08:18 PM.

  12. #12
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: c++ question

    Thanks, but now that i have learnt...

    "when a function calls another funtion, it can pass values to that function (either by copy, by reference or by pointer). So here we are passing the variables from WndProc() to the HandleEvent() function."

    ...i didnt know that before, i now understand whats going on here.

    Thanks for the help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured