Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: c++/win32 - static and callback

  1. #1
    Intern Contributor
    Join Date
    Dec 2001
    Location
    Porto Velho
    Posts
    97

    c++/win32 - static and callback

    I have a Window class with a callback on it.
    So each instance (window) must have its own callback(so callback cannot be static), but "wc.lpfnWndProc" requires a "static" member.

    The error:
    Code :
    error C2440: '=' : cannot convert from 'long (__stdcall MYCLASS::*)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'

  2. #2
    Junior Member Regular Contributor
    Join Date
    Dec 2001
    Location
    Belmont, CA, USA
    Posts
    216

    Re: c++/win32 - static and callback

    So you want to have a single class with several wndproc functions to choose from? That doesn't sound right. Or do you want to have a virtual wndproc and implement it in derived classes? That sounds better but it won't work in your case.
    How about storing the address of the window in the user data field? Then have a static callback function in the base class which grabs the address and calls the real callback function (implemented in the derived class).

  3. #3
    Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    442

    Re: c++/win32 - static and callback

    Your callback must be a static member. The solution is to use a map, to map instances to member functions and to execute the appropriate method.

    Frameworks like MFC and ATL do this automatically, so you don't have to worry about all the WndProc rubbish.

  4. #4
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    2,833

    Re: c++/win32 - static and callback

    C++ does this automatically. Your class methods are merely static C functions, and the C++ precompiler invisibly passes in an instance argument every time you call one.
    The Win32 framework is a C interface, so you have to do this yourself.
    Knackered

  5. #5
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,256

    Re: c++/win32 - static and callback

    >>> So each instance (window) must have its own callback(so callback cannot be static), but "wc.lpfnWndProc" requires a "static" member.<<<

    Callbacks are static. They are your basic C style functions.

    wc.lpfnWndProc=MYCLASS::function;

    where function is declared static.

    V-man
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Feb 2001
    Location
    Switzerland
    Posts
    1,594

    Re: c++/win32 - static and callback

    in createwindow you can give over an additional parameter, wich is the thispointer.
    in the static messageproc you check for WM_NCCREATE and there, you look for this additional userparameter of createwindow (the CREATEWINDOWSTRUCT you ressieve in one param, i think the LPARAM). this this-pointer you got over there you store into your window with SetWindowLong(GWL_USERDATA,(long)_the_this_pointer _of_createwindow);

    then, for all other messsages, use GetWindowLong(hwnd,GWL_USERDATA) to recieve the thispointer again, and call the nonstatic per window implemented HRESULT Window::WndProc(UINT msg,WPARAM wParam,LPARAM lParam);

    thats the way that is the fastest and best one..
    http://davepermen.net - if i could stay true to my heart, i would feel totally free

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    2,411

    Re: c++/win32 - static and callback

    For MDI child windows, you need to pass a specific Windows struct as the lpParam parameter. I prefer to pass an actual CLIENTCREATESTRUCT but embellish with my own window pointer, something like:

    struct MyCreateStruct {
    IMyWindow * window;
    CLIENTCREATESTRUCT mdiData;
    };

    Then you pass &myStruct.mdiData as lpParam, and recover the pointer to MyCreateStruct inside NCCREATE using pointer arithmetic.
    "If you can't afford to do something right,
    you'd better make sure you can afford to do it wrong!"

  8. #8
    Intern Contributor
    Join Date
    Dec 2001
    Location
    Porto Velho
    Posts
    97

    Re: c++/win32 - static and callback

    davepermen;

    do you have an example?

  9. #9
    Intern Contributor
    Join Date
    Dec 2001
    Location
    Porto Velho
    Posts
    97

    Re: c++/win32 - static and callback

    Hey guys. please help me. I Know theres an simple and object oriented solution for this problem.


    Thanks a lot;


  10. #10
    Senior Member OpenGL Pro
    Join Date
    Feb 2001
    Location
    Switzerland
    Posts
    1,594

    Re: c++/win32 - static and callback

    have fun: http://www.itstudents.ch/users/dave/...les/Window.zip
    its now some time old, dunno. but it works..

    it has more in than you want, but, its a working example
    http://davepermen.net - if i could stay true to my heart, i would feel totally free

Posting Permissions

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