Is WndProc special function?and callbacks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Is WndProc special function?and callbacks

  1. #1
    Join Date
    Sep 2005
    Posts
    336

    Is WndProc special function?and callbacks

    Hi
    This is code:
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    Here if ichange WndProc name to for example A it can be compiled(also i changed wc.lpfnWndProc = A

    But if i change its parameters is gives me an error:
    error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND,UINT,WPARAM)' to 'WNDPROC'
    for wc.lpfnWndProc = WndProc; line

    So i want to ask
    why do i obey this parameters rule?I completely define a function.So why windows force me to write standart parameter list.

    Also i want to learn what callback is.I heard that callback function is called by windows when event is occured.Is this true?In C# we write a fuction and add to its event with its delegate(function pointer).So windows can call it.
    this.button1.Click += new System.EventHandler(this.button1_Click);
    button1_Click func. is called when button1.Click event is occured.

    But here something different.GetMessage function takes mesg from msg queue.And calls to windproc.So windows doesn't call to callback function.My program itself calls to my callback function.So why is it callback?

    Am i wrong?
    I confused a lot.
    Can you please explain me parameters rules and callback questions.

    Thanks
    Full code: http://www.winprog.org/tutorial/simple_window.html

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Is WndProc special function?and callbacks

    Quote Originally Posted by sawer
    But here something different.GetMessage function takes mesg from msg queue.And calls to windproc.So windows doesn't call to callback function.My program itself calls to my callback function.So why is it callback?
    It is callback, because it is called fom another function, deep inside of OS. And OS expects, that function you provide has some specific signature, defined as WNDPROC. When you do not provide any function with prototype expected by OS (function of type WNDPROC), it is not able to manage messages inside of your window. Documentation clearly states, what window message proc must be of type WNDPROC. So, make it that type, and not other. Compiler complains, because in this line:
    Code:
    wc.lpfnWndProc   = WndProc;
    you are trying to assign incompatible function pointers. Field wc.lpfnWndProc is of type WNDPROC, while your WndProc is not.

    Regards,
    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Sep 2005
    Posts
    336

    Re: Is WndProc special function?and callbacks

    Thanks Hobson.
    But i still don't understand callback mechanism in win32.

  4. #4
    Join Date
    May 2005
    Location
    United States
    Posts
    263

    Re: Is WndProc special function?and callbacks

    Quote Originally Posted by sawer
    Thanks Hobson.
    But i still don't understand callback mechanism in win32.
    OK, my turn to take a crack at it. ;-) All controls/windows in Win32 send your program events through callback functions. If you're programming only using the Win32 API (i.e. no MFC, ATL, or whatever - just plain C code) then you need a WndProc callback function so the operating system can send your application events. The name of the callback doesn't neccessarily have to be "WndProc" but it must match the name of the function pointer supplied in the window/control creation function. The number of parameters and types of each parameter must match Win32's specification, which is:

    Code:
    HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
    Also the return value type must be LRESULT.

    Your callback function will be called whenever Windows needs to send you information about an event that occured in the associated window/control. Inside your WndProc you can choose to handle the event, or pass it along to Windows' default event handler function (called DefWindowProc). Actually, you can do both also (handle the event and call the default), but this usually isn't needed.

    Hope this helps.

    -Greg Dolley

  5. #5
    Join Date
    Sep 2005
    Posts
    336

    Re: Is WndProc special function?and callbacks

    Thanks greg
    One thing that i can't see is:
    In C# I write a function, I register it with its delagate.And when event occurs "windows" call my function.

    But in win32 i wrote in WinMain GetMessage().So it takes message from queue and call specific func.So function is called by "my program itself" not by "windows".But in C# i can see calling callback func. by windows clearly by its event-delegate mechanism.,But in win32 this message loop confused me because as i said before my program handle message itself.It is not done by windows.
    Code:
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    The problem is here.
    Here when/where does windows call callback funcs?(in which line?)

    thanks again.

  6. #6
    Join Date
    May 2005
    Location
    United States
    Posts
    263

    Re: Is WndProc special function?and callbacks

    Quote Originally Posted by sawer
    Code:
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    The problem is here.
    Here when/where does windows call callback funcs?(in which line?)

    thanks again.
    Actually your application never directly calls the callback, unless you just hardcode a call to WndProc. But calling directly is not needed. GetMessage, TranslateMessage, and DispatchMessage are just some functions in the Win32 API that are required for every application to call in order for the app to run (programmatically you're not aware of any of this stuff if you use MFC; but MFC does the exact same thing behind the scenes).

    The actual part of code which calls your WndProc is located inside a Windows operating system dll. I think it's in user32.dll, but I don't remember for sure. GetMessage/TranslateMessage/DispatchMessage are also in an operating system dll, but I don't remember which one (might also be user32.dll).

    If you're really curious and want to find out which dll it's in, just debug your app, put a breakpoint in WndProc, and then step out of WndProc. The debugger will now have you sitting inside the dll which made the call before the breakpoint was hit.

    -Greg Dolley

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center