Howto: Class member function as callback function for dialog box

Discussion in 'C++' started by prettysmurfed, Jul 19, 2003.

  1. Hi all

    I have a bit of a problem, the subject of this post is almost selfexplaing.
    But here goes: Heres an example of the code I want to implement, its
    all nice and simple, but the flaw is I can't seem to get the adress of the
    member function stated properly in the DialogBox function. I thought it
    was enough to write it as Dialog::DlgProc, if it was a normal situation
    and DlgProc was a normal function (not member of anything) then you
    would simply do it like this:

    DialogBox(hInstance,TEXT("DLG"),hwnd,DlgProc);

    What is wrong with my current approach. I've already been googling
    the issue all day, and the only thing I could come up with was making
    the member function static, but thats not fun, as I can't change anything
    within the callback procedure.

    ////////////////////////////////////////////////////////////////////////////
    //////////////////////////////
    class Dialog
    {
    public :
    Dialog();
    void Init(HINSTANCE, HWND);
    ~Dialog();
    private :
    BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
    };

    void Dialog::Init(HINSTANCE hInstance, HWND hwnd)
    {
    DialogBox(hInstance,TEXT("DLG"),hwnd,Dialog::DlgProc);
    }

    BOOL CALLBACK Dialog::DlgProc(HWND h, UINT m, WPARAM w, LPARAM l)
    {
    ...
    }
    ////////////////////////////////////////////////////////////////////////////
    //////////////////////////////

    I'm currently using Dev-c++ 4.9.8.0, and the error message generated
    by the compiler is

    In member function `void OOWrapper::start(HINSTANCE__*,
    cannot convert `BOOL (OOWrapper::*)(HWND__*, unsigned int,

    I hope theres a way out this,
    Go steady
    Martin
     
    prettysmurfed, Jul 19, 2003
    #1
    1. Advertising

  2. "prettysmurfed" <> wrote in message
    news:3f19089c$0$22649$...
    > Hi all
    >
    > I have a bit of a problem, the subject of this post is almost

    selfexplaing.
    > But here goes: Heres an example of the code I want to implement, its
    > all nice and simple, but the flaw is I can't seem to get the adress of the
    > member function stated properly in the DialogBox function. I thought it
    > was enough to write it as Dialog::DlgProc, if it was a normal situation
    > and DlgProc was a normal function (not member of anything) then you
    > would simply do it like this:
    >
    > DialogBox(hInstance,TEXT("DLG"),hwnd,DlgProc);
    >
    > What is wrong with my current approach. I've already been googling
    > the issue all day, and the only thing I could come up with was making
    > the member function static, but thats not fun, as I can't change anything
    > within the callback procedure.
    >
    >

    ////////////////////////////////////////////////////////////////////////////
    > //////////////////////////////
    > class Dialog
    > {
    > public :
    > Dialog();
    > void Init(HINSTANCE, HWND);
    > ~Dialog();
    > private :
    > BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
    > };
    >
    > void Dialog::Init(HINSTANCE hInstance, HWND hwnd)
    > {
    > DialogBox(hInstance,TEXT("DLG"),hwnd,Dialog::DlgProc);
    > }
    >
    > BOOL CALLBACK Dialog::DlgProc(HWND h, UINT m, WPARAM w, LPARAM l)
    > {
    > ...
    > }
    >

    ////////////////////////////////////////////////////////////////////////////
    > //////////////////////////////
    >
    > I'm currently using Dev-c++ 4.9.8.0, and the error message generated
    > by the compiler is
    >
    > In member function `void OOWrapper::start(HINSTANCE__*,
    > cannot convert `BOOL (OOWrapper::*)(HWND__*, unsigned int,
    >
    > I hope theres a way out this,
    > Go steady
    > Martin
    >


    Making the callback static is the only solution.

    Instead of using DialogBox, you should use DialogBoxParam, this takes a
    fifth parameter. Use that fifth parameter as a pointer to your OOWrapper
    object. The static callback function will receive that pointer as part of
    the WM_INITDIALOG message. You can store that pointer some where and use it
    to call non-static member functions on your OOWrapper object.

    This is the standard way of dealing with the 'how do I use a C++ member
    function as a callback' issue.

    Also see the FAQ

    http://www.parashift.com/c -faq-lite/pointers-to-members.html

    john
     
    John Harrison, Jul 19, 2003
    #2
    1. Advertising

  3. OOPS, I'm sorry for sending the reply to your mailbox!!

    Thanks for the reply John, though not a very helpful one :)

    I was afraid this was the only solution, so I have
    already taken the full step and start removing any parameter
    altering code in my callback func. So be it <sigh>.
    I really hope theres a good explanation for this because
    I don't like having those functions messing around out of
    scope, and I'm certainly not going to pick up MFC
    programming. I just want it to be possible to do my
    own OOD within the boundaries of normal windows
    programming - and when I'm at it I would really like a millionĀ£,
    a new car and peace throughout the world...

    Thanks again, I hope you have a nice and sunny day
    Martin
     
    prettysmurfed, Jul 19, 2003
    #3
  4. Thanks again for the help John

    I've read through the
    http://www.parashift.com/c -faq-lite/pointers-to-members.html
    and it really gave me the insight, in what the heck I was doing.
    Now I have encapsulated the whole thing into a neat little package, with
    only
    two calls in my main function, thats beautiful! Instead of before, where the
    object was scattered all over the place, with not so few globals!
    I had a slight problem with the passed object-reference in DialogBoxParam,
    but it turned out to be that I had forgot to make the variable static in the
    callback
    routine, resulting in alot of teeth-grinding, shame-on-you's and other stuff
    I have
    edited out.

    go steady
    Martin
     
    prettysmurfed, Jul 19, 2003
    #4
  5. "John Harrison" <> skrev i en meddelelse news:bfb3ra$c8a1o$-berlin.de...
    :
    : Making the callback static is the only solution.
    :
    : Instead of using DialogBox, you should use DialogBoxParam, this takes a
    : fifth parameter. Use that fifth parameter as a pointer to your OOWrapper
    : object. The static callback function will receive that pointer as part of
    : the WM_INITDIALOG message. You can store that pointer some where and use it
    : to call non-static member functions on your OOWrapper object.

    Yes, but you can only make one instance of the object or am I wrong? The second time you
    call it, the pointer will be overwritten with a new pointer to the new
    OOWrapper object. So even though I can create many dialogues with the OOWrapper
    object, theres only one callback function, which is common to all of them...?
    Or is there some smart way of differentiating among the various dialogues when
    the callback function is called?

    : This is the standard way of dealing with the 'how do I use a C++ member
    : function as a callback' issue.
    :
    : Also see the FAQ
    :
    : http://www.parashift.com/c -faq-lite/pointers-to-members.html
    :
    : john
    :
    :
     
    prettysmurfed, Jul 22, 2003
    #5
  6. On Tue, 22 Jul 2003 19:42:28 +0200, "prettysmurfed" <> wrote:

    >is there some smart way of differentiating among the various dialogues when
    >the callback function is called?


    Using standard C++ without any platform-specific code you can do that via
    e.g. a std::map in a namespace scope variable.

    Using platform-dependent functionality you can (1) store a pointer to the
    C++ object in the API-level object, (2) associate a pointer to the C++ object
    with the API-level object, e.g. via Windows property lists, or (3) replace
    the API-level object's message processing function with a pointer to a
    dynamically created thunk that calls a C++ object method.
     
    Alf P. Steinbach, Jul 22, 2003
    #6
  7. What! I'm sorry you must've mistaken me for someone who's a bit more
    intelligent. That description went straight over/through my head.
    I've already solved the problem, actually I was going to post that no bother
    answering my previous question, as the solution was right in front of me!
    The solution I came up with is probably a bit off-topic, more a windows
    issue, but solutions never seem to care what topic they are defined within.
    The different dialogues has different hwnd's so I just differentiate among
    them with the calling hwnd.

    But Alf I'm really happy that you responded anyway.
    - Martin

    "Alf P. Steinbach" <> skrev i en meddelelse news:...
    : On Tue, 22 Jul 2003 19:42:28 +0200, "prettysmurfed" <> wrote:
    :
    : >is there some smart way of differentiating among the various dialogues when
    : >the callback function is called?
    :
    : Using standard C++ without any platform-specific code you can do that via
    : e.g. a std::map in a namespace scope variable.
    :
    : Using platform-dependent functionality you can (1) store a pointer to the
    : C++ object in the API-level object, (2) associate a pointer to the C++ object
    : with the API-level object, e.g. via Windows property lists, or (3) replace
    : the API-level object's message processing function with a pointer to a
    : dynamically created thunk that calls a C++ object method.
    :
     
    prettysmurfed, Jul 22, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,946
    Thomas Weidenfeller
    Oct 12, 2005
  2. bienwell
    Replies:
    5
    Views:
    5,243
    bienwell
    Aug 13, 2006
  3. JDT
    Replies:
    6
    Views:
    569
    James Kanze
    Mar 29, 2007
  4. Nathan Huesken
    Replies:
    0
    Views:
    238
    Nathan Huesken
    Jul 7, 2010
  5. Dan Sikorsky

    HOWTO: Force the SaveAs Dialog Box For Downloads

    Dan Sikorsky, Apr 20, 2004, in forum: ASP General
    Replies:
    3
    Views:
    267
    Dan Sikorsky
    Apr 21, 2004
Loading...

Share This Page